FROM python:3.11-slim

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Install build deps needed for some packages (cryptography, etc.) then remove them
RUN apt-get update && \
    apt-get install -y --no-install-recommends build-essential libssl-dev libffi-dev curl ca-certificates && \
    rm -rf /var/lib/apt/lists/*

COPY requirements.txt ./

RUN pip install --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Copy application
COPY . /app

ENV FLASK_APP=app:create_app
EXPOSE 5000

# Healthcheck: call the Flask /health endpoint
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
    CMD curl -fS http://127.0.0.1:5000/health || exit 1

# Use gunicorn and call the factory to create the app
CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:create_app()"]
