#!/bin/bash
set -e

AUTO=0
if [ "$1" = "--auto" ]; then AUTO=1; fi

TARGET="$HOME/Documents/RITS-AI-Voice-Bridge"
MODEL_NAME="base.en"
MODEL_ROOT="$TARGET/whisper-models"
VENV_DIR="$TARGET/.venv"
VENV_PYTHON="$VENV_DIR/bin/python"
PYTHON_CMD=""

echo "==============================================="
echo "AI Voice Bridge Dictation Setup"
echo "macOS / Whisper accuracy backend"
echo "==============================================="
echo
echo "Target folder: $TARGET"
echo

if [ ! -f "$TARGET/ai_voice_bridge_server.py" ]; then
  echo "ERROR: Main AI Voice Bridge app is not installed yet."
  echo "Run Run-AI-Voice-Bridge-Mac-Installer.command first."
  if [ "$AUTO" = "0" ]; then
    echo "Setup complete. You may close this window."
  fi
  exit 1
fi

if command -v python3 >/dev/null 2>&1; then
  PYTHON_CMD="python3"
elif command -v python >/dev/null 2>&1 && python -c 'import sys; sys.exit(0 if sys.version_info[0] == 3 else 1)' >/dev/null 2>&1; then
  PYTHON_CMD="python"
fi

if [ -z "$PYTHON_CMD" ]; then
  echo "[MISSING] Python 3 was not detected."
  echo "Run the included python-3.10.11-macos11.pkg from this folder, then rerun setup."
  if [ "$AUTO" = "0" ]; then
    echo "Setup complete. You may close this window."
  fi
  exit 1
fi

echo "[OK] Python 3 detected: $PYTHON_CMD"
echo

echo "==> Checking local Python environment"
if [ ! -x "$VENV_PYTHON" ]; then
  echo "[NEW] Creating local Python virtual environment inside the app folder."
  "$PYTHON_CMD" -m venv "$VENV_DIR"
fi

if [ ! -x "$VENV_PYTHON" ]; then
  echo "ERROR: Could not create local Python environment."
  if [ "$AUTO" = "0" ]; then
    echo "Setup complete. You may close this window."
  fi
  exit 1
fi

echo "[OK] Local Python environment: $VENV_DIR"
echo

echo "==> Checking Whisper Python package"
if "$VENV_PYTHON" -c 'import faster_whisper' >/dev/null 2>&1; then
  echo "[OK] faster-whisper already installed in local environment."
else
  echo "[WARN] faster-whisper not found. Installing into local environment."
  "$VENV_PYTHON" -m pip install --upgrade pip
  "$VENV_PYTHON" -m pip install --upgrade faster-whisper
  "$VENV_PYTHON" -c 'import faster_whisper' >/dev/null 2>&1 || {
    echo "ERROR: faster-whisper installed, but local Python still cannot import it."
    if [ "$AUTO" = "0" ]; then
      echo "Setup complete. You may close this window."
    fi
    exit 1
  }
  echo "[OK] faster-whisper installed in local environment."
fi

echo

echo "==> Checking Whisper model"
echo "Model: $MODEL_NAME"
echo "Model cache folder: $MODEL_ROOT"
echo "This may download the model the first time. After setup, dictation runs locally."
mkdir -p "$MODEL_ROOT"
if MODEL_ROOT_ENV="$MODEL_ROOT" "$VENV_PYTHON" - <<'RITS_WHISPER_PY'
import os
from faster_whisper import WhisperModel
WhisperModel("base.en", device="cpu", compute_type="int8", download_root=os.environ["MODEL_ROOT_ENV"])
RITS_WHISPER_PY
then
  echo "[OK] Whisper model ready."
else
  echo "[WARN] Whisper model pre-cache did not complete."
  echo "The bridge is still installed. First dictation can retry the model download."
fi
echo

echo "==============================================="
echo "LOCAL DICTATION SETUP COMPLETE"
echo "==============================================="
echo "Backend: Whisper"
echo "Model: $MODEL_NAME"
echo "Python environment: $VENV_DIR"
echo
echo "Next step: restart Start-AI-Voice-Bridge.command"
echo "The server should say: Local dictation: available (Whisper accuracy backend)"
echo

if [ "$AUTO" = "0" ]; then
  echo "Setup complete. You may close this window."
fi
exit 0
