# ============================================================
# DemIA — Hugging Face Translation Starter
# Multi-stage build: keeps the final image lean.
# ============================================================

# ---- Stage 1: build dependencies ----
FROM python:3.11-slim AS builder

WORKDIR /app

# Install build tools needed by some native packages (sentencepiece, etc.)
RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        pkg-config \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt


# ---- Stage 2: runtime image ----
FROM python:3.11-slim AS runtime

LABEL maintainer="DemIA Living Lab - USAL / BISITE <bisite@usal.es>"
LABEL description="MarianMT machine-translation API service"

# Non-root user for security
RUN useradd --create-home --shell /bin/bash demia
WORKDIR /home/demia/app

# Copy installed packages from builder
COPY --from=builder /install /usr/local

# Copy application source
COPY config/ config/
COPY src/    src/
COPY api/    api/

# Model cache lives in a Docker volume so weights survive container restarts
VOLUME ["/home/demia/app/.model_cache"]

# Environment defaults (can be overridden at `docker run`)
ENV HF_MODEL_ID=Helsinki-NLP/opus-mt-es-en \
    HF_CACHE_DIR=/home/demia/app/.model_cache \
    TRANSLATION_DEVICE=auto \
    API_HOST=0.0.0.0 \
    API_PORT=8000 \
    LOG_LEVEL=INFO \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

USER demia

EXPOSE 8000

# Warm-up: download and cache the model at container build time (optional).
# Comment out the next two lines if you prefer lazy loading on first request.
# RUN python -c "from src.model import load_model_and_tokenizer; \
#                from src.utils import load_config; \
#                cfg = load_config(); load_model_and_tokenizer(cfg)"

CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000"]
