FROM python:3.12-slim
WORKDIR /app

# ── Dependencies ────────────────────────────────
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# ── Application code + migrations + tests + templates ──
COPY src/ .
COPY tests/ tests/

# Make entrypoint executable
RUN chmod +x entrypoint.sh

# ── Healthcheck (Docker-native) ─────────────────
HEALTHCHECK --interval=15s --timeout=3s --retries=3 \
    CMD python -c "import httpx,os; r=httpx.get(f'http://localhost:{os.getenv(\"HEALTH_PORT\",\"8765\")}/health'); r.raise_for_status()" || exit 1

# ── Entrypoint: migrate then run ────────────────
ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["python", "main.py"]
