#!/usr/bin/env bash
# AgentWait one-click installer.
# Installs the AgentWait CLI (global npm) and the Cursor/VS Code extension (VSIX).
# Usage:  curl -fsSL https://agent-wait-web-nu.vercel.app/install.sh | bash
set -euo pipefail

BOLD="\033[1m"; GREEN="\033[32m"; RED="\033[31m"; YELLOW="\033[33m"; RESET="\033[0m"
info()  { printf "${BOLD}${GREEN}✓${RESET} %s\n" "$1"; }
warn()  { printf "${BOLD}${YELLOW}!${RESET} %s\n" "$1"; }
err()   { printf "${BOLD}${RED}✗${RESET} %s\n" "$1" >&2; }
step()  { printf "\n${BOLD}%s${RESET}\n" "$1"; }

VERSION="0.2.0"
VSCODE_NPM="@agentwait/cli"
BASE_URL="${AGENTWAIT_BASE_URL:-https://agent-wait-web-nu.vercel.app}"

step "AgentWait installer (v${VERSION})"

# --- OS / IDE detection -----------------------------------------------------
OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
  Darwin) IDE_CLI_CANDIDATES=("/Applications/Cursor.app/Contents/Resources/app/bin/cursor" "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code");;
  Linux)  IDE_CLI_CANDIDATES=("/usr/bin/cursor" "/usr/share/cursor/bin/cursor" "/usr/bin/code" "/usr/share/code/bin/code");;
  *)      err "Unsupported OS: $OS"; exit 1;;
esac

IDE_CLI=""
IDE_NAME=""
for c in "${IDE_CLI_CANDIDATES[@]}"; do
  if command -v "$c" >/dev/null 2>&1 || [ -x "$c" ]; then
    IDE_CLI="$c"; IDE_NAME="$(basename "$c")"; break
  fi
done
# Also respect PATH lookups for `cursor` / `code`
if [ -z "$IDE_CLI" ]; then
  if command -v cursor >/dev/null 2>&1; then IDE_CLI="$(command -v cursor)"; IDE_NAME="cursor";
  elif command -v code >/dev/null 2>&1; then IDE_CLI="$(command -v code)"; IDE_NAME="code"; fi
fi

# --- 1. CLI (npm global) ----------------------------------------------------
step "1/3  Installing AgentWait CLI"
if ! command -v npm >/dev/null 2>&1; then
  err "npm not found. Install Node.js 18+ first: https://nodejs.org"
  exit 1
fi
if npm install -g "$VSCODE_NPM" >/tmp/agentwait-npm.log 2>&1; then
  info "CLI installed → $(command -v agentwait || echo 'agentwait')"
elif npm install -g "https://github.com/hforwood/AgentWait#main" >/tmp/agentwait-npm.log 2>&1; then
  info "CLI installed from GitHub → $(command -v agentwait || echo 'agentwait')"
else
  warn "npm install failed (see /tmp/agentwait-npm.log). Continuing — you can install the CLI later from https://github.com/hforwood/AgentWait"
fi

# --- 2. Extension (VSIX) ----------------------------------------------------
step "2/3  Installing AgentWait extension"
if [ -z "$IDE_CLI" ]; then
  warn "No Cursor or VS Code CLI found on PATH."
  echo "    Install Cursor from https://cursor.com, then run:"
  echo "      curl -fsSL ${BASE_URL}/downloads/agentwait-extension-latest.vsix -o /tmp/agentwait.vsix"
  echo "      cursor --install-extension /tmp/agentwait.vsix"
else
  TMP_VSIX="$(mktemp -t agentwait.XXXXXX).vsix"
  if curl -fsSL "${BASE_URL}/downloads/agentwait-extension-latest.vsix" -o "$TMP_VSIX"; then
    if "$IDE_CLI" --install-extension "$TMP_VSIX" >/tmp/agentwait-ext.log 2>&1; then
      info "Extension installed in $IDE_NAME"
    else
      warn "Extension install failed (see /tmp/agentwait-ext.log)."
    fi
  else
    warn "Could not download VSIX. Offline? Download manually from ${BASE_URL}/downloads/agentwait-extension-latest.vsix"
  fi
  rm -f "$TMP_VSIX"
fi

# --- 3. Glass-mode note -----------------------------------------------------
step "3/3  Important: Cursor Glass mode"
cat <<'NOTE'
Cursor's "Glass" UI mode has a hard-coded allowlist that blocks third-party
extensions like AgentWait from activating. To use AgentWait you must run Cursor
in classic mode:
    cursor --classic /path/to/your/project
NOTE

step "Done"
echo "  • Verify CLI:        agentwait status"
echo "  • Run your agent:    agentwait run -- codex \"fix the bug\""
echo "  • Docs:              ${BASE_URL}/install"
echo
warn "Reload Cursor (classic) once if the AgentWait icon doesn't appear in the sidebar."
