# Kamidso API. Build context is backend/ (Coolify base directory /backend,
# and the backend service in docker-compose.yml).
#
# Cache contract:
#   1. Base image and apt packages
#   2. requirements.txt -> pip install into /opt/venv
#   3. Application source
# A Python edit must leave the pip layer CACHED. A requirements.txt edit must not.
#
# The app is not installed as a distribution. `python -m kamidso.cli` imports
# the tree from /app, so source changes stay in the last COPY.
# requirements.txt is the install input (same file Liara uses).
# pyproject.toml is for local tooling and is not the image install input.
#
# Mirrors are build args. Empty values keep Debian and PyPI.

ARG PYTHON_IMAGE=python:3.12-slim-bookworm

FROM ${PYTHON_IMAGE} AS builder

# Build-only proxy. Do not copy these into an ENV; the running API must not
# use a global proxy. OpenRouter goes through OPENROUTER_PROXY_URL at runtime.
ARG HTTP_PROXY
ARG HTTPS_PROXY
ARG http_proxy
ARG https_proxy
ARG NO_PROXY
ARG no_proxy

ARG DEBIAN_MIRROR
ARG DEBIAN_SECURITY_MIRROR

ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PYTHONDONTWRITEBYTECODE=1

COPY docker/apt-mirror.py /tmp/apt-mirror.py
RUN python /tmp/apt-mirror.py \
    && apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        gcc \
        g++ \
        git \
        pkg-config \
        libffi-dev \
        libssl-dev \
    && rm -rf /var/lib/apt/lists/*

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

WORKDIR /build
COPY requirements.txt .

# Empty keeps https://pypi.org/simple. Set in Coolify for an Iran mirror.
# Defaults are empty strings so `set -u` does not fail when the arg is omitted.
ARG PIP_INDEX_URL=
ARG PIP_TRUSTED_HOST=

RUN --mount=type=cache,target=/root/.cache/pip \
    set -eu; \
    index_args=""; \
    if [ -n "${PIP_INDEX_URL}" ]; then index_args="--index-url ${PIP_INDEX_URL}"; fi; \
    trust_args=""; \
    if [ -n "${PIP_TRUSTED_HOST}" ]; then trust_args="--trusted-host ${PIP_TRUSTED_HOST}"; fi; \
    python -m pip install --upgrade pip $index_args $trust_args \
    && python -m pip install $index_args $trust_args -r requirements.txt

FROM ${PYTHON_IMAGE}

ARG DEBIAN_MIRROR
ARG DEBIAN_SECURITY_MIRROR

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONUTF8=1 \
    PATH="/opt/venv/bin:$PATH" \
    VIRTUAL_ENV=/opt/venv \
    KAMIDSO_HOST=0.0.0.0 \
    KAMIDSO_PORT=5670

COPY docker/apt-mirror.py /tmp/apt-mirror.py
RUN python /tmp/apt-mirror.py \
    && apt-get update && apt-get install -y --no-install-recommends \
        curl \
        git \
        graphviz \
        libgomp1 \
        libgfortran5 \
    && rm -rf /var/lib/apt/lists/* \
    && rm -f /tmp/apt-mirror.py

COPY --from=builder /opt/venv /opt/venv

WORKDIR /app
COPY . .

EXPOSE 5670

HEALTHCHECK --interval=20s --timeout=5s --start-period=180s --retries=10 \
    CMD curl -fsS http://127.0.0.1:5670/health/live || exit 1

CMD ["python", "-m", "kamidso.cli", "serve", "--host", "0.0.0.0", "--port", "5670"]
