# Thermograph backend: FastAPI API + accounts/notifications + the SSR content
# JSON API frontend consumes. Split from the monorepo (repo-split Stage 7).
FROM python:3.12-slim

# curl is only for the container HEALTHCHECK below. Everything Python needs
# ships as manylinux wheels (asyncpg, psycopg[binary], polars, numpy,
# cryptography via pywebpush, PyNaCl), so no compiler/build toolchain is
# required.
RUN apt-get update \
    && apt-get install -y --no-install-recommends curl \
    && rm -rf /var/lib/apt/lists/*

# Install deps first so this layer caches across code-only changes.
COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

COPY . /app/
RUN chmod +x /app/deploy/entrypoint.sh

# Non-root runtime user. Create the writable state dirs and own the whole tree
# so the parquet cache, logs, notifier.lock, homepage.json, vapid.json can be
# written. When the named volumes first mount over /app/data and /app/logs,
# Docker seeds them from this image content -- including this ownership --
# so they stay writable.
RUN useradd --system --create-home --uid 10001 thermograph \
    && mkdir -p /app/data /app/logs \
    && chown -R thermograph:thermograph /app

USER thermograph
WORKDIR /app

ENV PORT=8137 \
    WORKERS=4 \
    THERMOGRAPH_BASE=/ \
    PYTHONUNBUFFERED=1

EXPOSE 8137

HEALTHCHECK --interval=30s --timeout=5s --start-period=40s --retries=3 \
    CMD curl -fsS http://127.0.0.1:${PORT}/healthz || exit 1

ENTRYPOINT ["/app/deploy/entrypoint.sh"]
