FROM node:20-alpine AS builder

WORKDIR /app

# Copy package files
COPY package.json pnpm-lock.yaml ./
COPY packages/*/package.json ./packages/*/
COPY services/legal-documents/package.json ./services/legal-documents/

# Install pnpm
RUN npm install -g pnpm

# Install dependencies
RUN pnpm install --frozen-lockfile

# Copy source code
COPY . .

# Build
RUN pnpm build --filter=@the-order/legal-documents

FROM node:20-alpine AS runner

WORKDIR /app

# Copy package files
COPY package.json pnpm-lock.yaml ./
COPY packages/*/package.json ./packages/*/
COPY services/legal-documents/package.json ./services/legal-documents/

# Install pnpm
RUN npm install -g pnpm

# Install production dependencies
RUN pnpm install --frozen-lockfile --prod

# Copy built files
COPY --from=builder /app/services/legal-documents/dist ./services/legal-documents/dist
COPY --from=builder /app/packages ./packages

WORKDIR /app/services/legal-documents

EXPOSE 4005

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

