# syntax=docker/dockerfile:1
# ───────────── 1. deps：安装依赖 + 显式生成 Prisma Client ─────────────
FROM node:22-alpine AS deps
RUN apk add --no-cache libc6-compat openssl
WORKDIR /app

RUN corepack enable && corepack prepare pnpm@latest --activate \
    && pnpm config set registry https://registry.npmmirror.com

# .npmrc 必须先 COPY，否则 pnpm install 看不到 node-linker=hoisted
COPY .npmrc package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY prisma ./prisma

# pnpm 10+ 在 root/CI 默认跳过 lifecycle scripts，因此显式调用 prisma generate
# Prisma 6 直接把 client 写入 @prisma/client 包目录（不再用 .prisma/client）
RUN pnpm install --frozen-lockfile --ignore-scripts \
    && pnpm exec prisma generate \
    && ls -la /app/node_modules/@prisma/client/ \
    && ls /app/node_modules/@prisma/client/ | grep -E "(libquery_engine|schema.prisma|index.js)" || true

# ───────────── 2. builder：Next.js 构建（standalone 产物） ─────────────
FROM node:22-alpine AS builder
RUN apk add --no-cache libc6-compat openssl
WORKDIR /app

RUN corepack enable && corepack prepare pnpm@latest --activate

COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Build-time public env：NEXT_PUBLIC_* 必须在 next build 之前注入,
# 否则会被烧成空字符串,运行时再设也无效 (Next.js 把这类 env 编译进 client bundle)
ARG NEXT_PUBLIC_TOS_DOMAIN
ENV NEXT_PUBLIC_TOS_DOMAIN=${NEXT_PUBLIC_TOS_DOMAIN}
ENV NEXT_TELEMETRY_DISABLED=1
# COPY . . 会覆盖 prisma/schema 的最新版本，需要再 generate 一次确保 client 同步
RUN pnpm exec prisma generate \
    && pnpm exec next build \
    && ls -la /app/node_modules/@prisma/client/ \
    && ls -la /app/.next/standalone/

# ───────────── 3. runner：最小运行时镜像 ─────────────
FROM node:22-alpine AS runner
RUN apk add --no-cache libc6-compat openssl
WORKDIR /app

ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3000
ENV HOSTNAME=0.0.0.0

RUN addgroup --system --gid 1001 nodejs \
    && adduser --system --uid 1001 nextjs

# Next.js standalone 自带通过 tracing 解析出的运行时依赖（含 @prisma/client）
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

# 显式补 Prisma：tracing 有时会漏掉 engine 二进制和 schema
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/@prisma ./node_modules/@prisma
COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma

USER nextjs
EXPOSE 3000

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