#!/bin/bash
# LOTS ActivityWatch Bridge — Installer für macOS
# Doppelklick zum Installieren

set -euo pipefail

echo ""
echo "╔══════════════════════════════════════════════════╗"
echo "║   LOTS ActivityWatch Bridge — Installation       ║"
echo "╚══════════════════════════════════════════════════╝"
echo ""

# ── Homebrew prüfen / installieren ──────────────────────────────
if ! command -v brew &>/dev/null; then
  echo "→  Homebrew nicht gefunden — wird jetzt installiert …"
  echo "   (Administratorpasswort wird abgefragt)"
  echo ""
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

  # PATH für diese Session setzen (Apple Silicon + Intel)
  if [ -f /opt/homebrew/bin/brew ]; then
    eval "$(/opt/homebrew/bin/brew shellenv)"
  elif [ -f /usr/local/bin/brew ]; then
    eval "$(/usr/local/bin/brew shellenv)"
  fi
fi

if ! command -v brew &>/dev/null; then
  echo "✗  Homebrew konnte nicht installiert werden."
  echo "   Bitte manuell installieren: https://brew.sh"
  echo ""
  open "https://brew.sh"
  read -rp "Enter zum Schließen …"
  exit 1
fi

echo "✓  Homebrew $(brew --version | head -1) gefunden"
echo ""

# ── Node.js prüfen / installieren ───────────────────────────────
if ! command -v node &>/dev/null; then
  echo "→  Node.js nicht gefunden — installiere über Homebrew …"
  brew install node
fi

NODE_VER=$(node --version)
echo "✓  Node.js $NODE_VER gefunden"
echo ""

# ── ActivityWatch prüfen / installieren / starten ───────────────
echo "→  Prüfe ActivityWatch …"

if curl -sf http://localhost:5600/api/0/info > /dev/null 2>&1; then
  echo "✓  ActivityWatch läuft"
else
  echo ""
  AW_PROC=$(pgrep -x "aw-server" 2>/dev/null || pgrep -x "aw-server-rust" 2>/dev/null || true)
  AW_APP=$(ls /Applications/ 2>/dev/null | grep -i "ActivityWatch" || true)

  if [ -n "$AW_PROC" ]; then
    echo "⚠  ActivityWatch läuft, antwortet aber nicht auf Port 5600."
    echo "   Bitte ActivityWatch neu starten und dieses Script erneut ausführen."
    echo ""
    read -rp "Enter zum Schließen …"
    exit 1
  elif [ -n "$AW_APP" ]; then
    echo "⚠  ActivityWatch ist installiert, aber nicht gestartet."
    echo "→  Starte ActivityWatch …"
    open -a ActivityWatch 2>/dev/null || open "/Applications/$(echo "$AW_APP" | head -1)"
    echo "   Warte 5 Sekunden …"
    sleep 5
  else
    echo "⚠  ActivityWatch ist nicht installiert."
    echo "→  Installiere ActivityWatch über Homebrew …"
    brew install --cask activitywatch
    echo ""
    echo "→  Starte ActivityWatch …"
    open -a ActivityWatch
    echo "   Warte 8 Sekunden …"
    sleep 8
  fi

  if ! curl -sf http://localhost:5600/api/0/info > /dev/null 2>&1; then
    echo "⚠  ActivityWatch noch nicht bereit."
    echo "   Bitte ActivityWatch manuell starten und dieses Script erneut ausführen."
    echo ""
    read -rp "Enter zum Schließen …"
    exit 1
  fi
  echo "✓  ActivityWatch läuft"
fi
echo ""

# ── Bridge installieren ──────────────────────────────────────────
echo "→  Installiere LOTS ActivityWatch Bridge …"
echo ""
npx --yes lots-aw-bridge install
echo ""

# ── Fertig ───────────────────────────────────────────────────────
echo "══════════════════════════════════════════════════════"
echo ""
echo "  Bitte Claude Desktop neu starten."
echo "  Danach ist 'lots-aw-bridge' als MCP-Server verfügbar."
echo ""
read -rp "Enter zum Schließen …"
