# ── Stage 1: builder ─────────────────────────────────────────────────────────
FROM python:3.12-slim AS builder

WORKDIR /build

COPY requirements.txt .

# Kompajliraj dependencies u wheel fajlove
RUN pip install --upgrade pip && \
    pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt

# ── Stage 2: production ───────────────────────────────────────────────────────
FROM python:3.12-slim

# Ne koristiti root
RUN adduser --disabled-password --gecos "" appuser

WORKDIR /app

# Kopiraj samo wheel fajlove iz buildera
COPY --from=builder /wheels /wheels
RUN pip install --no-cache-dir --no-index --find-links=/wheels /wheels/* && \
    rm -rf /wheels

# Kopiraj aplikaciju
COPY app/ ./app/

# Prebaci na non-root korisnika
USER appuser

EXPOSE 8000

HEALTHCHECK --interval=15s --timeout=5s --start-period=30s --retries=3 \
  CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
