# One container: build the frontend, then serve it and the API from one process.
# Two services would mean two free-tier deployments, CORS, and a second URL to
# keep in sync, for an app whose whole job is that the two agree.

FROM node:20-alpine AS web
WORKDIR /build
COPY web/package*.json ./
RUN npm ci
COPY web/ ./
# Not VITE_STATIC: this build talks to the API in the same container.
RUN npm run build

FROM python:3.12-slim
WORKDIR /app

# Dependencies first, so a code change does not reinstall them.
COPY api/requirements.txt api/requirements.txt
RUN pip install --no-cache-dir -r api/requirements.txt

COPY api/ api/
COPY data/ data/
COPY --from=web /build/dist web/dist

# SQLite lives on a mounted volume when the host provides one; without a volume
# the history resets on redeploy, which is fine for a demo and not for real use.
ENV DATABASE_URL=sqlite:////data/ragsec.db
RUN mkdir -p /data
EXPOSE 8000

WORKDIR /app/api
# SEED_DEMO=0 starts empty, for a real team server; the demo seeds the published runs.
CMD ["sh", "-c", "if [ \"${SEED_DEMO:-1}\" = 1 ]; then python seed.py || true; fi; uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000}"]
