#!/bin/bash
set -e
clear

echo "==============================================="
echo "AI Voice Bridge Installer"
echo "macOS Whisper Customer Build 3.1 - Icon + Shortcut Pack"
echo "==============================================="
echo

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PAYLOAD="$SCRIPT_DIR/payload"
DEST="$HOME/Documents/RITS-AI-Voice-Bridge"
OLD_UNBRANDED_DEST="$HOME/Documents/AI-Voice-Bridge"
PYTHON_CMD=""
CHROME_OK=0

echo "Installer folder:"
echo "$SCRIPT_DIR"
echo

echo "Checking installer payload..."
if [ ! -f "$PAYLOAD/voice-bridge-extension/manifest.json" ]; then echo "ERROR: Missing payload."; echo "You may close this window."; exit 1; fi
if [ ! -f "$PAYLOAD/ai_voice_bridge_server.py" ]; then echo "ERROR: Missing server."; echo "You may close this window."; exit 1; fi
if [ ! -f "$PAYLOAD/Start-AI-Voice-Bridge.command" ]; then echo "ERROR: Missing launcher."; echo "You may close this window."; exit 1; fi
echo "[OK] Installer payload found."
echo

echo "Checking Python 3..."
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 payload/python-3.10.11-macos11.pkg, then rerun this installer."
  echo "You may close this window."
  exit 1
fi
echo "[OK] Python 3 detected: $PYTHON_CMD"
echo

echo "Checking Google Chrome..."
if [ -d "/Applications/Google Chrome.app" ] || [ -d "$HOME/Applications/Google Chrome.app" ]; then
  CHROME_OK=1
fi
if [ "$CHROME_OK" = "1" ]; then
  echo "[OK] Google Chrome detected."
else
  echo "[MISSING] Google Chrome was not detected."
  echo "Install Google Chrome, then rerun this installer."
  echo "You may close this window."
  exit 1
fi
echo

if [ -d "$DEST" ]; then
  echo "[FOUND] Existing AI Voice Bridge installation detected. Packaged app files will be refreshed. Existing dictation model will be preserved."
else
  echo "[NEW] No existing AI Voice Bridge installation detected. New install will be created."
fi
echo

echo "Installing to:"
echo "$DEST"
echo

if [ ! -d "$DEST" ] && [ -d "$OLD_UNBRANDED_DEST" ]; then
  echo "[FOUND] Previous unbranded install detected at: $OLD_UNBRANDED_DEST"
  echo "Migrating existing files to: $DEST"
  mv "$OLD_UNBRANDED_DEST" "$DEST" 2>/dev/null || true
  if [ -d "$DEST" ]; then
    echo "[OK] Previous install migrated."
  else
    echo "[WARN] Migration could not move old folder automatically. A fresh app folder will be created."
  fi
  echo
fi

mkdir -p "$DEST"

echo "Copying/refreshing packaged AI Voice Bridge files..."
rsync -a "$PAYLOAD/" "$DEST/"
cp "$SCRIPT_DIR/README.txt" "$DEST/README.txt"
chmod +x "$DEST/Start-AI-Voice-Bridge.command"
chmod +x "$DEST/Setup-Local-Dictation-Mac.command"
if [ -d "$DEST/AI Voice Bridge.app" ]; then
  chmod +x "$DEST/AI Voice Bridge.app/Contents/MacOS/AI-Voice-Bridge-Launcher" || true
  rm -rf "$HOME/Desktop/AI Voice Bridge.app"
  cp -R "$DEST/AI Voice Bridge.app" "$HOME/Desktop/AI Voice Bridge.app"
  echo "[OK] Desktop launcher app created: $HOME/Desktop/AI Voice Bridge.app"
fi

echo
echo "==============================================="
echo "AI VOICE BRIDGE FILE REFRESH COMPLETE"
echo "==============================================="
echo
echo "App folder:"
echo "$DEST"
echo

echo "Checking local dictation dependencies..."
echo "Default backend: Whisper accuracy backend."
echo "Existing local dictation files will be reused. Missing files will be downloaded."
echo
"$DEST/Setup-Local-Dictation-Mac.command" --auto || {
  echo
  echo "WARNING: Local dictation setup did not complete."
  echo "Manual retry: $DEST/Setup-Local-Dictation-Mac.command"
}

echo
echo "Next steps:"
echo "1. Open Google Chrome."
echo "2. Go to chrome://extensions"
echo "3. Turn Developer mode ON."
echo "4. Click Load unpacked."
echo "5. Select this folder:"
echo "$DEST/voice-bridge-extension"
echo
echo "Then start the server:"
echo "$DEST/Start-AI-Voice-Bridge.command"
echo "or use the Desktop launcher app:"
echo "$HOME/Desktop/AI Voice Bridge.app"
echo
echo "App icon files were installed to:"
echo "$DEST/assets"
echo
echo "Setup finished. You may close this window."
