# ---------------------------------------------------------------------------
# TensorFlow LSTM Starter — Dockerfile
# Builds a container that runs the FastAPI inference service.
# ---------------------------------------------------------------------------

# Use the official TensorFlow CPU image to keep image size manageable.
# Swap for tensorflow/tensorflow:2.15.0-gpu if you have an NVIDIA runtime.
FROM tensorflow/tensorflow:2.15.0 AS base

LABEL maintainer="DemIA Living Lab - Universidad de Salamanca (USAL) / BISITE"
LABEL description="TensorFlow LSTM Starter API service"

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

# ── Working directory ──────────────────────────────────────────────────────
WORKDIR /app

# ── Python dependencies ────────────────────────────────────────────────────
# Copy requirements first to leverage Docker layer caching.
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip \
    && pip install --no-cache-dir -r requirements.txt

# ── Application source ─────────────────────────────────────────────────────
COPY config/ config/
COPY src/ src/
COPY api/ api/
COPY scripts/ scripts/
COPY .env.example .env.example

# ── Create directories that the app writes to at runtime ──────────────────
RUN mkdir -p models logs

# ── Environment defaults (override via docker run -e or a .env file) ──────
ENV LOG_LEVEL=INFO
ENV API_HOST=0.0.0.0
ENV API_PORT=8000

# ── Expose the API port ────────────────────────────────────────────────────
EXPOSE 8000

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

# ── Default command: start the FastAPI server ─────────────────────────────
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000"]
