# syntax=docker/dockerfile:1.7
# PAC Docs Dockerfile — Next.js (Fumadocs) 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-docs/package.json apps/pac-docs/
COPY packages/types/package.json packages/types/
COPY packages/utils/package.json packages/utils/
# --ignore-scripts:跳过 pac-docs 的 `postinstall: fumadocs-mdx`(此刻 content/source.config.ts 还没 COPY 进来会失败);
# .source 由 build 阶段的 `fumadocs-mdx && next build` 生成。
RUN pnpm install --frozen-lockfile --ignore-scripts

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

FROM deps AS build
COPY . .
WORKDIR /app/apps/pac-docs
# OpenAPI server 按环境注入(Scalar cURL/Send 的 base URL):committed spec 默认 localhost:3101(本地开箱即对),
# 线上 build 传 --build-arg DOCS_API_URL=https://<域名> 覆盖成生产地址。server 在 build 期烘进 SSG 页,故须在 next build 前改。
ARG DOCS_API_URL=http://localhost:3101
RUN node -e "const p='openapi/pac.json',fs=require('fs');const d=JSON.parse(fs.readFileSync(p));d.servers=[{url:'$DOCS_API_URL',description:'API'}];fs.writeFileSync(p,JSON.stringify(d,null,2)+'\n')" \
 && echo "OpenAPI server → $DOCS_API_URL" \
 && pnpm build

FROM node:${NODE_VERSION} AS prod
RUN apk add --no-cache libc6-compat
WORKDIR /app
ENV NODE_ENV=production
# MDX 内容 build 期已编译进产物;但 openapi/pac.json 由 source 在【运行时】读取(搜索 / og / llms 路由),
# standalone 文件追踪不会带它 → 显式拷,否则这些动态路由 ENOENT 500。
COPY --from=build /app/apps/pac-docs/.next/standalone ./
COPY --from=build /app/apps/pac-docs/.next/static ./apps/pac-docs/.next/static
COPY --from=build /app/apps/pac-docs/openapi ./apps/pac-docs/openapi
EXPOSE 3102
CMD ["node", "apps/pac-docs/server.js"]
