#!/usr/bin/env bash
set -euo pipefail

# Bump when publishing changes to this script (used for self-update checks).
SCRIPT_VERSION="1.4.1"
SCRIPT_URL="${UPDATE_NVM_SCRIPT_URL:-https://omid.dev/scripts/update-nvm.sh}"

# Preserve argv for re-exec after an accepted self-update.
ORIGINAL_ARGS=("$@")

VERSIONS=()
EXPLICIT_VERSIONS=0
_NVM_REINSTALL_FROM=""
LTS_MODE=0
FORCE=0
PRUNE=0
SKIP_NPM=0
NPM_ONLY=0
LATEST_NPM=1
COREPACK=1
QUIET=0
DRY_RUN=0
PLAIN=0
SELF_UPDATE=0

usage() {
  cat <<'HELP'
Usage: update-nvm [OPTIONS] [VERSION ...]

Update installed Node.js versions with nvm and refresh global npm packages.

If no versions are given, updates every major version already installed via nvm.
Use --lts to update only the current LTS line (lts/*) instead.
Pass explicit versions to update or install those (e.g. update-nvm 24 or update-nvm 28).

When a major is already on the latest patch, Node is left alone but global npm
packages are still refreshed (unless --skip-npm). Use --npm-only to skip Node
installs entirely and only refresh npm.

When installing a major that is not installed yet, the script asks whether to
copy global npm packages from another installed major.

On interactive runs, if a newer update-nvm is available on omid.dev, you are
asked whether to self-update before work starts (default: Yes). Accepting
re-runs this command with the new script; declining continues with the
installed version. Quiet/cron and non-TTY runs skip that prompt.

Options:
  --lts             Update lts/* only (overrides auto-detected installed majors)
  --force, -f       Reinstall even when the latest patch is already installed
  --prune           Remove older patch releases within each updated major line
  --latest-npm      Upgrade npm to the latest version supported by each Node release
                    (default: on)
  --no-latest-npm   Keep the npm version bundled with Node
  --no-corepack     Skip corepack enable for each updated version
  --skip-npm        Reinstall Node only; skip global npm package updates
  --npm-only        Skip Node installs; only refresh npm and global packages
  --quiet, -q       Minimal output (errors still go to stderr; skips install prompts)
  --dry-run, -n     Show what would run without changing anything
  --plain, -p       No live progress status line (logs only)
  --self-update     Download and install the latest update-nvm script from omid.dev
                    (does not update Node or npm)
  --version         Print this script's version and exit
  -h, --help        Show this help

Environment:
  NVM_DIR                 nvm install directory (default: ~/.nvm)
  NVM_UPDATE_VERSIONS     Space-separated versions (explicit list; ignored with --lts)
  NVM_REINSTALL_FROM      When installing a missing major non-interactively, copy
                          global npm packages from this major (e.g. 24)
  UPDATE_NVM_SCRIPT_URL   Override script URL for self-update (default: omid.dev)
  UPDATE_NVM_SKIP_SELF_CHECK  Set to 1 to skip the newer-script availability check
  NO_COLOR                Set to disable ANSI colors
HELP
}

for arg in "$@"; do
  case "$arg" in
    --lts)
      LTS_MODE=1
      ;;
    --force|-f)
      FORCE=1
      ;;
    --prune)
      PRUNE=1
      ;;
    --latest-npm)
      LATEST_NPM=1
      ;;
    --no-latest-npm)
      LATEST_NPM=0
      ;;
    --no-corepack)
      COREPACK=0
      ;;
    --skip-npm)
      SKIP_NPM=1
      ;;
    --npm-only)
      NPM_ONLY=1
      ;;
    --quiet|-q)
      QUIET=1
      ;;
    --dry-run|-n)
      DRY_RUN=1
      ;;
    --plain|-p)
      PLAIN=1
      ;;
    --self-update)
      SELF_UPDATE=1
      ;;
    --version)
      echo "update-nvm ${SCRIPT_VERSION}"
      exit 0
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    --)
      shift
      VERSIONS+=("$@")
      EXPLICIT_VERSIONS=1
      break
      ;;
    -*)
      echo "Unknown option: $arg" >&2
      echo "Usage: update-nvm [--lts] [--force|-f] [--prune] [--npm-only] [--skip-npm] [--quiet|-q] [--dry-run|-n] [--plain|-p] [--self-update] [--version] [VERSION ...]" >&2
      exit 1
      ;;
    *)
      VERSIONS+=("$arg")
      EXPLICIT_VERSIONS=1
      ;;
  esac
done

if [[ "$NPM_ONLY" -eq 1 && "$SKIP_NPM" -eq 1 ]]; then
  echo "--npm-only and --skip-npm cannot be used together." >&2
  exit 1
fi

# Exit 0 if equal, 1 if $1 > $2, 2 if $1 < $2
version_compare() {
  local IFS=.
  local -a left=($1) right=($2)
  local i max=${#left[@]}

  if ((${#right[@]} > max)); then
    max=${#right[@]}
  fi

  for ((i = 0; i < max; i++)); do
    local a=${left[i]:-0}
    local b=${right[i]:-0}

    if ((10#$a > 10#$b)); then
      return 1
    fi
    if ((10#$a < 10#$b)); then
      return 2
    fi
  done

  return 0
}

script_version_from() {
  local source="$1"

  sed -n 's/^SCRIPT_VERSION="\([^"]*\)".*/\1/p' "$source" | head -1
}

remote_script_version() {
  if ! command -v curl >/dev/null 2>&1; then
    return 1
  fi

  curl -fsSL --connect-timeout 5 --max-time 15 "$SCRIPT_URL" 2>/dev/null \
    | sed -n 's/^SCRIPT_VERSION="\([^"]*\)".*/\1/p' | head -1
}

# Replace this script with the remote release. With --reexec, continue the
# original command using the new file; otherwise exit after updating.
self_update_script() {
  local reexec=0
  local remote script_path tmp new_version cmp

  if [[ "${1:-}" == "--reexec" ]]; then
    reexec=1
  fi

  if ! command -v curl >/dev/null 2>&1; then
    echo "curl is required for self-update." >&2
    exit 1
  fi

  remote="$(remote_script_version)" || true
  if [[ -z "$remote" ]]; then
    echo "Could not fetch script version from $SCRIPT_URL" >&2
    exit 1
  fi

  cmp=2
  version_compare "$SCRIPT_VERSION" "$remote" && cmp=0 || cmp=$?

  case "$cmp" in
    0)
      echo "update-nvm ${SCRIPT_VERSION} is already up to date."
      exit 0
      ;;
    1)
      echo "Local update-nvm ${SCRIPT_VERSION} is newer than remote ${remote}."
      exit 0
      ;;
  esac

  script_path="$(readlink -f "${BASH_SOURCE[0]}")"
  tmp="$(mktemp)"
  trap 'rm -f "$tmp"' EXIT

  curl -fsSL "$SCRIPT_URL" -o "$tmp"
  new_version="$(script_version_from "$tmp")"
  if [[ -z "$new_version" || "$new_version" != "$remote" ]]; then
    echo "Downloaded script does not look like a valid update-nvm release." >&2
    exit 1
  fi

  chmod +x "$tmp"
  mv -f "$tmp" "$script_path"
  trap - EXIT

  echo "update-nvm updated to ${new_version}."
  if [[ "$reexec" -eq 1 ]]; then
    export UPDATE_NVM_REEXEC=1
    exec "$script_path" "${ORIGINAL_ARGS[@]}"
  fi
  exit 0
}

