# ─────────────────────────────────────────────────────────────────────────────
# Dockerfile — TensorFlow Data Pipeline Starter
# Builds a CPU-only image that serves the FastAPI inference API.
# ─────────────────────────────────────────────────────────────────────────────

# Use the official TensorFlow CPU image as the base.
# Pin to a specific tag for reproducibility; update when upgrading TF.
FROM tensorflow/tensorflow:2.15.0 AS base

LABEL org.opencontainers.image.title="DemIA TF Data Pipeline Starter" \
      org.opencontainers.image.description="Reusable tf.data pipeline template — FastAPI inference service" \
      org.opencontainers.image.source="https://github.com/bisite-usal/demia-living-lab" \
      org.opencontainers.image.licenses="MIT"

# ── System dependencies ───────────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
        curl \
    && rm -rf /var/lib/apt/lists/*

# ── Python dependencies ───────────────────────────────────────────────────────
WORKDIR /app

COPY requirements.txt .
# Install non-TF deps; TF is already present in the base image.
RUN pip install --no-cache-dir \
        numpy>=1.24.0 \
        pandas>=2.0.0 \
        pyyaml>=6.0 \
        python-dotenv>=1.0.0 \
        fastapi>=0.110.0 \
        "uvicorn[standard]>=0.29.0" \
        pydantic>=2.0.0 \
        scikit-learn>=1.4.0

# ── Application code ──────────────────────────────────────────────────────────
COPY src/       ./src/
COPY api/       ./api/
COPY config/    ./config/
COPY .env.example .env

# Create output directories so the app can write logs / checkpoints.
RUN mkdir -p outputs/models outputs/logs outputs/tensorboard

# ── Environment defaults ──────────────────────────────────────────────────────
ENV TF_CPP_MIN_LOG_LEVEL=2 \
    CUDA_VISIBLE_DEVICES=-1 \
    API_HOST=0.0.0.0 \
    API_PORT=8000 \
    LOG_LEVEL=INFO

EXPOSE 8000

# ── Health check ──────────────────────────────────────────────────────────────
HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \
    CMD curl -f http://localhost:8000/health || exit 1

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