ARG PYTHON_IMAGE=ghcr.io/intuitem/ciso-assistant-community/mirror/dhi-python:3.14-dev
FROM ${PYTHON_IMAGE}

# UV_NO_SYNC=1: prevent `uv run` from auto-syncing at runtime (caused startup-docker-compose-test CI to fail);
#   the venv is built once at image build (uv sync --locked --no-dev)
# UV_PYTHON_DOWNLOADS=never: fail loudly rather than silently sideline the base interpreter
ENV PYTHONUNBUFFERED=1 \
  PYTHONDONTWRITEBYTECODE=1 \
  UV_LINK_MODE=copy \
  UV_NO_SYNC=1 \
  UV_PYTHON_DOWNLOADS=never \
  PATH="/code/.venv/bin:$PATH"

COPY --from=ghcr.io/astral-sh/uv:0.12.13 /uv /bin/

WORKDIR /code

RUN apt-get update \
  && apt-get install -y --no-install-recommends \
  # perl-base: clears the perl finding from docker scout (#4820)
  perl-base \
  # not pango-1.0: resolves to libpango1.0-dev, pulls in mesa + LLVM
  libpango-1.0-0 \
  libpangoft2-1.0-0 \
  libharfbuzz0b \
  libfontconfig1 \
  libfreetype6 \
  libstdc++6 \
  zlib1g \
  libffi8 \
  libmagic1 \
  locales \
  tzdata \
  fontconfig \
  fonts-freefont-ttf \
  bubblewrap \
  # curl: only keeps pre-existing compose healthchecks working, drop once on-prem users have migrated
  curl \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/* \
  && locale-gen en_US.UTF-8 \
  && locale-gen ar_SA.UTF-8 \
  && locale-gen fr_FR.UTF-8 \
  && locale-gen pt_PT.UTF-8 \
  && locale-gen es_ES.UTF-8 \
  && locale-gen de_DE.UTF-8 \
  && locale-gen nl_NL.UTF-8 \
  && locale-gen it_IT.UTF-8 \
  && locale-gen pl_PL.UTF-8 \
  && locale-gen ro_RO.UTF-8 \
  && locale-gen hi_IN.UTF-8 \
  && locale-gen ur_PK.UTF-8 \
  && locale-gen cs_CZ.UTF-8 \
  && locale-gen sv_SE.UTF-8 \
  && locale-gen id_ID.UTF-8

COPY pyproject.toml uv.lock ./

# torch is pinned to the PyTorch CPU index in pyproject.toml/uv.lock, so the CUDA
# wheels are never resolved or downloaded — a plain locked sync is enough.
RUN --mount=type=cache,target=/root/.cache/uv \
  uv sync --locked --no-dev

# Pre-bake the embedding and reranker models into the image so the AI works with no
# internet at runtime (otherwise sentence-transformers downloads them from HuggingFace
# on first use, which breaks fully offline / air-gapped instances).
# Model IDs come from chat/embedding_models.py (single source of truth). It is copied
# first so this layer re-bakes whenever the IDs change. Network is needed at BUILD time only.
COPY chat/embedding_models.py ./chat/embedding_models.py
ENV HF_HOME=/opt/hf-cache \
  SENTENCE_TRANSFORMERS_HOME=/opt/hf-cache
RUN uv run python -c "import chat.embedding_models as m; from sentence_transformers import SentenceTransformer, CrossEncoder; SentenceTransformer(m.DEFAULT_EMBEDDING_MODEL); CrossEncoder(m.RERANKER_MODEL)" \
  && chown -R 1001:1001 /opt/hf-cache

# Force offline at runtime: the model is cached above and nothing else in the chat
# stack reaches HuggingFace (LLM and Ollama use their own URLs). Avoids a network
# round-trip on model load. Set AFTER the bake so the build can still download.
ENV HF_HUB_OFFLINE=1 \
  TRANSFORMERS_OFFLINE=1

# pip is unused (uv builds the venv); its vendored setuptools/msgpack carry CVEs.
# Both prefixes covered in case PYTHON_IMAGE is overridden.
RUN rm -rf /usr/lib/python3/dist-packages/pip \
  /usr/lib/python3/dist-packages/pip-*.dist-info \
  /usr/local/lib/python3.*/site-packages/pip \
  /usr/local/lib/python3.*/site-packages/pip-*.dist-info \
  /usr/bin/pip /usr/bin/pip3 /usr/local/bin/pip /usr/local/bin/pip3 /usr/local/bin/pip3.*

#watch out for local files during dev and maintenance of .dockerignore
COPY . .

# No `passwd` in the DHI base; installing it would pull shadow + PAM into the runtime.
RUN echo 'app:x:1001:1001::/home/app:/bin/bash' >> /etc/passwd \
  && echo 'app:x:1001:' >> /etc/group \
  && install -d -m 700 -o 1001 -g 1001 /home/app
# USER app  # not activated by default to keep compatibility

EXPOSE 8000
ENTRYPOINT ["uv", "run", "bash", "startup.sh"]
