# Garden v2 — 3-stage build per /srv/projects/standards/react.md:
#   1. python "schema": dump the OpenAPI schema from the backend
#   2. node "web": generate the TS client from it + build the Vite dist
#      (tsc failure = failed image build — the free cross-boundary check)
#   3. python runtime: backend + built dist. Node exists ONLY in stage 2.

FROM python:3.12-slim AS schema
WORKDIR /build
COPY backend/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY backend/app ./app
COPY backend/dump_openapi.py ./
RUN python dump_openapi.py

FROM node:22-slim AS web
WORKDIR /web
COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci --no-fund --no-audit
COPY frontend/ ./
# version.py is read by vite.config.ts (single source of truth for the version)
COPY backend/app/version.py ../backend/app/version.py
COPY --from=schema /build/openapi.json ../backend/openapi.json
RUN npm run generate && npm run build

FROM python:3.12-slim AS runtime
WORKDIR /app
COPY backend/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY backend/app ./app
COPY backend/alembic ./alembic
COPY backend/alembic.ini ./
# Migration tooling ships in the image — cutover night runs it in-container
# (the v1 snapshot lives on the /data volume).
COPY scripts ./scripts
COPY --from=web /web/dist ./dist

ENV FRONTEND_DIST=/app/dist
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
