# syntax=docker/dockerfile:1
############################
# Builder — compile wheels
############################
FROM python:3.12-slim AS builder

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1

WORKDIR /build
COPY requirements.txt .
RUN pip install --upgrade pip && \
    pip wheel --wheel-dir /wheels -r requirements.txt

############################
# Runtime — slim final image
############################
FROM python:3.12-slim AS runtime

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PATH="/home/app/.local/bin:${PATH}"

# Non-root user
RUN useradd --create-home --uid 10001 app
WORKDIR /app

# Install deps from prebuilt wheels
COPY --from=builder /wheels /wheels
COPY requirements.txt .
RUN pip install --no-index --find-links=/wheels -r requirements.txt && \
    rm -rf /wheels

# App code
COPY --chown=app:app app ./app
COPY --chown=app:app minions ./minions
COPY --chown=app:app scripts ./scripts

# Create logs dir owned by app user
RUN mkdir -p /app/logs && chown -R app:app /app/logs

USER app
EXPOSE 8080

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