ARG PYTHON_BASE_IMAGE=python:3.13-alpine
FROM ${PYTHON_BASE_IMAGE} AS base

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

WORKDIR /app

RUN if command -v apt-get >/dev/null 2>&1; then \
      apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/*; \
    elif command -v apk >/dev/null 2>&1; then \
      apk upgrade --no-cache; \
    fi

RUN if addgroup --help 2>&1 | grep -q -- '--system'; then \
      addgroup --system app && adduser --system --ingroup app app; \
    else \
      addgroup -S app && adduser -S -G app app; \
    fi

COPY requirements.txt /app/requirements.txt
RUN pip install --upgrade pip && pip install -r /app/requirements.txt

COPY . /app

RUN chmod +x /app/entrypoint.sh

USER app

EXPOSE 8000

HEALTHCHECK --interval=30s --timeout=5s --retries=5 CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/api/v1/healthz')" || exit 1

CMD ["/app/entrypoint.sh"]
