# Sales Trainer — reproducible single-container build.
# The frontend is built inside the image so a clean clone never depends on
# locally generated/stale frontend/dist files.

FROM node:22-bookworm-slim AS frontend-builder
WORKDIR /build/frontend
COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci
COPY frontend/ ./
RUN npm run build

FROM python:3.11-slim

WORKDIR /app

# Copy the exact artifact produced by the builder stage, not the build context.
COPY --from=frontend-builder /build/frontend/dist/ ./frontend/dist/

COPY backend/requirements.txt backend/requirements.lock.txt ./backend/
RUN pip install --no-cache-dir --require-hashes -r backend/requirements.lock.txt
COPY backend/ ./backend/

WORKDIR /app/backend
ENV DATA_DIR=/app/backend/data
ENV FLASK_DEBUG=0
EXPOSE 5001
VOLUME ["/app/backend/data"]
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:5001/ready', timeout=4)" || exit 1
# Keep one worker while JSON stores are authoritative. Multi-worker operation
# is gated on the PostgreSQL repository cutover and shared rate-limit store.
CMD ["gunicorn", "--bind", "0.0.0.0:5001", "--workers", "1", "--threads", "4", "--access-logfile", "-", "--error-logfile", "-", "run:app"]
