FROM python:3.12-slim AS runtime

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    OSM_LEAD_SOURCE_WRITER_ENABLED=false \
    OSM_LEAD_SOURCE_LEGACY_WRITER_DISABLED=false \
    OSM_LEAD_SOURCE_SCHEDULER_ENABLED=false

RUN apt-get update \
    && apt-get install --yes --no-install-recommends libexpat1 osmium-tool \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd --system osmservice \
    && useradd --system --gid osmservice --home-dir /app --shell /usr/sbin/nologin osmservice \
    && install --directory --owner=osmservice --group=osmservice --mode=0750 \
        /var/lib/osm-lead-source \
        /var/lib/osm-lead-source/cache \
        /var/lib/osm-lead-source/reports

WORKDIR /app

COPY pyproject.toml README.md ./
COPY src ./src
RUN python -m pip install --no-cache-dir .

COPY alembic.ini ./
COPY migrations ./migrations
COPY docs ./docs

USER osmservice
EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/healthz', timeout=3).read()"]

CMD ["osm-lead-source", "serve", "--host", "0.0.0.0", "--port", "8080"]
