# Harmonious — self-hosted server image.
FROM python:3.12-slim-bookworm

# Runtime system libraries. ffmpeg decodes audio for transcription, libsndfile1
# backs soundfile, libgomp1 provides OpenMP for numba/scipy, ca-certificates lets
# faster-whisper fetch the model over HTTPS.
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        ca-certificates \
        ffmpeg \
        libsndfile1 \
        libgomp1 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install Python deps first so this layer is cached across code changes.
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Application code (routers, services and static UI live under app/).
COPY app ./app

# Run unprivileged, with all mutable state in /data.
RUN groupadd --gid 10001 harmonious \
    && useradd --uid 10001 --gid 10001 --no-create-home --home-dir /app harmonious \
    && mkdir -p /data \
    && chown 10001:10001 /data

# HOME=/data so numba / faster-whisper cache writes land on the writable volume.
ENV HARMONIOUS_DATA_DIR=/data \
    PYTHONUNBUFFERED=1 \
    HOME=/data

USER 10001

EXPOSE 8000

# curl is not installed; use Python's stdlib urllib. First boot warms compiled
# kernels and may download the Whisper model, so give it a long start_period.
HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/api/health', timeout=5)"

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers", "--forwarded-allow-ips", "*"]
