# Simplified Dockerfile for API Service
FROM node:20-alpine AS builder
WORKDIR /app

# Install pnpm
RUN npm install -g pnpm@10.11.0

# Copy everything needed
COPY pnpm-workspace.yaml package.json pnpm-lock.yaml ./
COPY apps/api-service ./apps/api-service
COPY packages/database ./packages/database

# Install and build
RUN pnpm install --frozen-lockfile
RUN pnpm --filter @banatie/database build
RUN pnpm --filter @banatie/api-service build

# Production runner
FROM node:20-alpine AS production
WORKDIR /app

ENV NODE_ENV=production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 apiuser

# Copy built app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/packages/database ./packages/database
COPY --from=builder /app/apps/api-service/dist ./apps/api-service/dist
COPY --from=builder /app/apps/api-service/package.json ./apps/api-service/
COPY --from=builder /app/apps/api-service/node_modules ./apps/api-service/node_modules

# Create directories
RUN mkdir -p /app/apps/api-service/logs /app/results /app/uploads/temp
RUN chown -R apiuser:nodejs /app/apps/api-service/logs /app/results /app/uploads

USER apiuser
EXPOSE 3000
WORKDIR /app/apps/api-service

CMD ["node", "dist/server.js"]
