# syntax=docker/dockerfile:1.7

# =============================================================================
# Tx OS — single Dockerfile with three targets:
#   --target api → runs the Express + Socket.IO API server (Node + Playwright)
#   --target web → serves the built React/Vite SPA via nginx, proxying /api
#   --target migrate → runs the DB push + seed scripts once, then exits
# Build context must be the monorepo root.
# =============================================================================

ARG NODE_VERSION=24
ARG PLAYWRIGHT_VERSION=1.59.1

# -----------------------------------------------------------------------------
# Stage 1: install workspace dependencies (cached layer for source rebuilds)
# -----------------------------------------------------------------------------
FROM node:${NODE_VERSION}-bookworm-slim AS deps
WORKDIR /app
RUN corepack enable && corepack prepare pnpm@10.26.1 --activate

# Copy lockfile + every package manifest first so dependency installation
# is cached as long as no manifest changes.
COPY pnpm-workspace.yaml pnpm-lock.yaml package.json ./
COPY artifacts/api-server/package.json artifacts/api-server/
COPY artifacts/tx-os/package.json artifacts/tx-os/
COPY artifacts/mockup-sandbox/package.json artifacts/mockup-sandbox/
COPY scripts/package.json scripts/
COPY lib lib
# Re-copy lib package.json files (lib has its own subprojects with manifests)
RUN find lib -name node_modules -prune -o -name dist -prune -o -type f -print | head -1 >/dev/null

ENV CI=true \
    HUSKY=0 \
    PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store \
    pnpm install --frozen-lockfile --prefer-offline

# -----------------------------------------------------------------------------
# Stage 2: build all workspace artifacts
# -----------------------------------------------------------------------------
FROM deps AS build
WORKDIR /app
COPY . .
# Build API (esbuild bundle to dist/index.mjs).
RUN pnpm --filter @workspace/api-server build
# Build SPA (Vite static bundle to dist/public). PORT/BASE_PATH are
# required by vite.config.ts at build time even though the static
# bundle is served by nginx — give them placeholder values.
RUN PORT=3000 BASE_PATH=/ pnpm --filter @workspace/tx-os build
# Compile TS in scripts/ for the migrate target.
RUN pnpm --filter scripts run build || true

# -----------------------------------------------------------------------------
# Stage 3: API runtime — Playwright base ships Chromium + Linux deps for PDF.
# -----------------------------------------------------------------------------
FROM mcr.microsoft.com/playwright:v${PLAYWRIGHT_VERSION}-jammy AS api
WORKDIR /app
ENV NODE_ENV=production \
    NODE_OPTIONS="--enable-source-maps"
# Bring across the api server bundle + its node_modules (esbuild externalises
# Playwright/PDFKit/etc. at runtime, so we need the installed packages here).
COPY --from=build /app/artifacts/api-server/dist artifacts/api-server/dist
COPY --from=build /app/artifacts/api-server/node_modules artifacts/api-server/node_modules
COPY --from=build /app/artifacts/api-server/assets artifacts/api-server/assets
COPY --from=build /app/node_modules node_modules
EXPOSE 8080
WORKDIR /app/artifacts/api-server
CMD ["node", "--enable-source-maps", "./dist/index.mjs"]

# -----------------------------------------------------------------------------
# Stage 4: web runtime — nginx serves the SPA + proxies /api to the API.
# -----------------------------------------------------------------------------
FROM nginx:1.27-alpine AS web
COPY --from=build /app/artifacts/tx-os/dist/public /usr/share/nginx/html
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
  CMD wget -qO- http://localhost/healthz >/dev/null || exit 1

# -----------------------------------------------------------------------------
# Stage 5a: mockup-sandbox runtime (dev-only). Carries source + node_modules
# so `pnpm --filter @workspace/mockup-sandbox dev` can run the Vite preview
# server. Not part of the default compose stack — gated behind the `dev` profile.
# -----------------------------------------------------------------------------
FROM node:${NODE_VERSION}-bookworm-slim AS mockup
WORKDIR /app
RUN corepack enable && corepack prepare pnpm@10.26.1 --activate
COPY --from=build /app /app
ENV NODE_ENV=development \
    PORT=8081 \
    BASE_PATH=/__mockup
EXPOSE 8081
CMD ["pnpm", "--filter", "@workspace/mockup-sandbox", "run", "dev"]

# -----------------------------------------------------------------------------
# Stage 5b: one-shot DB migration + seed runner (used by `docker compose run migrate`).
# -----------------------------------------------------------------------------
FROM node:${NODE_VERSION}-bookworm-slim AS migrate
WORKDIR /app
RUN corepack enable && corepack prepare pnpm@10.26.1 --activate
COPY --from=build /app /app
ENV NODE_ENV=production
CMD ["sh", "-c", "pnpm --filter db run push-force && pnpm --filter scripts run seed"]
