FROM python:3.12-slim

WORKDIR /app

# Install system dependencies (curl for uv, ca-certificates for HTTPS)
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Install uv package manager
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
ENV PATH="/root/.local/bin:$PATH"

# Copy dependency files and install into container virtualenv
COPY pyproject.toml uv.lock* ./
RUN uv sync --frozen --no-install-project

# Copy application files
COPY streamlit_app/ ./streamlit_app/
COPY scripts/ ./scripts/
COPY dbt_nba/reports/ ./dbt_nba/reports/
COPY data/ ./data/

# Make scripts executable
RUN chmod +x scripts/*.sh scripts/*.py

# Expose Streamlit default port
EXPOSE 8501

# Healthcheck to monitor Streamlit availability
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
  CMD curl --fail http://localhost:8501/_stcore/health || exit 1

# Launch: smart sync DB from Cloudflare R2, then start Streamlit
CMD ["sh", "-c", "uv run python scripts/db_storage.py download && uv run streamlit run streamlit_app/app.py --server.port=8501 --server.address=0.0.0.0 --server.headless=true --server.enableCORS=false --server.enableXsrfProtection=false"]