# ============================================================
# TensorFlow Model Evaluation Starter — Dockerfile
#
# Multi-stage build: slim CPU-only TensorFlow runtime image.
# GPU variant: replace the base FROM image with a TF GPU image
# (see "GPU support" section at the bottom for instructions).
# ============================================================

# --- Build stage: install Python dependencies ---
FROM python:3.11-slim AS builder

WORKDIR /build

# Install build-time system packages
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    g++ \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements first so this layer is cached when only code changes
COPY requirements.txt .

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

# --- Runtime stage ---
FROM python:3.11-slim AS runtime

LABEL maintainer="DemIA Living Lab - USAL/BISITE"
LABEL description="TensorFlow Model Evaluation Starter — DemIA Living Lab"
LABEL version="1.0.0"

WORKDIR /app

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

# Copy all application source files
COPY . .

# Create outputs directory for model checkpoints, plots, and reports
RUN mkdir -p outputs

# Default environment variables (override at runtime with -e or --env-file)
ENV CONFIG_PATH=config/config.yaml \
    LOG_LEVEL=INFO \
    RANDOM_SEED=42 \
    TF_CPP_MIN_LOG_LEVEL=2 \
    TF_DETERMINISTIC_OPS=0 \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1

# Expose the FastAPI port
EXPOSE 8000

# Default command: show available CLI commands
CMD ["python", "scripts/run.py", "--help"]

# ============================================================
# Build & run examples
# ============================================================
#
#   Build:
#     docker build -t demia-tf-eval-starter .
#
#   Train (outputs volume-mounted to persist checkpoints):
#     docker run --rm \
#         -v $(pwd)/outputs:/app/outputs \
#         demia-tf-eval-starter \
#         python scripts/run.py train
#
#   Evaluate:
#     docker run --rm \
#         -v $(pwd)/outputs:/app/outputs \
#         demia-tf-eval-starter \
#         python scripts/run.py evaluate --skip-train
#
#   Compare model variants:
#     docker run --rm \
#         -v $(pwd)/outputs:/app/outputs \
#         demia-tf-eval-starter \
#         python scripts/run.py compare
#
#   Generate HTML report:
#     docker run --rm \
#         -v $(pwd)/outputs:/app/outputs \
#         demia-tf-eval-starter \
#         python scripts/run.py report
#
#   FastAPI service:
#     docker run --rm -p 8000:8000 \
#         -v $(pwd)/outputs:/app/outputs \
#         demia-tf-eval-starter \
#         uvicorn api.main:app --host 0.0.0.0 --port 8000
#
#   Streamlit dashboard:
#     docker run --rm -p 8501:8501 \
#         -v $(pwd)/outputs:/app/outputs \
#         demia-tf-eval-starter \
#         streamlit run app/app.py --server.port 8501 --server.address 0.0.0.0
#
# ============================================================
# GPU support
# ============================================================
#
#   Replace the FROM lines with:
#     FROM tensorflow/tensorflow:2.15.0-gpu AS builder
#     FROM tensorflow/tensorflow:2.15.0-gpu AS runtime
#
#   And run with:
#     docker run --rm --gpus all \
#         -v $(pwd)/outputs:/app/outputs \
#         demia-tf-eval-starter-gpu \
#         python scripts/run.py train
# ============================================================