# Prompt (interactive) or hint (non-TTY) before Node work so an accepted
# update can re-exec and run jobs with the new script.
maybe_self_update_before_run() {
  local remote cmp answer

  # Set by a prior accepted update so the re-exec does not prompt again.
  if [[ "${UPDATE_NVM_REEXEC:-0}" == 1 ]]; then
    unset UPDATE_NVM_REEXEC
    return 0
  fi

  if [[ "${UPDATE_NVM_SKIP_SELF_CHECK:-0}" == 1 || "$QUIET" -eq 1 || "$DRY_RUN" -eq 1 ]]; then
    return 0
  fi

  remote="$(remote_script_version)" || return 0
  [[ -z "$remote" ]] && return 0

  cmp=2
  version_compare "$SCRIPT_VERSION" "$remote" && cmp=0 || cmp=$?
  if [[ "$cmp" -ne 2 ]]; then
    return 0
  fi

  if [[ -t 0 && -t 1 ]]; then
    echo
    read -r -p "update-nvm ${remote} is available (installed: ${SCRIPT_VERSION}). Update now? [Y/n] " answer
    case "$answer" in
      ''|y|Y|yes|YES|Yes)
        self_update_script --reexec
        ;;
      *)
        echo "Skipped. Continuing with update-nvm ${SCRIPT_VERSION}."
        echo "Run later: update-nvm --self-update"
        ;;
    esac
  else
    echo "update-nvm $remote is available (installed: $SCRIPT_VERSION). Run: update-nvm --self-update" >&2
  fi
}

