#!/usr/bin/env bash
# Voice-Sidecar-Setup AUF DER BOX (User hitonabi, sudo-frei). Idempotent — Erstinstallation
# + Updates. Eigenes Python-3.12-venv (~/.voice/venv), weil torch/chatterbox/faster-whisper
# nicht ins 3.14-Backend-venv passen (analog ~/.mem0/venv). Wird von deploy/deploy.sh aufgerufen.
set -euo pipefail

SRC="${MC2_SRC:-$HOME/mission-control-v2}"
VENV="$HOME/.voice/venv"
VOICES="$HOME/.voice/voices"
UV="$(command -v uv || echo "$HOME/.local/bin/uv")"

# torch/chatterbox brauchen Python ≤3.13 — das Default-python3 der Box ist 3.14. Darum das venv
# bevorzugt per uv mit gemanagtem Python 3.12 anlegen (wie das mem0-venv). pip läuft via `uv pip`.
pipi() {
  if [ -x "$UV" ]; then "$UV" pip install --python "$VENV/bin/python" "$@"
  else "$VENV/bin/python" -m pip install "$@"; fi
}

# --- venv (Python 3.12) -----------------------------------------------------
if [ ! -x "$VENV/bin/python" ]; then
  echo "[voice] Erstelle venv (Python 3.12) → $VENV"
  if [ -x "$UV" ]; then "$UV" venv --python 3.12 "$VENV"
  elif command -v python3.12 >/dev/null 2>&1; then python3.12 -m venv "$VENV"
  else echo "[voice] FATAL: weder uv noch python3.12 vorhanden."; exit 1; fi
fi
echo "[voice] venv-Python: $("$VENV/bin/python" --version 2>&1)"

# --- Kern-Deps (STT + Piper + Server) — required ----------------------------
echo "[voice] Installiere Kern-Abhängigkeiten …"
pipi -q -r "$SRC/voice_service/requirements.txt"

# --- Chatterbox (Premium-TTS) — best-effort, CPU-torch (kein 2-GB-CUDA-Wheel) ----
echo "[voice] Installiere Chatterbox (Premium-TTS, CPU-torch) — best-effort …"
if pipi -q --index-url https://download.pytorch.org/whl/cpu torch torchaudio \
   && pipi -q chatterbox-tts; then
  echo "[voice] Chatterbox bereit."
else
  echo "[voice] WARN: Chatterbox-Install fehlgeschlagen — Piper bleibt als Default-Stimme aktiv."
fi

# --- Piper-Binary (offiziell, bringt espeak-ng mit) → ~/.voice/piper -----------
if [ ! -x "$HOME/.voice/piper/piper" ]; then
  echo "[voice] Lade Piper-Binary …"
  curl -fsSL "https://github.com/rhasspy/piper/releases/download/2023.11.14-2/piper_linux_x86_64.tar.gz" \
       -o /tmp/piper.tgz \
    && tar xzf /tmp/piper.tgz -C "$HOME/.voice" \
    && rm -f /tmp/piper.tgz \
    && echo "[voice] Piper-Binary: $("$HOME/.voice/piper/piper" --version 2>&1 | head -1 || echo installiert)" \
    || echo "[voice] WARN: Piper-Binary-Download fehlgeschlagen."
fi

# --- Piper-Stimmen (Deutsch) nach ~/.voice/voices ---------------------------
mkdir -p "$VOICES"
BASE="https://huggingface.co/rhasspy/piper-voices/resolve/main/de/de_DE"
get_voice() {  # $1=relpfad-ohne-endung  $2=dateibasis
  local rel="$1" name="$2"
  for ext in onnx onnx.json; do
    if [ ! -f "$VOICES/$name.$ext" ]; then
      echo "[voice] Lade Piper-Stimme $name.$ext …"
      curl -fsSL "$BASE/$rel/$name.$ext" -o "$VOICES/$name.$ext" \
        || echo "[voice] WARN: $name.$ext konnte nicht geladen werden."
    fi
  done
}
get_voice "thorsten/medium" "de_DE-thorsten-medium"   # Default (schnell, klar)
get_voice "thorsten/high"   "de_DE-thorsten-high"      # natürlicher, etwas langsamer
get_voice "kerstin/low"     "de_DE-kerstin-low"        # weibliche Alternative

echo "[voice] Fertig. Stimmen in $VOICES:"
ls -1 "$VOICES"/*.onnx 2>/dev/null || echo "[voice] WARN: keine Piper-Stimme vorhanden!"
