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

FROM python:3.12-slim-bookworm 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-bookworm 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).
# Cache lives on the ephemeral rootfs — fine since gpkg sources are
# already on the volume and re-warming is cheap. (Symlinking the cache
# to /data/cache breaks the library's mkdir-on-startup.)
RUN ln -sf /data /app/data

EXPOSE 8080

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