#!/usr/bin/env sh
# Fusion installer — https://runfusion.ai
#
# Usage:
#   curl -fsSL https://runfusion.ai/install.sh | sh
#
# What it does:
#   1. Prefers Homebrew on macOS and Linux — taps runfusion/fusion and
#      `brew install fusion` (installs Node as a dependency).
#   2. Falls back to `npm install -g @runfusion/fusion` if Homebrew isn't
#      available but Node/npm already is.
#   3. Otherwise prints guidance on how to install Node or Homebrew.
#
# After install, launch the dashboard with:
#   fusion dashboard     # (also available as `fn dashboard`)

set -eu

# ── Style helpers ──────────────────────────────────────────────────────
if [ -t 1 ] && command -v tput >/dev/null 2>&1 && [ "$(tput colors 2>/dev/null || echo 0)" -ge 8 ]; then
  BOLD=$(tput bold)
  DIM=$(tput dim)
  CYAN=$(tput setaf 6)
  GREEN=$(tput setaf 2)
  YELLOW=$(tput setaf 3)
  RED=$(tput setaf 1)
  RESET=$(tput sgr0)
else
  BOLD='' DIM='' CYAN='' GREEN='' YELLOW='' RED='' RESET=''
fi

log()   { printf '%s\n' "${CYAN}▸${RESET} $*"; }
ok()    { printf '%s\n' "${GREEN}✓${RESET} $*"; }
warn()  { printf '%s\n' "${YELLOW}!${RESET} $*" >&2; }
fail()  { printf '%s\n' "${RED}✗${RESET} $*" >&2; exit 1; }
step()  { printf '\n%s\n' "${BOLD}$*${RESET}"; }

banner() {
  printf '\n'
  printf '%s\n' "${BOLD}${CYAN}  Fusion${RESET}${DIM} — multi-node AI agent orchestrator${RESET}"
  printf '%s\n' "${DIM}  https://runfusion.ai${RESET}"
  printf '\n'
}

# ── OS detection ───────────────────────────────────────────────────────
detect_os() {
  case "$(uname -s 2>/dev/null || echo unknown)" in
    Darwin) echo macos ;;
    Linux)  echo linux ;;
    *)      echo other ;;
  esac
}

# ── Install paths ──────────────────────────────────────────────────────
install_via_brew() {
  step "Installing Fusion via Homebrew"
  log "brew tap runfusion/fusion"
  brew tap runfusion/fusion
  log "brew install fusion"
  brew install fusion
}

install_via_npm() {
  step "Installing Fusion via npm"
  log "npm install -g @runfusion/fusion"
  if npm install -g @runfusion/fusion; then
    return 0
  fi
  warn "Global npm install failed — trying with sudo (you may be prompted for your password)."
  sudo npm install -g @runfusion/fusion
}

print_manual_instructions() {
  cat <<'EOF'

Fusion needs either Homebrew or Node.js + npm to install.

  macOS & Linux (recommended):
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    curl -fsSL https://runfusion.ai/install.sh | sh

  Or install Node.js first:
    https://nodejs.org/
    npm install -g @runfusion/fusion

  Then run:
    fusion dashboard

More info: https://github.com/Runfusion/Fusion

EOF
  exit 1
}

# ── Post-install verification ──────────────────────────────────────────
verify_and_finish() {
  if command -v fusion >/dev/null 2>&1; then
    VERSION="$(fusion --version 2>/dev/null || echo '')"
  elif command -v fn >/dev/null 2>&1; then
    VERSION="$(fn --version 2>/dev/null || echo '')"
  else
    warn "Install appeared to succeed, but 'fusion' / 'fn' aren't on your PATH yet."
    warn "You may need to open a new shell or add the install location to PATH."
    VERSION=""
  fi

  step "Done"
  if [ -n "$VERSION" ]; then
    ok "Fusion $VERSION is installed"
  else
    ok "Fusion is installed"
  fi
  printf '\n%s\n'     "  Launch the dashboard:"
  printf '%s\n'       "    ${BOLD}fusion dashboard${RESET}"
  printf '%s\n\n'     "  Docs: https://github.com/Runfusion/Fusion"
}

# ── Main ───────────────────────────────────────────────────────────────
main() {
  banner

  OS="$(detect_os)"
  log "Detected OS: $OS"

  if [ "$OS" = "other" ]; then
    warn "Automated install is supported on macOS and Linux. For Windows, use"
    warn "  npm install -g @runfusion/fusion   (requires Node.js)"
    exit 1
  fi

  if command -v brew >/dev/null 2>&1; then
    ok "Found Homebrew"
    install_via_brew
    verify_and_finish
    return 0
  fi

  if command -v npm >/dev/null 2>&1; then
    warn "Homebrew not found; falling back to npm"
    install_via_npm
    verify_and_finish
    return 0
  fi

  warn "Neither Homebrew nor npm found."
  print_manual_instructions
}

main "$@"
