# syntax=docker/dockerfile:1.7
# PAC Web Dockerfile — Next.js standalone build
ARG NODE_VERSION=22-alpine

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

FROM base AS deps
COPY pnpm-workspace.yaml pnpm-lock.yaml package.json .npmrc* ./
COPY turbo.json tsconfig.base.json ./
COPY apps/pac-web/package.json apps/pac-web/
COPY packages/types/package.json packages/types/
COPY packages/utils/package.json packages/utils/
RUN pnpm install --frozen-lockfile

FROM deps AS dev
COPY . .
WORKDIR /app/apps/pac-web
EXPOSE 3100
CMD ["pnpm", "dev"]

FROM deps AS build
# NEXT_PUBLIC_* 必须 build-time 注入(Next.js 把它们 inline 进客户端 bundle)
ARG NEXT_PUBLIC_API_BASE_URL
ENV NEXT_PUBLIC_API_BASE_URL=${NEXT_PUBLIC_API_BASE_URL}
# Sentry(前端 DSN 公开,build-time inline;缺省则前端 Sentry no-op)
ARG NEXT_PUBLIC_SENTRY_DSN
ENV NEXT_PUBLIC_SENTRY_DSN=${NEXT_PUBLIC_SENTRY_DSN}
ARG NEXT_PUBLIC_SENTRY_ENVIRONMENT
ENV NEXT_PUBLIC_SENTRY_ENVIRONMENT=${NEXT_PUBLIC_SENTRY_ENVIRONMENT}
ARG NEXT_PUBLIC_SENTRY_TRACES_SAMPLE_RATE
ENV NEXT_PUBLIC_SENTRY_TRACES_SAMPLE_RATE=${NEXT_PUBLIC_SENTRY_TRACES_SAMPLE_RATE}
COPY . .
RUN pnpm --filter "./packages/*" build
WORKDIR /app/apps/pac-web
RUN pnpm build

FROM node:${NODE_VERSION} AS prod
RUN apk add --no-cache libc6-compat
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/apps/pac-web/.next/standalone ./
COPY --from=build /app/apps/pac-web/.next/static ./apps/pac-web/.next/static
COPY --from=build /app/apps/pac-web/public ./apps/pac-web/public
EXPOSE 3100
CMD ["node", "apps/pac-web/server.js"]
