# ---- Base image ----
FROM python:3.12-slim

# ---- Metadata ----
LABEL maintainer="Mohamed Adel"
LABEL description="Simple Flask API with /health endpoint, deployed on Ghaymah Containers"

# ---- Working directory ----
WORKDIR /app

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

# ---- Copy application code ----
COPY app.py .

# ---- Create non-root user for security ----
RUN useradd -m appuser
USER appuser

# ---- Expose the app port ----
EXPOSE 8080

# ---- Container-level healthcheck (used by Ghaymah / Docker runtime) ----
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8080/health')" || exit 1

# ---- Run the app with a production WSGI server ----
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "--workers", "2", "app:app"]
