FROM python:3.12-slim AS runtime

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    MGMRADAR_ENV=production

RUN apt-get update \
    && apt-get install -y --no-install-recommends libexpat1 \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd --gid 10001 mgmradar \
    && useradd --uid 10001 --gid 10001 --create-home --shell /usr/sbin/nologin mgmradar

WORKDIR /app

COPY pyproject.toml README.md ./
COPY src ./src
COPY config ./config
COPY scripts ./scripts
RUN pip install --upgrade pip && pip install ".[s3]"

# Fail the image build if the native wheels cannot load at runtime. rasterio's
# manylinux wheel needs libexpat1 from the base image (installed above).
RUN python -c "import numpy, scipy, rasterio"

RUN mkdir -p /app/data && chown -R mgmradar:mgmradar /app

USER mgmradar
EXPOSE 8000

HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/v1/health', timeout=3)"

CMD ["mgmradar", "serve", "--host", "0.0.0.0", "--port", "8000"]
