# syntax=docker/dockerfile:1
# Multi-stage, non-root, no credentials. Pin a Debian bookworm tag — never :latest.
# Build from the lab root: docker build -f docker/Dockerfile -t baylearn/order-api:local .

FROM python:3.12-slim-bookworm AS builder

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_NO_CACHE_DIR=1

WORKDIR /build
COPY app/pyproject.toml /build/pyproject.toml
COPY app/src /build/src

RUN python -m venv /opt/venv \
    && /opt/venv/bin/pip install --upgrade pip \
    && /opt/venv/bin/pip install .

FROM python:3.12-slim-bookworm AS runtime

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PATH="/opt/venv/bin:${PATH}" \
    PYTHONPATH="/app/src" \
    TMPDIR=/tmp \
    LOG_LEVEL=INFO

RUN useradd --uid 10001 --create-home --shell /usr/sbin/nologin appuser \
    && mkdir -p /app /tmp \
    && chown -R appuser:appuser /app /tmp

COPY --from=builder --chown=appuser:appuser /opt/venv /opt/venv
COPY --from=builder --chown=appuser:appuser /build/src /app/src

WORKDIR /app
USER 10001

EXPOSE 8000

# Container-level health uses liveness (/health), not readiness.
# Kubernetes probes are the production signal; this is a local safety net.
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=2)"

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
