# syntax=docker/dockerfile:1
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Ein Image für Web und Worker. Läuft ohne Root und mit schreibgeschütztem
# Dateisystem; beschreibbar sind nur /data (Volume) und /tmp (tmpfs).

ARG PYTHON_VERSION=3.13
ARG DEBIAN_SUITE=trixie

# ---------------------------------------------------------------------------
# Bauen: Abhängigkeiten aus der Lock-Datei (nur mit passenden Hashes)
# ---------------------------------------------------------------------------
FROM python:${PYTHON_VERSION}-slim-${DEBIAN_SUITE} AS build

ENV PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

WORKDIR /build
COPY requirements.lock ./
RUN python -m venv /opt/venv \
 && /opt/venv/bin/pip install --require-hashes --no-deps --only-binary=:all: -r requirements.lock

COPY surveybox ./surveybox
RUN /opt/venv/bin/pybabel compile --statistics -D messages -d surveybox/locale \
 && /opt/venv/bin/python -m compileall -q surveybox

# ---------------------------------------------------------------------------
# Laufzeit
# ---------------------------------------------------------------------------
FROM python:${PYTHON_VERSION}-slim-${DEBIAN_SUITE} AS runtime

ARG DEBIAN_SUITE
# PostgreSQL-Client-Werkzeuge für die Sicherung. Mehrere Hauptversionen,
# damit zu jedem unterstützten Server ein passendes pg_dump vorhanden ist.
ARG PG_CLIENT_VERSIONS="17 18 19"

RUN set -eux; \
    apt-get update; \
    apt-get install -y --no-install-recommends ca-certificates curl gnupg; \
    install -d /usr/share/postgresql-common/pgdg; \
    curl -fsSL -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc \
        https://www.postgresql.org/media/keys/ACCC4CF8.asc; \
    echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt ${DEBIAN_SUITE}-pgdg main" \
        > /etc/apt/sources.list.d/pgdg.list; \
    apt-get update; \
    pkgs=""; for v in ${PG_CLIENT_VERSIONS}; do pkgs="$pkgs postgresql-client-$v"; done; \
    apt-get install -y --no-install-recommends $pkgs; \
    apt-get purge -y --auto-remove curl gnupg; \
    rm -rf /var/lib/apt/lists/*

RUN groupadd --system --gid 10001 surveybox \
 && useradd --system --uid 10001 --gid surveybox --home-dir /nonexistent \
        --no-create-home --shell /usr/sbin/nologin surveybox \
 && install -d -o surveybox -g surveybox -m 0750 /data /data/backups

COPY --from=build /opt/venv /opt/venv
COPY --from=build /build/surveybox /app/surveybox

ENV PATH=/opt/venv/bin:$PATH \
    PYTHONPATH=/app \
    PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    SURVEYBOX_DATA_DIR=/data

WORKDIR /app
USER surveybox:surveybox
VOLUME ["/data"]
EXPOSE 8000

HEALTHCHECK --interval=30s --timeout=10s --start-period=300s --retries=3 \
    CMD ["python", "-m", "surveybox.cli", "healthcheck"]

ENTRYPOINT ["python", "-m", "surveybox.cli"]
CMD ["web"]

LABEL org.opencontainers.image.title="Surveybox" \
      org.opencontainers.image.description="Formulare und Abstimmungen, selbst gehostet" \
      org.opencontainers.image.licenses="AGPL-3.0-or-later"
