# 前端 Next.js Dockerfile
# 使用多阶段构建 + BuildKit 缓存优化

# ==================== 依赖安装阶段 ====================
FROM node:20-alpine AS deps
WORKDIR /app

# 安装 pnpm
RUN corepack enable && corepack prepare pnpm@latest --activate

# 复制依赖文件
COPY frontend/package.json frontend/pnpm-lock.yaml ./

# 安装依赖（使用 BuildKit 缓存加速）
RUN --mount=type=cache,target=/root/.local/share/pnpm/store \
    pnpm install --frozen-lockfile

# ==================== 构建阶段 ====================
FROM node:20-alpine AS builder
WORKDIR /app

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

# 复制依赖
COPY --from=deps /app/node_modules ./node_modules
COPY frontend/ ./

# 设置环境变量（构建时使用）
ARG NEXT_PUBLIC_API_URL
ARG IMAGE_TAG=unknown
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
ENV NEXT_PUBLIC_VERSION=${IMAGE_TAG}
# Docker 内部网络使用服务名 server 作为后端地址
ENV API_HOST=server

# 构建（使用 BuildKit 缓存加速）
RUN --mount=type=cache,target=/app/.next/cache \
    pnpm build

# ==================== 运行阶段 ====================
FROM node:20-alpine AS runner
WORKDIR /app

ENV NODE_ENV=production

# 创建非 root 用户
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# 复制构建产物
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

USER nextjs

EXPOSE 3000

ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

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