# mc is copied from the pinned MinIO image already used by the stand (static binary).
FROM minio/minio:RELEASE.2025-09-07T16-13-09Z AS mc

FROM python:3.12-alpine@sha256:d09d15e60962ca365d1cd544a48773bac9d33f2fb1b00f2aa0deec78ade7dc31

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

COPY --from=mc /usr/bin/mc /usr/local/bin/mc

RUN adduser -D -u 10001 -h /home/app app \
    && mkdir -p /tmp/uploads /tmp/mc \
    && chown app:app /tmp/uploads /tmp/mc

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY app ./app
COPY infra/migrate.sql ./infra/migrate.sql
COPY infra/bootstrap-s3.sh ./infra/bootstrap-s3.sh

RUN chmod 755 /usr/local/bin/mc /app/infra/bootstrap-s3.sh \
    && chown -R app:app /app

USER app
EXPOSE 8080

HEALTHCHECK --interval=5s --timeout=4s --start-period=25s --retries=20 \
    CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=3)"]

CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-graceful-shutdown", "2"]
