# Base ML Image - Contains all heavy ML dependencies
# This image is built once and reused by all ML services (svc-ocr, svc-rag-indexer, svc-rag-retriever)
# 
# Build: docker build -t gitea.harkon.co.uk/harkon/base-ml:v1.0.1 -f infra/docker/base-ml.Dockerfile .
# Push: docker push gitea.harkon.co.uk/harkon/base-ml:v1.0.1

FROM python:3.12-slim as builder

# Install build dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    git \
    && rm -rf /var/lib/apt/lists/*

# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Copy requirements files
COPY libs/requirements-base.txt /tmp/requirements-base.txt
COPY libs/requirements-ml.txt /tmp/requirements-ml.txt

# Install all dependencies (base + ML)
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r /tmp/requirements-base.txt && \
    pip install --no-cache-dir -r /tmp/requirements-ml.txt

# Final stage - Runtime image
FROM python:3.12-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
    curl \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd -r appuser \
    && useradd -r -g appuser appuser

# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv

# Set environment variables
ENV PATH="/opt/venv/bin:$PATH" \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD python -c "import sentence_transformers; import transformers; print('ML base image healthy')"

# Default user
USER appuser

# Label
LABEL maintainer="AI Tax Agent Team" \
      description="Base ML image with sentence-transformers, PyTorch, and ML dependencies" \
      version="1.0.1"

