# Stage 1: Builder
# Base images pinned by digest and Python dependencies by hash
# (requirements-docker.txt): every build installs the same files. Dependabot
# proposes base-image updates; scripts/update_docker_requirements.sh refreshes the lock.
FROM python:3.11-slim@sha256:7032841967d07aa3353905945dc186901c89b6b319c61425f4439f677a4a8a34 AS builder

WORKDIR /build

RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

COPY requirements-docker.txt .
COPY setup.py pyproject.toml MANIFEST.in README.md LICENSE ./

RUN pip install --no-cache-dir --require-hashes -r requirements-docker.txt

COPY kosty/ ./kosty/

# MANIFEST.in is what ships plugins.sha256 (the plugin integrity manifest);
# without it the image had no manifest and strict_plugins refused every check.
RUN pip install --no-cache-dir --no-deps . && \
    python -c "import os, kosty; p = os.path.join(os.path.dirname(kosty.__file__), 'providers', 'aws', 'services', 'plugins.sha256'); assert os.path.exists(p), 'plugins.sha256 missing from the image'"

# Stage 2: Runner (Distroless)
FROM gcr.io/distroless/python3-debian12:nonroot@sha256:7d1042ce588ab97019fe95c24ffca7bc5a82ccdac572511d5e09bda4435c89c5


COPY --from=builder --chown=nonroot:nonroot /opt/venv /opt/venv

ENV PATH="/opt/venv/bin:$PATH" \
    PYTHONPATH="/opt/venv/lib/python3.11/site-packages" \
    PYTHONUNBUFFERED=1

WORKDIR /home/nonroot

USER nonroot

ENTRYPOINT ["python3", "-m", "kosty.cli"]
