#!/usr/bin/env bash
#
# AimHuge Workshops — macOS machine setup
# ----------------------------------------
# Takes a stock Mac to a working dev stack:
#   Homebrew · Node (LTS) · pnpm · Claude Code · Vercel CLI · Supabase CLI · Superwhisper
#
# Run with:  bash <(curl -fsSL https://aimhuge.com/setup/mac.sh)
# Idempotent and safe to re-run if a step fails.
#
# Supabase is set up for the HOSTED workflow (CLI for migrations + type-gen).
# No Docker is installed. Resend needs no install — it's an npm package + API key.

set -uo pipefail

# --- pretty output -----------------------------------------------------------
step() { printf "\n\033[1;36m==>\033[0m \033[1m%s\033[0m\n" "$1"; }
ok()   { printf "  \033[32mok\033[0m  %s\n" "$1"; }
warn() { printf "  \033[33m!!\033[0m  %s\n" "$1"; }
err()  { printf "  \033[31mxx\033[0m  %s\n" "$1"; }

step "AimHuge machine setup (macOS)"
echo "Installs: Homebrew, Node LTS, pnpm, Claude Code, Vercel CLI, Supabase CLI, Superwhisper."
echo "Safe to re-run. You'll be asked for your password by Homebrew."

# --- 1. Command Line Tools (git + compilers) ---------------------------------
step "Command Line Tools (git, compilers)"
if xcode-select -p >/dev/null 2>&1; then
  ok "already installed"
else
  warn "installing — accept the popup, let it finish, then re-run this script"
  xcode-select --install || true
fi

# --- 2. Homebrew -------------------------------------------------------------
step "Homebrew"
if ! command -v brew >/dev/null 2>&1; then
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
# Load brew into this session (Apple Silicon vs Intel paths)
if [ -x /opt/homebrew/bin/brew ]; then
  eval "$(/opt/homebrew/bin/brew shellenv)"
elif [ -x /usr/local/bin/brew ]; then
  eval "$(/usr/local/bin/brew shellenv)"
fi
if command -v brew >/dev/null 2>&1; then
  ok "brew $(brew --version | head -n1 | awk '{print $2}')"
else
  err "Homebrew not on PATH — open a new Terminal and re-run this script"
  exit 1
fi

# --- 3. Node.js (LTS) --------------------------------------------------------
step "Node.js (LTS)"
if command -v node >/dev/null 2>&1; then
  ok "node $(node -v) already installed"
else
  brew install node && ok "node $(node -v)"
fi

# --- 4. pnpm (via Corepack, ships with Node) ---------------------------------
step "pnpm"
if command -v pnpm >/dev/null 2>&1; then
  ok "pnpm $(pnpm -v) already installed"
else
  corepack enable >/dev/null 2>&1 && corepack prepare pnpm@latest --activate >/dev/null 2>&1
  if command -v pnpm >/dev/null 2>&1; then
    ok "pnpm $(pnpm -v)"
  else
    brew install pnpm && ok "pnpm $(pnpm -v)"
  fi
fi

# --- 5. Claude Code (via npm — integrity-verified by npm) --------------------
# npm checks the package's registry integrity hash before installing, and the
# Homebrew-node global bin is already on PATH — so no PATH surgery needed.
step "Claude Code"
if command -v claude >/dev/null 2>&1; then
  ok "claude $(claude --version 2>/dev/null || echo installed) already installed"
else
  npm install -g @anthropic-ai/claude-code \
    && ok "claude $(claude --version 2>/dev/null || echo installed)" \
    || warn "claude install failed — re-run, or see https://docs.claude.com/claude-code"
fi

# --- 6. Vercel CLI -----------------------------------------------------------
step "Vercel CLI"
if command -v vercel >/dev/null 2>&1; then
  ok "vercel $(vercel --version 2>/dev/null) already installed"
else
  npm install -g vercel && ok "vercel $(vercel --version 2>/dev/null)"
fi

# --- 7. Supabase CLI (hosted workflow — no Docker) ---------------------------
step "Supabase CLI"
if command -v supabase >/dev/null 2>&1; then
  ok "supabase $(supabase --version 2>/dev/null) already installed"
else
  brew install supabase/tap/supabase && ok "supabase $(supabase --version 2>/dev/null)"
fi

# --- 8. Superwhisper (dictation) ---------------------------------------------
step "Superwhisper (dictation)"
if [ -d "/Applications/superwhisper.app" ]; then
  ok "already installed"
else
  brew install --cask superwhisper \
    && ok "installed" \
    || warn "skipped — install manually from https://superwhisper.com"
fi

# --- Version check -----------------------------------------------------------
step "Version check"
check() {
  if command -v "$1" >/dev/null 2>&1; then
    printf "  \033[32mok\033[0m  %-9s %s\n" "$1" "$("${@:2}" 2>/dev/null | head -n1)"
  else
    printf "  \033[31mxx\033[0m  %-9s not found — open a new Terminal and re-check\n" "$1"
  fi
}
check git     git --version
check node    node -v
check pnpm    pnpm -v
check claude  claude --version
check vercel  vercel --version
check supabase supabase --version

# --- Next steps --------------------------------------------------------------
step "Next: log in and set your keys"
cat <<'EOF'
  Open a NEW Terminal window (so every PATH change loads), then:

    claude              # then /login
    vercel login
    supabase login

  Then, in your project's .env.local, set:
    NEXT_PUBLIC_SUPABASE_URL=...
    NEXT_PUBLIC_SUPABASE_ANON_KEY=...
    SUPABASE_SERVICE_ROLE_KEY=...      # server only
    RESEND_API_KEY=...                 # Resend = npm package + this key, nothing to install

  Stuck? Run:  claude doctor
EOF

step "Done"
echo "If anything shows 'not found' above, open a fresh Terminal and re-run this script."
