FROM node:22-alpine AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable && corepack prepare pnpm@11.5.2 --activate

FROM base AS deps
WORKDIR /app
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY apps/api/package.json apps/api/
COPY apps/player/package.json apps/player/
COPY apps/admin/package.json apps/admin/
COPY packages/shared/package.json packages/shared/
RUN pnpm install --frozen-lockfile

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/apps/api/node_modules ./apps/api/node_modules
COPY --from=deps /app/apps/player/node_modules ./apps/player/node_modules
COPY --from=deps /app/apps/admin/node_modules ./apps/admin/node_modules
COPY --from=deps /app/packages/shared/node_modules ./packages/shared/node_modules
COPY . .
# 移除历史中文目录（解压 zip 覆盖部署时可能残留，Linux 下会导致 vite publicDir 复制失败）
RUN find packages/shared/public -mindepth 1 -maxdepth 1 -type d \
  ! -name flags ! -name players -exec rm -rf {} + 2>/dev/null || true
RUN pnpm --filter @thebet365/shared build
RUN pnpm --filter @thebet365/player build

FROM nginx:1.27-alpine
COPY docker/nginx/player.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/apps/player/dist /usr/share/nginx/html
EXPOSE 80
