FROM python:3.13-slim AS base

# uv package manager (copia binario ufficiale)
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

# System deps per opencv (libgl/glib), numba (libgomp)
RUN apt-get update && apt-get install -y --no-install-recommends \
    libgl1 \
    libglib2.0-0 \
    libgomp1 \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install deps da lockfile (layer cachato finché pyproject/uv.lock non cambiano)
COPY pyproject.toml uv.lock ./
COPY .python-version* ./
RUN uv sync --frozen --no-dev

# Copia sorgenti applicazione
COPY pm2d ./pm2d
COPY main.py ./

# Defaults (override via docker-compose env)
ENV IMAGES_DIR=/data/images \
    HOST=0.0.0.0 \
    PORT=8080 \
    PYTHONUNBUFFERED=1

# Cartella dati (montata come volume in compose)
RUN mkdir -p /data/images

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8080/images').read()" || exit 1

CMD ["uv", "run", "python", "main.py"]
