FROM python:3.12.16-slim-trixie

ARG BUILD_REVISION=local
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONUTF8=1 \
    APP_HOST=127.0.0.1 \
    APP_PORT=8080 \
    APP_BUILD_REVISION=${BUILD_REVISION}

WORKDIR /app

RUN apt-get update \
    && apt-get upgrade -y --no-install-recommends \
    && apt-get install -y --no-install-recommends nginx \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt ./
RUN pip install --no-cache-dir --requirement requirements.txt

COPY src/ ./src/
COPY migrations/ ./migrations/
COPY infra/api-nginx.conf /etc/nginx/conf.d/default.conf
COPY infra/supervisord.conf /etc/supervisor/supervisord.conf

RUN useradd --create-home --uid 10001 appuser \
    && rm -f /etc/nginx/sites-enabled/default
USER appuser

EXPOSE 8443

HEALTHCHECK --interval=3s --timeout=2s --start-period=20s --retries=3 \
  CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2).status==200 else 1)"

CMD ["supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]
