#!/bin/sh
# Actionway CLI installer (macOS / Linux).
# Usage: curl -fsSL https://actionway.ai/install.sh | sh
#
# Checks Node.js >= 20 (installing via Homebrew on macOS after confirmation),
# installs @actionway/cli globally with npm, then runs `actionway doctor`.
# Never uses sudo; never edits shell profiles, proxies, or the npm registry.
set -eu

REQUIRED_NODE_MAJOR=20

say() { printf '%s\n' "$*"; }
fail() { printf 'actionway-install: %s\n' "$*" >&2; exit 1; }

node_ok() {
  command -v node >/dev/null 2>&1 || return 1
  major="$(node -v 2>/dev/null | sed 's/^v//' | cut -d. -f1)"
  [ -n "$major" ] && [ "$major" -ge "$REQUIRED_NODE_MAJOR" ] 2>/dev/null
}

confirm() {
  # curl | sh 场景下 stdin 是脚本本身;交互确认走 /dev/tty,回车即继续
  # (rustup/Homebrew 模式)。无终端(agent/CI 管道)时不卡在提问上:
  # 直接继续——运行本安装器即表达了"装好 CLI 及其运行时"的意图。
  if [ -t 0 ]; then
    printf '%s [Y/n] ' "$1"
    read -r answer || answer=""
  elif [ -r /dev/tty ]; then
    printf '%s [Y/n] ' "$1" >/dev/tty
    read -r answer </dev/tty || answer=""
  else
    say "$1 [non-interactive: proceeding]"
    return 0
  fi
  case "$answer" in
    n | N | no | NO) return 1 ;;
    *) return 0 ;;
  esac
}

ensure_node() {
  node_ok && return 0
  if [ "$(uname -s)" = "Darwin" ] && command -v brew >/dev/null 2>&1; then
    if confirm "Node.js ${REQUIRED_NODE_MAJOR}+ is required. Install it now with Homebrew (brew install node)?"; then
      brew install node
      node_ok || fail "Node.js is installed but not visible in this shell. Open a new terminal and re-run this script."
      return 0
    fi
    fail "Node.js ${REQUIRED_NODE_MAJOR}+ is required. Run \`brew install node\`, then re-run this script."
  fi
  fail "Node.js ${REQUIRED_NODE_MAJOR}+ is required. Install the LTS from https://nodejs.org (distro package repos are often older than ${REQUIRED_NODE_MAJOR}), open a new terminal, then re-run this script."
}

main() {
  ensure_node
  say "==> Installing @actionway/cli (npm install --global @actionway/cli@latest)"
  if ! npm install --global @actionway/cli@latest; then
    fail "npm install failed. If this is a permissions error, do not use sudo: configure a user-level npm prefix (https://docs.npmjs.com/resolving-eacces-permissions-errors) and re-run this script."
  fi
  global_prefix="$(npm prefix --global 2>/dev/null)" || fail "npm global prefix lookup failed after installation. Check your npm configuration and re-run this script."
  [ -n "$global_prefix" ] || fail "npm global prefix lookup returned an empty path. Check your npm configuration and re-run this script."
  global_command="${global_prefix%/}/bin/actionway"
  [ -x "$global_command" ] || fail "npm reported a global prefix, but the Actionway executable was not found at ${global_command}. Re-run npm install or check your npm prefix configuration."

  init_command="actionway init"
  if ! command -v actionway >/dev/null 2>&1; then
    init_command="\"${global_command}\" init"
  fi

  say "==> Checking the installation (${global_command} doctor)"
  if "$global_command" doctor; then
    say ""
    say "Actionway CLI is ready. Next: run \`${init_command}\` to sign in and stage the agent skill."
  else
    say "actionway doctor reported failed checks above. Fix them, then run \`${init_command}\`."
  fi

  if ! command -v actionway >/dev/null 2>&1; then
    say ""
    say "Actionway CLI was installed, but ${global_prefix%/}/bin is not in PATH."
    say "Use the absolute command shown above now, or add ${global_prefix%/}/bin to your shell PATH before opening a new terminal."
  fi
}

main
