# ── Build stage ──────────────────────────────────────────
FROM node:22-alpine AS builder
# Browser → PB goes through nginx at /pb (same origin) — fixed for docker.
# ENV PUBLIC_PB_URL=/pb
RUN corepack enable
WORKDIR /app
COPY . .
RUN pnpm install --frozen-lockfile
RUN pnpm --filter famchamp build
RUN pnpm --filter proxy build

# ── Runtime stage ────────────────────────────────────────
FROM node:22-alpine
# Browser → PB goes through nginx at /pb (same origin). PB_ENDPOINT (server →
# PB) is a loopback constant in the app code, not an env var.
# ENV PUBLIC_PB_URL=/pb
RUN corepack enable && apk add --no-cache nginx wget unzip ca-certificates
WORKDIR /app
COPY --from=builder /app/frontend/build ./frontend/build
COPY --from=builder /app/proxy/dist ./proxy/dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/pnpm-lock.yaml ./
COPY docker/nginx.conf /etc/nginx/http.d/default.conf
COPY docker/entrypoint.sh /entrypoint.sh
# Bundle PocketBase (same version as Dockerfile.dev) into the app container.
RUN wget -qO /tmp/pb.zip https://github.com/pocketbase/pocketbase/releases/download/v0.25.8/pocketbase_0.25.8_linux_amd64.zip \
    && unzip -o /tmp/pb.zip -d /usr/local/bin/ \
    && rm /tmp/pb.zip
RUN chmod +x /entrypoint.sh /usr/local/bin/pocketbase
# ENV FRONTEND_PORT=2080
# ENV PROXY_PORT=3456
# Public entrypoint. The host port is chosen at deploy time via compose PORT;
# EXPOSE here is documentation only (nginx listens on 3001). PB (8090) stays
# internal — never published from the image.
EXPOSE 3000 8090
CMD ["/entrypoint.sh"]
