# syntax=docker/dockerfile:1.7
FROM python:3.12-slim

ENV PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_NO_CACHE_DIR=1 \
    HF_HOME=/root/.cache/huggingface

# Build deps for apsw + sqlite-vec. apt cache cleaned in same layer to keep
# the image slim. We pin sqlite-extension loading on at build time so apsw
# can dlopen vec0 at runtime.
RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        ca-certificates \
        curl \
        git \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install the PyTorch CPU wheel from the dedicated index BEFORE the project
# install, so the project's `torch>=2.3` constraint resolves to the CPU build
# rather than dragging in the (much larger) CUDA wheel from PyPI default.
RUN pip install --index-url https://download.pytorch.org/whl/cpu \
        "torch>=2.3"

# Copy source and install (editable so PR2 / iteration is fast).
COPY pyproject.toml README.md ./
COPY src/ ./src/
RUN pip install -e .

# Default HTTP bind — overridden by env at runtime.
EXPOSE 8001

# start-period is generous: first boot downloads bge-large (~1.3GB) into
# hf-cache. Subsequent boots reuse the cache and start in seconds.
HEALTHCHECK --interval=30s --timeout=5s --start-period=600s --retries=3 \
    CMD curl -sf "http://127.0.0.1:${INDEXER_PORT:-8001}/health" || exit 1

CMD ["kv-indexer"]
