# Use a Python version with pre-built wheels for Streamlit/pyarrow
# Pin to 3.12 to avoid uv selecting a newer unreleased 3.14 and forcing a pyarrow source build
FROM python:3.12-slim

RUN apt update
RUN apt install curl -y

# Set the working directory
WORKDIR /app

# Install uv package manager
RUN pip install --no-cache-dir uv

# Copy dependency definition files first to leverage Docker cache
COPY pyproject.toml ./
COPY proxy/pyproject.toml proxy/

# Copy all apps directory structure (includes all pyproject.toml files)
COPY apps/ apps/

## Sync all workspace dependencies
## uv was previously downloading Python 3.14 (pre-release) due to open-ended requires-python (>=3.12)
## which lacks pre-built pyarrow wheels, triggering a C++/cmake build and failing.
## We pin the image to 3.12 and also export UV_PYTHON so uv reuses it.
ENV UV_PYTHON=/usr/local/bin/python
# Create lockfile if not present (first build) then install
RUN if [ ! -f uv.lock ]; then uv lock; fi && uv sync

# Copy remaining source code
COPY proxy/ /app/proxy/
COPY scripts/ /app/scripts/

# Make the start script executable
RUN chmod +x /app/scripts/start.py

# Expose the port the FastAPI proxy runs on
EXPOSE 8080

# Set environment variables for Python
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app

# Command to run the application using uv run
CMD ["uv", "run", "python", "/app/scripts/start.py"]