if [[ "$SELF_UPDATE" -eq 1 ]]; then
  self_update_script
  exit 0
fi

maybe_self_update_before_run

if [[ "$LTS_MODE" -eq 1 && ${#VERSIONS[@]} -eq 0 ]]; then
  VERSIONS=(lts/*)
  EXPLICIT_VERSIONS=1
elif ((${#VERSIONS[@]} == 0)); then
  if [[ -n "${NVM_UPDATE_VERSIONS:-}" ]]; then
    # shellcheck disable=SC2206
    VERSIONS=($NVM_UPDATE_VERSIONS)
    EXPLICIT_VERSIONS=1
  fi
fi

export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"
if [[ ! -s "$NVM_DIR/nvm.sh" ]]; then
  echo "nvm not found at $NVM_DIR/nvm.sh" >&2
  echo "Install nvm: https://github.com/nvm-sh/nvm" >&2
  exit 1
fi

# shellcheck source=/dev/null
. "$NVM_DIR/nvm.sh"

# default may be unset or unusable (e.g. lts/* not installed); pick latest installed.
if ! command -v npm >/dev/null 2>&1; then
  nvm use node >/dev/null 2>&1 || true
fi

if ! command -v npm >/dev/null 2>&1; then
  echo "npm is required but not available after loading nvm." >&2
  exit 1
fi

USE_COLOR=0
if [[ "$QUIET" -eq 0 && -t 1 && -z "${NO_COLOR:-}" && "${TERM:-}" != "dumb" ]]; then
  USE_COLOR=1
fi

C_RESET=""
C_BOLD=""
C_DIM=""
C_CYAN=""
C_GREEN=""
C_YELLOW=""
C_RED=""
if [[ "$USE_COLOR" -eq 1 ]]; then
  C_RESET=$'\033[0m'
  C_BOLD=$'\033[1m'
  C_DIM=$'\033[2m'
  C_CYAN=$'\033[36m'
  C_GREEN=$'\033[32m'
  C_YELLOW=$'\033[33m'
  C_RED=$'\033[31m'
fi

# Parallel to VERSIONS: todo | doing | done | refreshed | failed | skipped
STEP_STATE=()
STEP_DETAIL=()
TOTAL_STEPS=0
STEP_INDEX=0
# Live status line rewritten in place as the last output line (not viewport-pinned).
STATUS_SHOWN=0
STATUS_ENABLED=1

can_use_status_line() {
  [[ "$STATUS_ENABLED" -eq 1 && "$PLAIN" -eq 0 && "$QUIET" -eq 0 && -t 1 && "${TERM:-}" != "dumb" ]]
}

step_note() {
  local state="$1"
  local detail="$2"
  local note

  case "$state" in
    done) note="updated" ;;
    refreshed) note="current · npm refreshed" ;;
    skipped) note="skipped" ;;
    failed) note="failed" ;;
    doing) note="doing" ;;
    *) note="todo" ;;
  esac
  if [[ -n "$detail" ]]; then
    note+=" · ${detail}"
  fi
  printf '%s\n' "$note"
}

# Compact one-liner: "→ [2/3] Node.js 24  doing · …  · ✓22 ·24 ·26"
status_line_text() {
  local i state detail icon color note label trail=""
  local cur_idx=-1 completed=0

  for ((i = 0; i < TOTAL_STEPS; i++)); do
    state="${STEP_STATE[i]:-todo}"
    if [[ "$state" == "doing" ]]; then
      cur_idx=$i
    fi
    case "$state" in
      done|refreshed) icon="✓"; color="$C_GREEN"; completed=$((completed + 1)) ;;
      skipped) icon="○"; color="$C_YELLOW"; completed=$((completed + 1)) ;;
      failed) icon="✗"; color="$C_RED"; completed=$((completed + 1)) ;;
      doing) icon="→"; color="$C_CYAN" ;;
      *) icon="·"; color="$C_DIM" ;;
    esac
    trail+=" ${color}${icon}${VERSIONS[i]}${C_RESET}"
  done

  if ((cur_idx >= 0)); then
    state="${STEP_STATE[cur_idx]}"
    detail="${STEP_DETAIL[cur_idx]:-}"
    note="$(step_note "$state" "$detail")"
    label="Node.js ${VERSIONS[cur_idx]}"
    if [[ "$NPM_ONLY" -eq 1 ]]; then
      label+=" (npm)"
    fi
    printf '%s→%s [%d/%d] %s  %s%s%s ·%s' \
      "$C_CYAN" "$C_RESET" \
      "$((cur_idx + 1))" "$TOTAL_STEPS" \
      "$label" \
      "$C_DIM" "$note" "$C_RESET" \
      "$trail"
  else
    printf '%sProgress%s [%d/%d] ·%s' \
      "$C_BOLD" "$C_RESET" "$completed" "$TOTAL_STEPS" "$trail"
  fi
}

# Status ANSI must hit the tty directly so `run cmd >/dev/null` cannot swallow it.
status_printf() {
  if [[ -w /dev/tty ]]; then
    # shellcheck disable=SC2059
    printf "$@" >/dev/tty
  else
    # shellcheck disable=SC2059
    printf "$@"
  fi
}

hide_status_line() {
  if [[ "$STATUS_SHOWN" -eq 1 ]]; then
    status_printf '\r\033[K'
    STATUS_SHOWN=0
  fi
}

show_status_line() {
  if ! can_use_status_line; then
    return 0
  fi
  status_printf '\r\033[K%s' "$(status_line_text)"
  STATUS_SHOWN=1
}

# Finish the status line with a newline so later output does not overwrite it.
finish_status_line() {
  if [[ "$STATUS_SHOWN" -eq 1 ]]; then
    status_printf '\n'
    STATUS_SHOWN=0
  fi
  STATUS_ENABLED=0
}

print_banner() {
  if [[ "$QUIET" -ne 0 ]]; then
    return 0
  fi

  echo "${C_CYAN}${C_BOLD}"
  cat <<'BANNER'
 _   _ ____  ____    _  _____ _____      _   ___     ____  __
| | | |  _ \|  _ \  / \|_   _| ____|    | \ | \ \   / /  \/  |
| | | | |_) | | | |/ _ \ | | |  _| _____|  \| |\ \ / /| |\/| |
| |_| |  __/| |_| / ___ \| | | |__|_____| |\  | \ V / | |  | |
 \___/|_|   |____/_/   \_\_| |_____|    |_| \_|  \_/  |_|  |_|
BANNER
  echo "${C_RESET}${C_DIM}  keep Node majors current  ·  v${SCRIPT_VERSION}${C_RESET}"
  echo
}

print_progress_board() {
  local i state label icon color detail note

  if [[ "$QUIET" -ne 0 ]]; then
    return 0
  fi

  echo "${C_BOLD} Progress${C_RESET}"
  for ((i = 0; i < TOTAL_STEPS; i++)); do
    state="${STEP_STATE[i]:-todo}"
    detail="${STEP_DETAIL[i]:-}"
    label="Node.js ${VERSIONS[i]}"
    if [[ "$NPM_ONLY" -eq 1 ]]; then
      label+=" (npm)"
    fi

    case "$state" in
      done)
        icon="✓"
        color="$C_GREEN"
        note="updated"
        ;;
      refreshed)
        icon="✓"
        color="$C_GREEN"
        note="current · npm refreshed"
        ;;
      skipped)
        icon="○"
        color="$C_YELLOW"
        note="skipped"
        ;;
      failed)
        icon="✗"
        color="$C_RED"
        note="failed"
        ;;
      doing)
        icon="→"
        color="$C_CYAN"
        note="doing"
        ;;
      *)
        icon="·"
        color="$C_DIM"
        note="todo"
        ;;
    esac

    if [[ -n "$detail" ]]; then
      note+=" · ${detail}"
    fi

    printf '  %s%s%s  [%d/%d] %-28s %s\n' \
      "$color" "$icon" "$C_RESET" \
      "$((i + 1))" "$TOTAL_STEPS" \
      "$label" \
      "${color}${note}${C_RESET}"
  done
  echo "${C_DIM}────────────────────────────────────────────────────────${C_RESET}"
}

render_frame() {
  show_status_line
}

set_step_state() {
  local idx="$1"
  local state="$2"
  local detail="${3:-}"

  STEP_STATE[idx]="$state"
  STEP_DETAIL[idx]="$detail"
}

emit_line() {
  if [[ "$QUIET" -ne 0 ]]; then
    return 0
  fi
  hide_status_line
  printf '%s\n' "$1"
  show_status_line
}

log() {
  if [[ "$QUIET" -eq 0 ]]; then
    if [[ $# -eq 0 ]]; then
      emit_line ""
    else
      emit_line "$*"
    fi
  fi
}

log_step() {
  emit_line "${C_BOLD}${C_CYAN}$*${C_RESET}"
}

log_info() {
  emit_line "  ${C_DIM}$*${C_RESET}"
}

log_action() {
  emit_line "  ${C_CYAN}→${C_RESET} $*"
}

log_ok() {
  emit_line "  ${C_GREEN}✓${C_RESET} $*"
}

log_skip() {
  emit_line "  ${C_YELLOW}○${C_RESET} $*"
}

log_warn() {
  hide_status_line
  echo "  ${C_YELLOW}!${C_RESET} $*" >&2
  show_status_line
}

log_fail() {
  hide_status_line
  echo "  ${C_RED}✗${C_RESET} $*" >&2
  show_status_line
}

run() {
  if [[ "$DRY_RUN" -eq 1 ]]; then
    if [[ "$QUIET" -eq 0 ]]; then
      hide_status_line
      printf '  +'
      printf ' %q' "$@"
      printf '\n'
      show_status_line
    fi
  else
    hide_status_line
    local ec=0
    "$@" || ec=$?
    show_status_line
    return "$ec"
  fi
}

# Like run, but silences the command only (status ANSI still reaches the tty).
run_silent() {
  if [[ "$DRY_RUN" -eq 1 ]]; then
    run "$@"
    return $?
  fi
  hide_status_line
  local ec=0
  "$@" >/dev/null || ec=$?
  show_status_line
  return "$ec"
}

installed_version() {
  local spec="$1"
  local version

  version="$(nvm version "$spec" 2>/dev/null || true)"
  if [[ -z "$version" || "$version" == "N/A" || "$version" == "none" || "$version" == "system" ]]; then
    return 1
  fi

  printf '%s\n' "$version"
}

remote_version() {
  local spec="$1"
  local version

  version="$(nvm version-remote "$spec" 2>/dev/null || true)"
  if [[ -z "$version" || "$version" == "N/A" ]]; then
    return 1
  fi

  printf '%s\n' "$version"
}

version_major() {
  local version="${1#v}"
  printf '%s\n' "${version%%.*}"
}

list_installed_versions() {
  nvm ls --no-alias --no-colors 2>/dev/null \
    | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' \
    | sort -u -V
}

list_installed_majors() {
  list_installed_versions \
    | sed -E 's/^v([0-9]+)\..*/\1/' \
    | sort -u -n
}

if ((${#VERSIONS[@]} == 0)); then
  mapfile -t VERSIONS < <(list_installed_majors)
  if ((${#VERSIONS[@]} == 0)); then
    log_warn "No Node.js versions installed via nvm."
    exit 0
  fi
fi

update_global_packages() {
  local label="$1"

  if [[ "$SKIP_NPM" -eq 1 ]]; then
    return 0
  fi

  log_action "Updating global npm packages ($label)…"
  run npm update -g
}

upgrade_npm() {
  local label="$1"

  if [[ "$LATEST_NPM" -eq 0 ]]; then
    return 0
  fi

  log_action "Upgrading npm ($label)…"
  run nvm install-latest-npm
}

# Refresh npm + global packages + corepack for the currently selected Node.
refresh_npm_stack() {
  local label="$1"

  upgrade_npm "$label"
  update_global_packages "$label"
  enable_corepack "$label"
}

enable_corepack() {
  local version_label="${1:-$(node -v)}"

  if [[ "$COREPACK" -eq 0 ]]; then
    return 0
  fi

  if [[ "$DRY_RUN" -eq 0 ]] && ! command -v corepack >/dev/null 2>&1; then
    log_skip "corepack not available for $version_label"
    return 0
  fi

  log_action "Enabling corepack for $version_label…"
  run corepack enable
}

prune_old_patches() {
  local spec="$1"
  local keep major ver

  if ! keep="$(installed_version "$spec")"; then
    return 0
  fi

  major="$(version_major "$keep")"

  while IFS= read -r ver; do
    [[ -z "$ver" ]] && continue
    [[ "$ver" == "$keep" ]] && continue
    [[ "$(version_major "$ver")" != "$major" ]] && continue

    log_action "Pruning old patch $ver…"
    run nvm uninstall "$ver"
  done < <(list_installed_versions)
}

prompt_reinstall_from() {
  local target_spec="$1"
  local major source_version

  _NVM_REINSTALL_FROM=""

  if [[ -n "${NVM_REINSTALL_FROM:-}" ]]; then
    if source_version="$(installed_version "$NVM_REINSTALL_FROM")"; then
      log_action "Copying global packages from Node $NVM_REINSTALL_FROM ($source_version)"
      _NVM_REINSTALL_FROM="$NVM_REINSTALL_FROM"
      return 0
    fi

    log_warn "NVM_REINSTALL_FROM=$NVM_REINSTALL_FROM is not installed; installing without package copy."
    return 1
  fi

  if [[ "$QUIET" -eq 1 || "$DRY_RUN" -eq 1 || ! -t 0 ]]; then
    log_info "Installing $target_spec without copying global npm packages."
    return 1
  fi

  local -a sources=()
  while IFS= read -r major; do
    [[ -z "$major" ]] && continue
    [[ "$major" == "$target_spec" ]] && continue
    sources+=("$major")
  done < <(list_installed_majors)

  if ((${#sources[@]} == 0)); then
    log_info "No other installed majors to copy global npm packages from."
    return 1
  fi

  hide_status_line
  echo
  echo "Node.js $target_spec is not installed yet."
  echo "Reinstall global npm packages from another installed major?"
  echo "  n) No, fresh install (default)"
  for major in "${sources[@]}"; do
    source_version="$(installed_version "$major")"
    echo "  $major) From Node $major ($source_version)"
  done

  while true; do
    local choice
    read -r -p "Choice [N/${sources[*]}]: " choice

    case "$choice" in
      n|N|'')
        log_info "Installing $target_spec without copying global npm packages."
        return 1
        ;;
      *)
        if source_version="$(installed_version "$choice")"; then
          log_action "Copying global packages from Node $choice ($source_version)"
          _NVM_REINSTALL_FROM="$choice"
          return 0
        fi
        echo "Invalid choice. Enter n or one of: ${sources[*]}"
        ;;
    esac
  done
}

build_nvm_install_args() {
  local spec="$1"
  local prev_installed

  NVM_INSTALL_ARGS=(install "$spec")

  if prev_installed="$(installed_version "$spec")"; then
    NVM_INSTALL_ARGS+=(--reinstall-packages-from="$prev_installed")
  elif [[ "$EXPLICIT_VERSIONS" -eq 1 ]] && prompt_reinstall_from "$spec"; then
    NVM_INSTALL_ARGS+=(--reinstall-packages-from="$_NVM_REINSTALL_FROM")
  fi

  if [[ "$LATEST_NPM" -eq 1 ]]; then
    NVM_INSTALL_ARGS+=(--latest-npm)
  fi
}

restore_active_version() {
  local major

  if [[ "$ORIGINAL_NVM" == "none" || "$ORIGINAL_NVM" == "system" ]]; then
    if nvm alias default >/dev/null 2>&1; then
      log_action "Restoring nvm default…"
      run_silent nvm use default
    fi
    return 0
  fi

  major="$(version_major "$ORIGINAL_NVM")"
  # Redirect: installed_version prints the version on stdout when used as a test.
  if installed_version "$major" >/dev/null; then
    log_action "Restoring Node.js $major…"
    run_silent nvm use "$major"
  elif nvm alias default >/dev/null 2>&1; then
    log_info "Node.js $major not installed; using default"
    run_silent nvm use default
  fi
}

# Result of the last process_one_version call: done | refreshed | skipped | failed
_STEP_OUTCOME=""
_STEP_OUTCOME_DETAIL=""

# Process a single major. Sets _STEP_OUTCOME / _STEP_OUTCOME_DETAIL. Returns 0 unless failed.
process_one_version() {
  local version="$1"
  local remote="" installed=""

  _STEP_OUTCOME="done"
  _STEP_OUTCOME_DETAIL=""

  if remote="$(remote_version "$version")"; then
    if installed="$(installed_version "$version")"; then
      log_info "Installed: $installed"
      log_info "Latest:    $remote"
    else
      log_info "Not installed yet"
      log_info "Latest:    $remote"
    fi
  else
    log_warn "Could not resolve latest version for $version; continuing anyway."
    installed="$(installed_version "$version" || true)"
  fi

  if [[ "$NPM_ONLY" -eq 1 ]]; then
    if [[ -z "$installed" ]]; then
      log_warn "$version is not installed; skipping npm-only refresh."
      _STEP_OUTCOME="skipped"
      _STEP_OUTCOME_DETAIL="not installed"
      return 0
    fi

    log_info "Node $installed left in place; refreshing npm stack…"
    run_silent nvm use "$version"
    refresh_npm_stack "$installed"
    PACKAGES_REFRESHED+=("$installed")
    if [[ "$PRUNE" -eq 1 ]]; then
      prune_old_patches "$version"
    fi
    log_ok "npm stack refreshed for $installed"
    _STEP_OUTCOME="refreshed"
    _STEP_OUTCOME_DETAIL="$installed"
    return 0
  fi

  if [[ -n "$remote" && -n "$installed" && "$installed" == "$remote" && "$FORCE" -eq 0 ]]; then
    CURRENT_VERSIONS+=("$installed")
    run_silent nvm use "$version"
    if [[ "$SKIP_NPM" -eq 1 ]]; then
      log_skip "Node already current ($installed); npm refresh skipped (--skip-npm)"
      _STEP_OUTCOME="skipped"
      _STEP_OUTCOME_DETAIL="$installed"
    else
      log_info "Node already current ($installed); refreshing npm / globals…"
      refresh_npm_stack "$installed"
      PACKAGES_REFRESHED+=("$installed")
      log_ok "npm stack refreshed (Node left at $installed)"
      _STEP_OUTCOME="refreshed"
      _STEP_OUTCOME_DETAIL="$installed"
    fi
    if [[ "$PRUNE" -eq 1 ]]; then
      prune_old_patches "$version"
    fi
    return 0
  fi

  if [[ -n "$installed" ]]; then
    run_silent nvm use "$version"
    update_global_packages "before Node update"
  else
    log_info "Fresh install"
  fi

  log_action "Installing latest Node.js $version…"
  build_nvm_install_args "$version"
  run nvm "${NVM_INSTALL_ARGS[@]}"

  update_global_packages "after Node update"
  if [[ "$DRY_RUN" -eq 0 ]]; then
    enable_corepack "$(node -v)"
  elif [[ -n "$remote" ]]; then
    enable_corepack "$remote"
  else
    enable_corepack "$version"
  fi

  if [[ "$PRUNE" -eq 1 ]]; then
    prune_old_patches "$version"
  fi

  if [[ "$DRY_RUN" -eq 0 ]]; then
    log_ok "Updated to $(node -v) (npm $(npm -v))"
    UPDATED_VERSIONS+=("$(node -v)")
    _STEP_OUTCOME_DETAIL="$(node -v)"
  elif [[ -n "$remote" ]]; then
    log_ok "Would update to $remote"
    UPDATED_VERSIONS+=("$remote")
    _STEP_OUTCOME_DETAIL="$remote"
  else
    log_ok "Would update $version"
    UPDATED_VERSIONS+=("$version")
    _STEP_OUTCOME_DETAIL="$version"
  fi

  _STEP_OUTCOME="done"
  return 0
}

ORIGINAL_NVM="$(nvm current)"
UPDATED_VERSIONS=()
CURRENT_VERSIONS=()
PACKAGES_REFRESHED=()
FAILED_VERSIONS=()
TOTAL_STEPS=${#VERSIONS[@]}
STEP_INDEX=0

for ((i = 0; i < TOTAL_STEPS; i++)); do
  STEP_STATE[i]=todo
  STEP_DETAIL[i]=""
done

trap 'finish_status_line' EXIT INT TERM HUP

print_plan() {
  local plan extras=()

  if [[ "$NPM_ONLY" -eq 1 ]]; then
    plan="Refresh npm / globals / corepack for ${TOTAL_STEPS} installed major(s): ${VERSIONS[*]}"
  elif [[ "$LTS_MODE" -eq 1 ]]; then
    plan="Update LTS (lts/*) to the latest patch"
  else
    plan="Update ${TOTAL_STEPS} Node major(s) to the latest patch: ${VERSIONS[*]}"
  fi

  if [[ "$NPM_ONLY" -eq 0 && "$SKIP_NPM" -eq 0 ]]; then
    extras+=("refresh npm/globals/corepack when already current")
  elif [[ "$NPM_ONLY" -eq 0 && "$SKIP_NPM" -eq 1 ]]; then
    extras+=("Node only (--skip-npm)")
  fi
  if [[ "$FORCE" -eq 1 ]]; then
    extras+=("force reinstall")
  fi
  if [[ "$PRUNE" -eq 1 ]]; then
    extras+=("prune older patches")
  fi
  if [[ "$LATEST_NPM" -eq 0 ]]; then
    extras+=("keep bundled npm")
  fi
  if [[ "$COREPACK" -eq 0 ]]; then
    extras+=("skip corepack")
  fi
  if [[ "$DRY_RUN" -eq 1 ]]; then
    extras+=("dry-run — no changes")
  fi

  log_step "Plan"
  log_info "$plan"
  if ((${#extras[@]} > 0)); then
    local joined
    joined="$(printf '%s, ' "${extras[@]}")"
    log_info "${joined%, }"
  fi
}

print_banner
print_plan
log
show_status_line

for version in "${VERSIONS[@]}"; do
  set_step_state "$STEP_INDEX" "doing"
  render_frame
  log
  log_step "[$((STEP_INDEX + 1))/${TOTAL_STEPS}] Node.js ${version}"
  log

  if process_one_version "$version"; then
    set_step_state "$STEP_INDEX" "$_STEP_OUTCOME" "$_STEP_OUTCOME_DETAIL"
  else
    _STEP_OUTCOME="failed"
    _STEP_OUTCOME_DETAIL="${_STEP_OUTCOME_DETAIL:-error}"
    set_step_state "$STEP_INDEX" "failed" "$_STEP_OUTCOME_DETAIL"
    FAILED_VERSIONS+=("$version")
    log_fail "Step failed for Node.js $version"
  fi
  render_frame

  STEP_INDEX=$((STEP_INDEX + 1))
done

finish_status_line
trap - EXIT INT TERM HUP
restore_active_version

if [[ "$QUIET" -eq 1 ]]; then
  summary=""
  if ((${#UPDATED_VERSIONS[@]} > 0)); then
    summary="updated ${UPDATED_VERSIONS[*]}"
  fi
  if ((${#PACKAGES_REFRESHED[@]} > 0)); then
    if [[ -n "$summary" ]]; then
      summary+=", "
    fi
    summary+="npm-refreshed ${PACKAGES_REFRESHED[*]}"
  fi
  if ((${#CURRENT_VERSIONS[@]} > 0)) && ((${#PACKAGES_REFRESHED[@]} == 0)); then
    if [[ -n "$summary" ]]; then
      summary+=", "
    fi
    summary+="current ${CURRENT_VERSIONS[*]}"
  fi
  if ((${#FAILED_VERSIONS[@]} > 0)); then
    if [[ -n "$summary" ]]; then
      summary+=", "
    fi
    summary+="failed ${FAILED_VERSIONS[*]}"
  fi
  if [[ -z "$summary" ]]; then
    summary="no changes"
  fi
  if [[ "$DRY_RUN" -eq 1 ]]; then
    summary="dry-run: $summary"
  fi
  echo "update-nvm: $summary"
else
  echo
  print_progress_board
  echo
  log_step "Summary"
  echo
  if ((${#UPDATED_VERSIONS[@]} > 0)); then
    log_ok "Node updated: ${UPDATED_VERSIONS[*]}"
  fi
  if ((${#PACKAGES_REFRESHED[@]} > 0)); then
    log_ok "Node current, npm refreshed: ${PACKAGES_REFRESHED[*]}"
  fi
  if ((${#CURRENT_VERSIONS[@]} > 0)) && [[ "$SKIP_NPM" -eq 1 ]]; then
    log_skip "Node current (npm skipped): ${CURRENT_VERSIONS[*]}"
  fi
  if ((${#FAILED_VERSIONS[@]} > 0)); then
    log_fail "Failed: ${FAILED_VERSIONS[*]}"
  fi
  if [[ "$DRY_RUN" -eq 0 ]]; then
    log_info "Active Node: $(node -v)"
    log_info "Active npm:  $(npm -v)"
  else
    log_info "Dry run complete; no changes were made."
  fi
  if ((${#FAILED_VERSIONS[@]} > 0)); then
    log_fail "Finished with errors."
  else
    log_ok "Done."
  fi
fi

if ((${#FAILED_VERSIONS[@]} > 0)); then
  exit 1
fi

