FROM python:3.11-slim

# System dependencies for ML libraries
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc g++ libgomp1 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install Python dependencies first (cacheable layer)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Environment defaults
ENV FLASK_ENV=production \
    FLASK_DEBUG=0 \
    FLASK_PORT=8888 \
    GUNICORN_WORKERS=3 \
    SCHEDULER_ENABLED=1

EXPOSE 8888

# Health check
HEALTHCHECK --interval=30s --timeout=10s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8888/')" || exit 1

# Start with Gunicorn
CMD ["gunicorn", "-c", "gunicorn.conf.py", "app:create_app()"]
