# --- Stage 1: build the wheel -----------------------------------------------
FROM python:3.12-slim AS builder

# Pinned uv version: two builds a month apart use the same uv.
# Equivalent: copy the binary from the official image instead,
# COPY --from=ghcr.io/astral-sh/uv:0.11.8 /uv /bin/
RUN pip install --no-cache-dir uv==0.11.8

WORKDIR /app
COPY . .

# Exact versions from uv.lock (without the dev group) in a format pip reads,
# then the project packaged as a wheel in dist/.
RUN uv export --locked --no-dev --no-hashes --no-emit-project \
        -o requirements.txt && \
    uv build --wheel

# --- Stage 2: final image, without uv or source code ------------------------
# Alpine works because dependencies are installed here: pip picks their musl
# wheels. A .venv built on Debian (glibc) would not run on it.
FROM python:3.12-alpine

RUN adduser -D -u 1000 app

# Dependencies first, then the wheel: a code change only reinstalls the wheel.
COPY --from=builder /app/requirements.txt /tmp/
RUN pip install --no-cache-dir -r /tmp/requirements.txt
COPY --from=builder /app/dist/*.whl /tmp/
RUN pip install --no-cache-dir --no-deps /tmp/*.whl

USER 1000
WORKDIR /data

ENTRYPOINT ["fil-rouge"]
CMD ["/data/input", "/data/output"]
