# ============================================================================
# Production Backend Dockerfile — Multi-stage Build
# Optimized from ~3.5GB down to ~800MB–1.2GB
# ============================================================================
# Stage 1 (builder): Full node:22-bookworm — install everything needed for building
# Stage 2 (runtime): node:22-bookworm-slim — minimal runtime only
# ============================================================================

# --------------------------- Builder Stage -----------------------------------
FROM docker.m.daocloud.io/library/node:22-bookworm AS builder

ARG DEBIAN_MIRROR=mirrors.cloud.tencent.com
ARG NPM_REGISTRY=https://registry.npmmirror.com

# NOTE: Use HTTP (not HTTPS) for apt mirrors — OrbStack BuildKit cannot verify
# TLS certs during build.  Apt packages are GPG-signed so HTTP is safe.
ENV DEBIAN_FRONTEND=noninteractive
ENV NPM_CONFIG_REGISTRY=${NPM_REGISTRY}

WORKDIR /app

# --- System packages for building ---
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    sed -i "s|http://deb.debian.org/debian|http://${DEBIAN_MIRROR}/debian|g" /etc/apt/sources.list.d/debian.sources \
  && sed -i "s|http://security.debian.org/debian-security|http://${DEBIAN_MIRROR}/debian-security|g" /etc/apt/sources.list.d/debian.sources \
  && apt-get update \
  && apt-get install -y --no-install-recommends \
       curl ca-certificates tzdata

# --- Node dependencies (full, including devDeps for postinstall scripts) ---
COPY apps/backend/package.json apps/backend/package-lock.json ./
RUN --mount=type=cache,target=/root/.npm \
    npm ci

# --- Playwright — install Chromium with system deps (builder has full apt) ---
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
RUN --mount=type=cache,target=/ms-playwright-cache,sharing=locked \
    PLAYWRIGHT_BROWSERS_PATH=/ms-playwright-cache \
    PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright/ \
    npx playwright install --with-deps chromium \
  && cp -r /ms-playwright-cache /ms-playwright

# --- Copy application source ---
COPY apps/backend/ ./

# --- Build application and prune devDependencies ---
RUN npm run build \
  && npm prune --omit=dev

# --------------------------- Runtime Stage -----------------------------------
FROM docker.m.daocloud.io/library/node:22-bookworm-slim AS runtime

# HTTP mirrors — see builder note about OrbStack TLS
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
ENV NODE_ENV=production
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright

WORKDIR /app

# --- Minimal runtime apt packages ---
# - curl: health check (docker-compose uses `curl -fsS http://127.0.0.1:3000/health/ready`)
# - ca-certificates: TLS for outbound HTTPS calls
# - tzdata: timezone data for TZ=Asia/Shanghai
# - Chromium runtime libs: minimal set for headless Chromium on slim
#
# NOTE: We do NOT switch to Tencent mirrors here because:
#   1. slim image has no ca-certificates pre-installed
#   2. Tencent mirrors redirect HTTP → HTTPS, which fails without ca-certificates (chicken-and-egg)
#   3. Debian official HTTP mirrors (deb.debian.org) work without TLS verification
#   4. Apt packages are GPG-signed, so HTTP is safe
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt-get update \
  && apt-get install -y --no-install-recommends \
       curl ca-certificates tzdata \
       libnss3 libnspr4 libdbus-1-3 libatk1.0-0 libatk-bridge2.0-0 \
       libcups2 libdrm2 libxkbcommon0 libatspi2.0-0 libxcomposite1 \
       libxdamage1 libxfixes3 libxrandr2 libgbm1 libpango-1.0-0 \
       libcairo2 libasound2 \
  && ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime \
  && echo ${TZ} > /etc/timezone \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

# --- Copy artefacts from builder ---
# Pruned node_modules (no devDeps)
COPY --from=builder /app/node_modules ./node_modules
# Compiled application
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/dist ./dist
# Playwright browsers
COPY --from=builder /ms-playwright /ms-playwright

# --- Non-root user for security ---
RUN groupadd -g 1001 appuser \
  && useradd -m -u 1001 -g appuser appuser \
  && mkdir -p /app/data /app/data/browser-sessions /app/data/redeem-screenshots \
  && chown -R appuser:appuser /app/data /ms-playwright

USER appuser

EXPOSE 3000

CMD ["npm", "run", "start"]
