FROM node:22-alpine AS base

FROM base AS builder

RUN apk add --no-cache gcompat
RUN corepack enable && corepack prepare pnpm@10.18.2 --activate
WORKDIR /app

# 复制 monorepo 根配置（catalog、lockfile 等）
COPY pnpm-workspace.yaml pnpm-lock.yaml package.json ./
# 复制 csms 应用源码
COPY apps/csms/package.json ./apps/csms/package.json
COPY apps/csms/esbuild.config.js ./apps/csms/esbuild.config.js
COPY apps/csms/tsconfig.json ./apps/csms/tsconfig.json
COPY apps/csms/src ./apps/csms/src

RUN pnpm install --filter csms && \
    pnpm --filter csms run build:prod

FROM base AS runner
WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 hono

COPY --from=builder --chown=hono:nodejs /app/apps/csms/dist /app/dist
COPY --from=builder --chown=hono:nodejs /app/apps/csms/package.json /app/package.json

USER hono
EXPOSE 3001

CMD ["node", "/app/dist/index.js"]
