FROM python:3.12.11-slim-bookworm

ARG BUILD_REVISION=local
LABEL org.opencontainers.image.revision=$BUILD_REVISION

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    APP_HOST=0.0.0.0 \
    APP_PORT=8080

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py auth.py db.py sessions.py s3store.py worker.py ./
COPY migrations ./migrations
COPY scripts/migrate.py scripts/reconcile_attachments.py scripts/replay_attachment.py ./scripts/
COPY contracts/openapi.json ./contracts/openapi.json
RUN useradd --uid 10001 --create-home --shell /usr/sbin/nologin appuser \
    && chown -R 10001:10001 /app
USER 10001:10001

EXPOSE 8080
HEALTHCHECK --interval=10s --timeout=3s --start-period=10s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)" || exit 1

STOPSIGNAL SIGTERM
CMD ["python", "app.py"]
