FROM python:3.12-slim

ARG UID=1000
ARG GID=1000

# HOME=/tmp: streamrip legt beim Import ~/.config an. Mit user:-Overrides
# (UID ohne passwd-Eintrag) würde das auf '/.config' fallen und scheitern.
# Die echte Config liegt ohnehin in STREAMRIP_CONFIG.
ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    STREAMRIP_CONFIG=/config/config.toml \
    STREAMRIP_DOWNLOAD_DIR=/downloads \
    BACKEND_PORT=8000 \
    HOME=/tmp

RUN apt-get update && apt-get install -y --no-install-recommends ffmpeg git \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd -g "$GID" appuser \
    && useradd -u "$UID" -g "$GID" -m -s /bin/false appuser

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt && pip install --no-cache-dir "git+https://github.com/nathom/streamrip.git@v2.2.0"

COPY app ./app
RUN chown -R appuser:appuser /app

ARG VERSION=dev
ENV APP_VERSION=$VERSION

ARG GIT_SHA=dev
ENV APP_COMMIT=$GIT_SHA

USER appuser

EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
    CMD python -c "import os, urllib.request; urllib.request.urlopen(f'http://localhost:{os.environ.get(\"BACKEND_PORT\", \"8000\")}/api/health')"
CMD uvicorn app.main:app --host 0.0.0.0 --port ${BACKEND_PORT:-8000}
