# Stage 1: build the React SPA. Produces frontend/dist, which the second
# stage copies into the Python image so app/main.py's StaticFiles mount +
# catch-all route (SPA_DIST_DIR = <repo root>/frontend/dist) find it - this
# app has no separate frontend server/port in production, FastAPI serves
# the built bundle directly.
FROM node:22-alpine AS frontend-build
WORKDIR /frontend
COPY frontend/package.json frontend/package-lock.json* ./
RUN npm ci
COPY frontend .
RUN npm run build

# Stage 2: the FastAPI app, serving both the JSON API and the built SPA.
FROM python:3.11-slim
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

WORKDIR /app

RUN apt-get update \
    && apt-get install -y --no-install-recommends libpq-dev gcc \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY app ./app
COPY alembic ./alembic
COPY alembic.ini .
COPY --from=frontend-build /frontend/dist ./frontend/dist

EXPOSE 8000

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
