# syntax=docker/dockerfile:1.7
# PAC Service Dockerfile — workspace monorepo build
ARG NODE_VERSION=22-alpine

# === base: workspace layout + pnpm ===
FROM node:${NODE_VERSION} AS base
RUN apk add --no-cache libc6-compat openssl
RUN corepack enable && corepack prepare pnpm@10.13.1 --activate
WORKDIR /app

# === deps: install everything once, share across stages ===
FROM base AS deps
COPY pnpm-workspace.yaml pnpm-lock.yaml package.json .npmrc* ./
COPY turbo.json tsconfig.base.json ./
COPY apps/pac-service/package.json apps/pac-service/
COPY packages/types/package.json packages/types/
COPY packages/utils/package.json packages/utils/
RUN pnpm install --frozen-lockfile

# === dev: hot reload via nest start --watch ===
FROM deps AS dev
COPY . .
WORKDIR /app/apps/pac-service
RUN pnpm prisma generate
EXPOSE 3101
CMD ["pnpm", "dev"]

# === build ===
FROM deps AS build
COPY . .
RUN pnpm --filter "./packages/*" build
WORKDIR /app/apps/pac-service
RUN pnpm prisma generate
RUN pnpm build

# === prod runtime ===
FROM node:${NODE_VERSION} AS prod
RUN apk add --no-cache libc6-compat openssl
RUN corepack enable && corepack prepare pnpm@10.13.1 --activate
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/packages ./packages
COPY --from=build /app/apps/pac-service/node_modules ./apps/pac-service/node_modules
COPY --from=build /app/apps/pac-service/dist ./apps/pac-service/dist
COPY --from=build /app/apps/pac-service/prisma ./apps/pac-service/prisma
COPY --from=build /app/apps/pac-service/data ./apps/pac-service/data
COPY --from=build /app/apps/pac-service/package.json ./apps/pac-service/
WORKDIR /app/apps/pac-service
EXPOSE 3101
CMD ["node", "--enable-source-maps", "dist/main.js"]
