# =============================================================================
#  Dockerfile — DemIA Summarisation Starter
#
#  Builds a lightweight container that serves the FastAPI summarisation API.
#  The model is downloaded at first startup (or restored from a mounted cache).
#
#  Build:
#    docker build -t demia-summarisation-starter .
#
#  Run (CPU, model downloaded on first start):
#    docker run -p 8000:8000 \
#      -e HF_MODEL_ID="sshleifer/distilbart-cnn-12-6" \
#      demia-summarisation-starter
#
#  Run (with a pre-downloaded model cache to avoid repeated downloads):
#    docker run -p 8000:8000 \
#      -e HF_MODEL_ID="sshleifer/distilbart-cnn-12-6" \
#      -e HF_CACHE_DIR="/app/hf_cache" \
#      -v $(pwd)/.hf_cache:/app/hf_cache \
#      demia-summarisation-starter
#
#  Run (fine-tuned local model):
#    docker run -p 8000:8000 \
#      -e HF_MODEL_ID="/app/outputs/finetuned" \
#      -v $(pwd)/outputs:/app/outputs \
#      demia-summarisation-starter
# =============================================================================

FROM python:3.11-slim

# Metadata
LABEL maintainer="DemIA Living Lab - USAL / BISITE <bisite@usal.es>"
LABEL description="Hugging Face seq2seq summarisation API"

# ── System dependencies ───────────────────────────────────────────────────────
# git is needed by some HF hub utilities; libgomp1 for OpenMP (used by PyTorch CPU).
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    libgomp1 \
  && rm -rf /var/lib/apt/lists/*

# ── Create a non-root user ────────────────────────────────────────────────────
RUN useradd --create-home --shell /bin/bash appuser
WORKDIR /app

# ── Install Python dependencies ───────────────────────────────────────────────
# Copy only the requirements file first to leverage Docker layer caching.
COPY requirements.txt ./
RUN pip install --no-cache-dir --upgrade pip \
 && pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu \
 && pip install --no-cache-dir -r requirements.txt

# ── Copy application source ───────────────────────────────────────────────────
COPY config/ ./config/
COPY src/     ./src/
COPY api/     ./api/
COPY app/     ./app/
COPY scripts/ ./scripts/

# Ensure the outputs directory exists and is writable.
RUN mkdir -p outputs && chown -R appuser:appuser /app

# ── Runtime configuration ─────────────────────────────────────────────────────
USER appuser

# Respect HF_CACHE_DIR if provided; default to the standard cache location.
ENV HF_HOME="/app/.cache/huggingface"
ENV HF_MODEL_ID="sshleifer/distilbart-cnn-12-6"
ENV LOG_LEVEL="INFO"
ENV API_HOST="0.0.0.0"
ENV API_PORT="8000"

EXPOSE 8000

# ── Health check ─────────────────────────────────────────────────────────────
# The /health endpoint is available once the model has loaded (~30-60 s on cold start).
HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"

# ── Entrypoint ────────────────────────────────────────────────────────────────
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000", "--log-level", "info"]
