# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
RUN npm install -g pnpm

# 复制 workspace 配置
COPY package.json pnpm-lock.yaml* pnpm-workspace.yaml ./

# 复制 shared-proto（gRPC proto-loader 运行时加载）
COPY packages/shared-proto ./packages/shared-proto

# 复制 shared-ts（GraphQL schema 文件）
COPY packages/shared-ts ./packages/shared-ts

# 复制 teacher-bff 源码
COPY services/teacher-bff ./services/teacher-bff

# 安装依赖（仅 teacher-bff + shared-ts）
RUN cd services/teacher-bff && pnpm install --frozen-lockfile

# 构建
WORKDIR /app/services/teacher-bff
RUN pnpm build

# Runtime stage
FROM node:20-alpine
WORKDIR /app
RUN npm install -g pnpm

# 复制 workspace 配置
COPY package.json pnpm-lock.yaml* pnpm-workspace.yaml ./

# 复制 shared-proto + shared-ts（运行时需要）
COPY packages/shared-proto ./packages/shared-proto
COPY packages/shared-ts ./packages/shared-ts

# 复制 teacher-bff
COPY services/teacher-bff ./services/teacher-bff

WORKDIR /app/services/teacher-bff
RUN pnpm install --prod --frozen-lockfile

# 复制构建产物
COPY --from=builder /app/services/teacher-bff/dist ./dist

EXPOSE 3003
CMD ["node", "dist/main.js"]
