# Stage 1: Build the React frontend
FROM node:22-alpine AS frontend-builder
WORKDIR /app/frontend

# Install dependencies and build the static assets
COPY frontend/package*.json ./
RUN npm ci
COPY frontend/ .
RUN npm run build

# Stage 2: Build the Python backend and serve
FROM python:3.13-slim
WORKDIR /app

# Install system dependencies (for building PostgreSQL drivers and other native extensions)
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    git \
    && rm -rf /var/lib/apt/lists/*

# Install uv package manager
RUN pip install uv

# Copy dependency files
COPY pyproject.toml uv.lock ./

# Install python dependencies without the current package (speeds up layer caching)
RUN uv sync --frozen --no-install-project --no-dev

# Copy the rest of the application
COPY . .

# Copy the built frontend static assets from Stage 1
COPY --from=frontend-builder /app/frontend/dist /app/frontend/dist

# Expose FastAPI and Ray Dashboard ports
EXPOSE 8000 8265

# Start the application
CMD ["uv", "run", "python", "main.py"]
