# ── Stage 1: Build frontend (optional, can pre-build locally) ──
FROM node:22-alpine AS frontend
WORKDIR /project
COPY web/package.json web/package-lock.json ./web/
RUN cd web && npm ci --legacy-peer-deps
COPY web/ ./web/
COPY docs/ ./docs/
RUN cd web && npm run build

# ── Stage 2: Python runtime ──
FROM python:3.12-slim
WORKDIR /app

# Copy project files
COPY pyproject.toml README.md ./
COPY src/ ./src/

# Install Python dependencies (web extras) — use Tsinghua mirror for speed in China
RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn ".[web]"

# Copy built frontend (from Stage 1 or pre-built)
COPY --from=frontend /project/web/dist ./web/dist

# Configure FastAPI to serve static files
ENV PYTHONUNBUFFERED=1

EXPOSE 8000

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