# =============================================================================
#  Dockerfile — DemIA Image Captioning Starter
#
#  Multi-stage build: builder installs deps; runtime image is lean.
#
#  Build:
#    docker build -t demia-image-captioning .
#
#  Run (API server, CPU):
#    docker run -p 8000:8000 \
#      -e HF_MODEL_ID=Salesforce/blip-image-captioning-base \
#      demia-image-captioning api
#
#  Run (Gradio UI):
#    docker run -p 7860:7860 demia-image-captioning app
#
#  Run (caption one image via volume mount):
#    docker run --rm \
#      -v /host/images:/app/data/images \
#      demia-image-captioning \
#      caption --image data/images/photo.jpg
#
#  Persist model cache between runs (avoids re-downloading weights):
#    docker run -p 8000:8000 \
#      -v hf_cache:/root/.cache/huggingface \
#      demia-image-captioning api
# =============================================================================

# ---------------------------------------------------------------------------
# Stage 1 — builder: install Python deps into a prefix
# ---------------------------------------------------------------------------
FROM python:3.11-slim AS builder

WORKDIR /build

# System deps needed to compile wheels (pillow, tokenizers, etc.).
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    git \
    && rm -rf /var/lib/apt/lists/*

# Copy dependency specs first to leverage Docker layer caching.
COPY requirements.txt pyproject.toml ./

# Install CPU-only PyTorch (smaller image; swap the index URL for CUDA).
RUN pip install --upgrade pip && \
    pip install torch torchvision \
        --index-url https://download.pytorch.org/whl/cpu \
        --no-cache-dir && \
    pip install -r requirements.txt \
        --no-cache-dir

# ---------------------------------------------------------------------------
# Stage 2 — runtime: copy only what is needed
# ---------------------------------------------------------------------------
FROM python:3.11-slim AS runtime

LABEL maintainer="DemIA Living Lab — Universidad de Salamanca (USAL) / BISITE"
LABEL description="Hugging Face Image Captioning Starter"
LABEL version="0.1.0"

WORKDIR /app

# Copy installed packages from the builder stage.
COPY --from=builder /usr/local/lib/python3.11 /usr/local/lib/python3.11
COPY --from=builder /usr/local/bin /usr/local/bin

# Copy application source.
COPY src/       ./src/
COPY api/       ./api/
COPY app/       ./app/
COPY scripts/   ./scripts/
COPY examples/  ./examples/
COPY config/    ./config/
COPY .env.example ./

# Create runtime directories.
RUN mkdir -p data/images outputs

# Pre-create a non-root user for security.
RUN useradd --no-create-home --shell /bin/false appuser && \
    chown -R appuser:appuser /app
USER appuser

# Expose both the API and Gradio ports.
EXPOSE 8000 7860

# Default environment variables (override with -e at runtime).
ENV LOG_LEVEL=INFO \
    HF_MODEL_ID=Salesforce/blip-image-captioning-base \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

# Default command: start the FastAPI server.
# Override with e.g.:  docker run ... app
ENTRYPOINT ["python", "scripts/run.py"]
CMD ["api", "--host", "0.0.0.0", "--port", "8000"]
