#!/usr/bin/env bash
# TWC Automation · Claude Code onboarding
# Mirrors the toolchain the TWC Automation team runs locally.
# Re-runnable: brew/npm will no-op if a package is already installed.

set -euo pipefail

say() { printf '\n\033[1;34m==>\033[0m %s\n' "$*"; }
ok()  { printf '   \033[1;32m✓\033[0m %s\n' "$*"; }
warn(){ printf '   \033[1;33m!\033[0m %s\n' "$*"; }

# 0. Sudo keep-alive -------------------------------------------------------
# Homebrew + each cask install (1Password, gcloud SDK) trigger sudo
# independently, and macOS expires the auth after ~5 minutes. Without
# this block you'd be re-prompted 3-4 times. Ask once, refresh in the
# background, drop the keep-alive on exit.
say "Asking for your Mac password ONCE so the installer can run without re-prompting"
echo "   (Homebrew + the apps below all need admin to install. We'll keep auth alive in the background.)"
sudo -v
( while true; do sudo -n true; sleep 50; kill -0 "$$" 2>/dev/null || exit; done ) >/dev/null 2>&1 &
SUDO_KEEPER_PID=$!
trap 'kill $SUDO_KEEPER_PID 2>/dev/null || true' EXIT

# 1. Homebrew --------------------------------------------------------------
say "Checking for Homebrew"
if ! command -v brew >/dev/null 2>&1; then
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  if [[ -d /opt/homebrew/bin ]]; then
    eval "$(/opt/homebrew/bin/brew shellenv)"
    grep -q 'brew shellenv' ~/.zprofile 2>/dev/null \
      || echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
  fi
  ok "Homebrew installed"
else
  ok "Homebrew already present"
fi

# 2. CLI formulae ----------------------------------------------------------
say "Installing CLI tools (awscli, gh, node, jq, python@3.12, uv, cloudflared)"
brew install awscli gh node jq python@3.12 uv cloudflared

# 3. Casks -----------------------------------------------------------------
say "Installing apps (1Password, 1Password CLI, gcloud SDK)"
brew install --cask 1password 1password-cli gcloud-cli

# 4. Global npm packages ---------------------------------------------------
say "Installing Claude Code + companion CLIs from npm"
npm install -g \
  @anthropic-ai/claude-code \
  @google/clasp \
  @googleworkspace/cli \
  @trayio/cdk-cli

# 5. Anthropic API key -----------------------------------------------------
say "Wiring your Anthropic API key into ~/.zshrc"
echo "   (The team DM'd this to you. Paste, then Enter. Input is hidden.)"
read -r -s -p "   ANTHROPIC_API_KEY: " ANTHROPIC_KEY
echo

if [[ -z "${ANTHROPIC_KEY}" ]]; then
  warn "No key entered. Skipping. Re-run the script when you have it."
else
  touch ~/.zshrc
  /usr/bin/sed -i '' '/^export ANTHROPIC_API_KEY=/d' ~/.zshrc
  printf '\nexport ANTHROPIC_API_KEY=%q\n' "$ANTHROPIC_KEY" >> ~/.zshrc
  ok "Key written to ~/.zshrc"
fi

# 6. Done ------------------------------------------------------------------
say "All done"
cat <<'EOF'

   Next steps (one-time, manual):
     • Open a NEW Terminal tab so ANTHROPIC_API_KEY is loaded.
     • Run:  claude        (starts Claude Code)
     • Run:  gh auth login        (GitHub access)
     • Run:  aws configure        (only if you have AWS creds)
     • Run:  gcloud init          (only if you'll use Google Cloud)

   Questions? Ping the TWC Automation team on Slack.

EOF
