# Shared image for both the `worker` and `web` services. Which process
# runs is decided by the `command:` in compose.yml, not by this file.
FROM debian:bookworm-slim

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

# --- gmic ------------------------------------------------------------------
# Debian bookworm's own `gmic` apt package is stuck at 2.9.4, whose bundled
# filter library is missing commands the reference pipeline needs (e.g.
# `fx_LCE`, one of the fixed POST_FILTERS — that one is not optional, so an
# old stdlib breaks every single variant, not just a random filter pick).
# We install the official gmic.eu build for Debian 12 instead (currently
# 4.0.2), which has these commands built in.
#
# Trade-off: that package also pulls in the GIMP-plugin/Qt/GTK stack as
# hard dependencies (it's one combined .deb for CLI + gimp plugin + zart),
# adding a few hundred MB we don't otherwise need for a headless CLI tool.
# Accepted pragmatically here — correctness over image size. If this ever
# matters, the alternative is compiling gmic from source with `make cli`
# (see https://gmic.eu/download.html) to skip the GUI dependencies.
ARG GMIC_DEB_URL=https://gmic.eu/get_file.php?file=linux/gmic_4.0.2_debian12_bookworm_amd64.deb

RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        python3 \
        python3-pip \
    && curl -fsSL "$GMIC_DEB_URL" -o /tmp/gmic.deb \
    && apt-get install -y --no-install-recommends /tmp/gmic.deb \
    && rm -f /tmp/gmic.deb \
    && rm -rf /var/lib/apt/lists/*

# Bake in gmic's community filter definitions at build time (one network
# fetch, ~6MB) so the wider fx_*/community filter set used by
# background/foreground name lists resolves without needing outbound
# internet at runtime. `nice` comes from coreutils (already present).
ENV HOME=/app/.home
RUN mkdir -p /app/.home && gmic -update

WORKDIR /app

COPY requirements.txt .
# Installing system-wide (no venv) is intentional here: the container
# itself is the isolation boundary, this is the standard pattern for
# Python-on-slim-Docker images.
RUN pip install --break-system-packages -r requirements.txt

COPY app ./app

# rembg downloads its ONNX model (u2net, ~176MB) from GitHub on first use
# and caches it under $HOME/.u2net — persisted via the `rembg_cache`
# volume in compose.yml so it survives container restarts.

# Overridden per-service in compose.yml (`worker` -> python -m app.worker,
# `web` -> uvicorn app.main:app).
CMD ["python3", "-m", "app.worker"]
