# ============================================================
# Single stage: Python runtime + Node.js
# ============================================================
# CUDA 12.4 + cuDNN 9 runtime — provides GPU support for:
#   • PyTorch (cu124 wheels bundle their own cuBLAS/cuDNN)
#   • onnxruntime-gpu (requires system libcudnn; uses CUDAExecutionProvider for
#     MDX-Net separation and basic-pitch ONNX inference)
#   • libcudnn8 is also installed alongside cuDNN 9 because WhisperX's phoneme
#     alignment model (wav2vec2 via pyannote / CTranslate2) links against the
#     cuDNN 8 ABI (.so.8 filenames) which cuDNN 9 no longer ships.
#
# Node.js is also installed here so that start.sh can run
# `vite build --watch`, keeping the frontend live without a Docker rebuild.
# node_modules are installed once at first container start and persisted in
# the stemengine-node-modules named volume.
FROM nvidia/cuda:12.4.1-cudnn-runtime-ubuntu22.04

ENV DEBIAN_FRONTEND=noninteractive

# ---------- System dependencies ----------
# Add deadsnakes PPA for Python 3.11 (not available in default Ubuntu 22.04 repos).
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    curl \
    gnupg \
    && mkdir -p /etc/apt/keyrings \
    && curl -fsSL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xF23C5A6CF475977595C89F51BA6932366A755776" \
       | gpg --dearmor -o /etc/apt/keyrings/deadsnakes.gpg \
    && echo "deb [signed-by=/etc/apt/keyrings/deadsnakes.gpg] https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy main" \
       > /etc/apt/sources.list.d/deadsnakes.list \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
    ffmpeg \
    abcmidi \
    libcudnn8 \
    python3.11 \
    python3.11-dev \
    python3.11-venv \
    build-essential \
    git \
    libsndfile1 \
    libsndfile1-dev \
    espeak-ng \
    libespeak-ng-dev \
    libmecab-dev \
    && rm -rf /var/lib/apt/lists/*

# Bootstrap pip for Python 3.11, then install uv (faster resolver, no depth limit)
RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 \
    && python3.11 -m pip install --no-cache-dir uv

# ---------- Node.js 20.x ----------
# Required for `vite build --watch` which runs at container startup via start.sh.
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get install -y --no-install-recommends nodejs \
    && rm -rf /var/lib/apt/lists/*

# ---------- Main Python 3.11 dependencies ----------
WORKDIR /app

COPY backend/requirements.txt /app/backend/requirements.txt
RUN python3.11 -m uv pip install --system --no-cache -r /app/backend/requirements.txt

# Install CUDA-enabled PyTorch (CUDA 12.4 wheels; self-contained, does not need system CUDA 12.4)
# torch 2.6+ is required — earlier versions have a serious torch.load vulnerability (even with
# weights_only=True).  cu121 wheels were discontinued after torch 2.4; use cu124 going forward.
RUN python3.11 -m uv pip install --system --no-cache \
    torch==2.6.0 torchaudio==2.6.0 torchvision==0.21.0 \
    --index-url https://download.pytorch.org/whl/cu124

# Install basic-pitch without its tensorflow dependency.
# basic-pitch can run via onnxruntime (already installed above); installing with
# --no-deps skips the tensorflow pull that has no cp311 wheels and conflicts with
# numpy>=2.1.0 required by whisperx.
RUN python3.11 -m uv pip install --system --no-cache --no-deps basic-pitch

# Note: Model weights are downloaded on-demand at runtime on first use.
# This keeps the Docker image smaller and avoids build-time network dependencies.

# ---------- Application source ----------
COPY backend/ /app/backend/
COPY lyrics-sync/ /app/lyrics-sync/

# Frontend source is bind-mounted at runtime (${SEFOLDER}/frontend:/app/frontend).
# Copy it here so the image has a fallback if the bind-mount is absent,
# and so COPY frontend/package*.json is available for the layer cache.
COPY frontend/ /app/frontend/

# ---------- Startup script ----------
COPY start.sh /app/start.sh
RUN chmod +x /app/start.sh

# ---------- Runtime ----------
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV MEDIA_DIR=/app/media
ENV CPU_SEPARATION=true
ENV BS_ROFORMER_MODEL_DIR=/opt/bs_roformer_models
ENV MEL_BAND_MODEL_DIR=/opt/mel_band_models
ENV MDXNET_MODEL_DIR=/opt/mdxnet_models
ENV SCNET_MODEL_DIR=/opt/scnet_models
ENV MEDLEYVOX_MODEL_DIR=/opt/medleyvox_models
ENV APOLLO_MODEL_DIR=/opt/apollo_models
ENV ASPIRATION_MODEL_DIR=/opt/aspiration_models
ENV BANDIT_MODEL_DIR=/opt/bandit_models

EXPOSE 8000

CMD ["/app/start.sh"]
