FROM python:3.12-slim

ARG PIP_INDEX_URL=https://pypi.org/simple

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

WORKDIR /app

RUN addgroup --system app && adduser --system --ingroup app app

COPY requirements.txt .
RUN python -m pip install --upgrade pip \
    --index-url "$PIP_INDEX_URL" \
    && python -m pip install --retries 10 --timeout 120 \
    --index-url "$PIP_INDEX_URL" \
    -r requirements.txt

COPY --chown=app:app market_api ./market_api

USER app

EXPOSE 8000

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

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