#!/usr/bin/env bash
#
# Aident CLI installer
#   curl -fsSL https://app.aident.ai/cli/install.sh | bash
#
# What this does:
#   1. Verifies Node.js 18+ is installed (or guides you to install it).
#   2. Installs the @aident-ai/cli package globally via npm.
#   3. Runs `aident doctor` to confirm the install.
#
# Optional environment overrides:
#   AIDENT_BASE_URL       API host (default: https://app.aident.ai)
#   AIDENT_CLI_PACKAGE    npm package spec (default: @aident-ai/cli)
#   AIDENT_SKIP_DOCTOR=1  Skip the post-install doctor step.

set -euo pipefail

# --- pretty output ----------------------------------------------------------
if [ -t 1 ]; then
  bold="$(printf '\033[1m')"; dim="$(printf '\033[2m')"; reset="$(printf '\033[0m')"
  red="$(printf '\033[31m')"; green="$(printf '\033[32m')"; yellow="$(printf '\033[33m')"; cyan="$(printf '\033[36m')"
else
  bold=""; dim=""; reset=""; red=""; green=""; yellow=""; cyan=""
fi

info() { printf "%s\n" "$1"; }
ok()   { printf "${green}✓${reset} %s\n" "$1"; }
warn() { printf "${yellow}!${reset} %s\n" "$1"; }
err()  { printf "${red}✗${reset} %s\n" "$1" >&2; }
step() { printf "\n${bold}%s${reset}\n" "$1"; }

# --- preflight --------------------------------------------------------------
step "Aident CLI installer"
CLI_PACKAGE="${AIDENT_CLI_PACKAGE:-@aident-ai/cli}"
info "${dim}Installs ${CLI_PACKAGE} from npm.${reset}"

if ! command -v node >/dev/null 2>&1; then
  err "Node.js is not installed."
  info "Install Node.js 18 or later, then re-run this script:"
  info "  ${cyan}https://nodejs.org/${reset}"
  info "Or with a version manager (recommended):"
  info "  ${cyan}brew install node${reset}    (macOS Homebrew)"
  info "  ${cyan}nvm install --lts${reset}    (https://github.com/nvm-sh/nvm)"
  info "  ${cyan}fnm install --lts${reset}    (https://github.com/Schniz/fnm)"
  exit 1
fi

NODE_VERSION="$(node -v 2>&1 | sed 's/^v//')"
NODE_MAJOR="${NODE_VERSION%%.*}"
if ! [[ "$NODE_MAJOR" =~ ^[0-9]+$ ]] || [ "$NODE_MAJOR" -lt 18 ]; then
  err "Node.js 18 or later is required (you have v${NODE_VERSION})."
  exit 1
fi
ok "Node.js v${NODE_VERSION} detected"

# Pick a package manager (npm is always present with Node).
if command -v npm >/dev/null 2>&1; then
  PM="npm"
  INSTALL_CMD="npm install -g ${CLI_PACKAGE}"
else
  err "npm not found alongside node. Reinstall Node.js so npm is available."
  exit 1
fi

NPM_VERSION="$(npm --version 2>/dev/null || echo 0)"
NPM_MAJOR="${NPM_VERSION%%.*}"
if ! [[ "$NPM_MAJOR" =~ ^[0-9]+$ ]] || [ "$NPM_MAJOR" -lt 7 ]; then
  err "npm v${NPM_VERSION} is too old (need ≥ 7 for ESM packages)."
  info "Upgrade with: ${cyan}npm install -g npm@latest${reset}, then re-run."
  exit 1
fi
ok "Using ${PM} v${NPM_VERSION}"

# --- install ----------------------------------------------------------------
step "Installing ${CLI_PACKAGE}"
info "${dim}\$ ${INSTALL_CMD}${reset}"

if ! $INSTALL_CMD; then
  err "Global install failed."
  warn "Most often this is a permissions error from running npm against a system-owned prefix."
  info "Recommended: install Node via a version manager so npm globals live in your home directory."
  info "  • ${cyan}nvm install --lts${reset}    https://github.com/nvm-sh/nvm"
  info "  • ${cyan}fnm install --lts${reset}    https://github.com/Schniz/fnm"
  info "  • ${cyan}volta install node${reset}   https://volta.sh"
  info ""
  info "Then re-run: ${cyan}curl -fsSL https://app.aident.ai/cli/install.sh | bash${reset}"
  exit 1
fi

# --- verify -----------------------------------------------------------------
if ! command -v aident >/dev/null 2>&1; then
  err "aident installed but not on PATH."
  info "Run ${cyan}npm bin -g${reset} and add that directory to PATH, then retry."
  exit 1
fi

INSTALLED_VERSION="$(aident --version 2>/dev/null | tail -1 || echo unknown)"
ok "${INSTALLED_VERSION}"

# Persist baseUrl override if set so first login uses the right host.
if [ -n "${AIDENT_BASE_URL:-}" ]; then
  aident config set baseUrl "${AIDENT_BASE_URL}" >/dev/null 2>&1 || true
  ok "Saved baseUrl=${AIDENT_BASE_URL}"
fi

if [ "${AIDENT_SKIP_DOCTOR:-0}" != "1" ]; then
  step "Running doctor"
  aident doctor || true
fi

step "Next steps"
info "  ${cyan}aident login${reset}     Authenticate with Aident (browser-based)"
info "  ${cyan}aident --help${reset}    Discover commands"
info "  ${cyan}aident setup${reset}     Interactive setup wizard"
info ""
info "Skill for coding agents:  ${cyan}npx skills add aident-ai/aident-skill${reset}"
info "Docs:                     ${cyan}https://docs.aident.ai${reset}"
