FROM node:22-alpine

WORKDIR /app

# 国内构建环境加速（仅容器构建期生效，不影响本地/正式 registry）
# 1) Alpine apk 源换清华
RUN sed -i 's|https\?://dl-cdn.alpinelinux.org|https://mirrors.tuna.tsinghua.edu.cn|g' /etc/apk/repositories

RUN corepack enable && corepack prepare pnpm@9 --activate \
  && apk add --no-cache openssl

# 2) npm / pnpm registry 换淘宝镜像
# 3) Prisma 下载引擎走淘宝镜像
ENV PRISMA_ENGINES_MIRROR=https://registry.npmmirror.com/-/binary/prisma
RUN npm config set registry https://registry.npmmirror.com/ \
  && pnpm config set registry https://registry.npmmirror.com/

# Copy everything (.dockerignore filters out node_modules, dist, .git, etc.)
COPY . .

# 强制项目级 registry（兜底：防止项目根 .npmrc 未覆盖）
RUN echo "registry=https://registry.npmmirror.com/" > /app/.npmrc

# Install all deps (tsx is used at runtime via devDependencies)
RUN pnpm install --frozen-lockfile --registry=https://registry.npmmirror.com/

# Generate Prisma client (produces linux-musl binary per schema.prisma)
RUN pnpm exec prisma generate

# Build web SPA; server runs directly from TS via tsx
RUN pnpm --filter @stamp/web build

# Stash initial stamp assets so the uploads volume can be seeded on first run
RUN mkdir -p /app/stamps-seed \
  && cp packages/server/uploads/stamps/*.jpg /app/stamps-seed/ \
  && rm -rf packages/server/uploads/stamps

COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

ENV NODE_ENV=production
EXPOSE 3000

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
