# syntax=docker/dockerfile:1.7
# Multi-stage build: small final image, no toolchain weight.

FROM python:3.12-slim AS builder

# Build deps for geopandas / rasterio / pyproj (compiled wheels need GDAL libs).
RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        curl \
        ca-certificates \
        gdal-bin \
        libgdal-dev \
        libgeos-dev \
        libproj-dev \
        proj-bin \
    && rm -rf /var/lib/apt/lists/*

# uv: fast install + lockfile-aware sync.
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
ENV PATH="/root/.local/bin:${PATH}"

WORKDIR /app
COPY pyproject.toml uv.lock README.md ./
COPY src ./src
COPY api ./api

RUN uv sync --frozen --no-dev --no-editable


FROM python:3.12-slim AS runtime

# Runtime-only geo libraries (no -dev headers).
RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        gdal-bin \
        libgdal32 \
        libgeos-c1v5 \
        libproj25 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY --from=builder /app /app

ENV PATH="/app/.venv/bin:${PATH}" \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    MIREYE_DATA_DIR=/data

# /data is the Fly volume mount (gpkg/parquet files, ~3GB).
# Hot disk cache also lives on the volume so it survives restarts.
RUN mkdir -p /data/cache /root/.cache \
    && ln -sf /data/cache /root/.cache/mireye-earth \
    && ln -sf /data /app/data

EXPOSE 8080

CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8080", "--workers", "1"]
