FROM python:3.10-slim
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1

# Installiamo libgomp1, mosquitto-clients e coreutils (per stdbuf)
RUN apt-get update && apt-get install -y --no-install-recommends \
    libgomp1 \
    mosquitto-clients \
    coreutils \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copia e installazione in un unico step per ridurre i layer
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Copia del resto dell'applicazione
COPY app/ /app/app/
COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

# Setup utente e permessi
RUN useradd -m appuser && \
    mkdir -p /home/appuser/data /home/appuser/models && \
    chown -R appuser:appuser /home/appuser /app

USER appuser
ENTRYPOINT ["/app/entrypoint.sh"]
