# 2FA-Server image (Pflegesyndikat)
#
# WHY MULTI-STAGE:
#   The 2FA binary dynamically links against the system OpenSSL (reqwest →
#   native-tls → openssl). If it is built on the host (e.g. Fedora 45 with
#   OpenSSL 4), it ends up needing `libssl.so.4` — which does NOT exist on
#   the Debian 13 (trixie) vserver (OpenSSL 3). To guarantee the binary runs
#   in exactly this image, we COMPILE it here, inside a Debian 13 builder
#   stage. The result links against `libssl.so.3`, which the slim runtime
#   (libssl3t64) provides.
#
#   Source lives in the repo at ../../2FA-Server-furilabs/. We COPY it into
#   the builder (context = ~/docker/caddy/2fa, so we must pass the source via
#   the build context — see the build note in docker-compose.yml). The build
#   is fully reproducible from the repo; no host toolchain or host OpenSSL is
#   involved.
#
# Runtime:
#   - Runs as a non-root user (UID 1000) so the bind-mounted SQLite dir
#     (/var/lib/pflegesyndikat-2fa) can be host-owned by UID 1000.
#   - Binds 0.0.0.0:8080 inside the container; Caddy reverse-proxies
#     https://pflegesyndikat.de/api/v1/* → this container.
#   - Image is tagged ps-2fa-server:0.1.0 (pinned, local — watchtower cannot
#     pull a floating tag for it).

# ── Stage 1: builder (Debian 13, same glibc + OpenSSL 3 as the runtime) ──
FROM debian:stable AS builder
# build-essential: cc for linking. libssl-dev: OpenSSL 3 headers (so the
# binary links libssl.so.3). pkg-config: lets openssl-sys find the system SSL.
# The Rust toolchain comes from rustup (NOT the distro's cargo/rustc, which on
# Debian 13 is 1.85.1 — too old for the locked deps that need rustc >= 1.88).
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
       build-essential \
       pkg-config \
       libssl-dev \
       ca-certificates \
       curl \
    && rm -rf /var/lib/apt/lists/* \
    && curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain stable \
    && rm -rf /root/.cargo/registry /root/.rustup/toolchains/*/share/doc
ENV PATH="/root/.cargo/bin:${PATH}"

WORKDIR /src
COPY 2fa-src/ ./
RUN CARGO_NET_GIT_FETCH_WITH_CLI=true cargo build --release \
    && strip target/release/pflegesyndikat-2fa-server

# ── Stage 2: slim runtime ──
FROM debian:stable-slim
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
       ca-certificates \
       libssl3t64 \
    && rm -rf /var/lib/apt/lists/*

# Non-root runtime user (UID 1000 to match host ownership of the bind dir).
RUN groupadd -g 1000 ps2fa && useradd -u 1000 -g 1000 -m -d /home/ps2fa ps2fa

WORKDIR /app
COPY --from=builder /src/target/release/pflegesyndikat-2fa-server /app/pflegesyndikat-2fa-server
RUN chown ps2fa:ps2fa /app/pflegesyndikat-2fa-server \
    && mkdir -p /var/lib/pflegesyndikat-2fa \
    && chown -R ps2fa:ps2fa /var/lib/pflegesyndikat-2fa

USER ps2fa
ENV PFLEGE_2FA_BIND_ADDR=0.0.0.0:8080 \
    PFLEGE_2FA_DB_PATH=/var/lib/pflegesyndikat-2fa/2fa.db \
    PFLEGE_2FA_ISSUER=Pflegesyndikat \
    RUST_LOG=info

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
    CMD ["/bin/sh", "-c", "exec 3<>/dev/tcp/127.0.0.1/8080 && printf 'GET /api/v1/health HTTP/1.0\\r\\n\\r\\n' >&3 && grep -q '\"status\"' <&3"]

CMD ["/app/pflegesyndikat-2fa-server"]
