#!/usr/bin/env bash
set -euo pipefail
# NOTE: IFS is intentionally NOT set globally. Use local IFS in read commands:
#   IFS=$'\n\t' read -r ...

readonly CLI_PACKAGE="@dallay/corvus"
readonly PRIMARY_REPO_SLUG="profiletailors/corvus"
readonly LEGACY_REPO_SLUG="dallay/corvus"
readonly DEFAULT_INSTALL_DIR_USER="$HOME/.local/bin"
readonly DEFAULT_INSTALL_DIR_ROOT="/usr/local/bin"

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'

INSTALL_METHOD="${CORVUS_INSTALL_METHOD:-}"
REQUESTED_VERSION="${CORVUS_VERSION:-latest}"
REQUESTED_INSTALL_DIR="${CORVUS_INSTALL_DIR:-}"
AUTO_APPROVE="${CORVUS_YES:-0}"
SKIP_ONBOARD="${CORVUS_SKIP_ONBOARD:-${CORVUS_NO_ONBOARD:-0}}"
FORCE_ONBOARD="${CORVUS_FORCE_ONBOARD:-0}"
SKIP_CHECKSUM="${CORVUS_SKIP_CHECKSUM:-0}"
APT_INDEX_UPDATED="0"

ALREADY_INSTALLED="0"
EXISTING_CMD_PATH=""
EXISTING_INSTALL_DIR=""
EXISTING_INSTALL_METHOD=""
SKIP_ONBOARD_REASON=""

OS_TYPE=""
LINUX_DISTRO=""
PKG_MGR=""

RESOLVED_DOWNLOAD_URL=""
RESOLVED_CHECKSUM_URL=""
RESOLVED_ASSET_NAME=""
INSTALLED_BINARY_PATH=""

has_cmd() {
  command -v "$1" >/dev/null 2>&1
}

detect_existing_install() {
  local config_path="${HOME}/.corvus/config.toml"

  if has_cmd corvus; then
    EXISTING_CMD_PATH="$(command -v corvus)"
    EXISTING_INSTALL_DIR="$(dirname "$EXISTING_CMD_PATH")"
    ALREADY_INSTALLED="1"
    if [ -L "$EXISTING_CMD_PATH" ] || printf "%s" "$EXISTING_CMD_PATH" | grep -q "/node_modules/.bin/"; then
      EXISTING_INSTALL_METHOD="package"
    else
      local first_line=""
      first_line="$(head -n 1 "$EXISTING_CMD_PATH" 2>/dev/null || true)"
      if printf "%s" "$first_line" | grep -qi "node"; then
        EXISTING_INSTALL_METHOD="package"
      fi
    fi

    if [ "$EXISTING_INSTALL_METHOD" = "package" ]; then
      case "$EXISTING_CMD_PATH" in
        *pnpm*) EXISTING_INSTALL_METHOD="pnpm" ;;
        *yarn*) EXISTING_INSTALL_METHOD="yarn" ;;
        *bun*) EXISTING_INSTALL_METHOD="bun" ;;
        *npm*) EXISTING_INSTALL_METHOD="npm" ;;
      esac
    fi

    if [ -z "$EXISTING_INSTALL_METHOD" ]; then
      EXISTING_INSTALL_METHOD="binary"
    fi
  fi

  if [ -f "$config_path" ]; then
    if [ "$SKIP_ONBOARD" != "1" ] && [ "$FORCE_ONBOARD" != "1" ]; then
      SKIP_ONBOARD="1"
      SKIP_ONBOARD_REASON="existing config detected"
    fi
  fi

  if [ "$ALREADY_INSTALLED" = "1" ] && [ "$SKIP_ONBOARD" != "1" ] && [ "$FORCE_ONBOARD" != "1" ]; then
    SKIP_ONBOARD="1"
    SKIP_ONBOARD_REASON="existing install detected"
  fi
}

print_info() {
  printf "%b\n" "${BLUE}$*${NC}"
}

print_success() {
  printf "%b\n" "${GREEN}$*${NC}"
}

print_warn() {
  printf "%b\n" "${YELLOW}$*${NC}"
}

print_error() {
  printf "%b\n" "${RED}$*${NC}"
}

die() {
  print_error "$*"
  exit 1
}

is_interactive() {
  [ -t 0 ] && [ -t 1 ]
}

has_tty() {
  [ -t 1 ] && [ -r /dev/tty ]
}

confirm() {
  local prompt="$1"
  local default_answer="${2:-y}"
  local answer=""

  if [ "$AUTO_APPROVE" = "1" ]; then
    return 0
  fi

  if ! is_interactive; then
    [ "$default_answer" = "y" ]
    return
  fi

  if [ "$default_answer" = "y" ]; then
    read -r -p "$prompt [Y/n] " answer || true
    answer="${answer:-y}"
  else
    read -r -p "$prompt [y/N] " answer || true
    answer="${answer:-n}"
  fi

  case "$answer" in
    y|Y|yes|YES)
      return 0
      ;;
    *)
      return 1
      ;;
  esac
}

usage() {
  cat <<'EOF'
Corvus installer

Usage:
  curl -fsSL https://profiletailors.com/install | bash
  curl -fsSL https://profiletailors.com/install | bash -s -- [options]

Options:
  --method <binary|pnpm|npm|yarn|bun>  Installation method
  --version <tag>                       Release tag (latest, 0.1.2, v0.1.2)
  --install-dir <path>                  Binary install directory
  --yes                                 Non-interactive mode, auto-confirm prompts
  --no-onboard                          Skip onboarding prompt
  --force-onboard                       Always run onboarding even if already configured
  --skip-checksum                       Skip SHA-256 verification (not recommended)
  -h, --help                            Show this help

Environment variables:
  CORVUS_INSTALL_METHOD
  CORVUS_VERSION
  CORVUS_INSTALL_DIR
  CORVUS_YES=1
  CORVUS_SKIP_ONBOARD=1
  CORVUS_FORCE_ONBOARD=1
  CORVUS_SKIP_CHECKSUM=1
EOF
}

require_supported_shell() {
  [ -n "${BASH_VERSION:-}" ] || die "This installer requires bash."
}

detect_platform() {
  local uname_s=""

  uname_s="$(uname -s | tr '[:upper:]' '[:lower:]')"
  case "$uname_s" in
    darwin)
      OS_TYPE="macos"
      LINUX_DISTRO=""
      ;;
    linux)
      OS_TYPE="linux"
      if [ -r /etc/os-release ]; then
        LINUX_DISTRO="$(awk -F= '/^ID=/{gsub(/\"/, "", $2); print tolower($2)}' /etc/os-release)"
      fi
      ;;
    *)
      die "Unsupported OS: ${uname_s}. This installer supports macOS and Linux."
      ;;
  esac
}

detect_package_manager() {
  PKG_MGR=""

  if [ "$OS_TYPE" = "macos" ]; then
    if has_cmd brew; then
      PKG_MGR="brew"
      return 0
    fi
    return 1
  fi

  if [ "$OS_TYPE" = "linux" ]; then
    if has_cmd apt-get; then
      PKG_MGR="apt-get"
      return 0
    fi
    if has_cmd dnf; then
      PKG_MGR="dnf"
      return 0
    fi
    if has_cmd yum; then
      PKG_MGR="yum"
      return 0
    fi
    if has_cmd pacman; then
      PKG_MGR="pacman"
      return 0
    fi
  fi

  return 1
}

print_preflight_notice() {
  print_info "Preflight"
  print_warn "Beta disclaimer: Corvus is currently in beta. Behavior and compatibility may change or fail without prior notice."
  print_info "This installer will:"
  printf "  - Install Corvus CLI\n"
  printf "  - Ensure minimal dependencies for secure install and plugin verification (curl, tar, and SHA-256 tooling)\n"
  printf "  - Verify checksums by default before installing binaries\n"
}

has_checksum_tool() {
  has_cmd sha256sum || has_cmd shasum
}

can_auto_install_dependency() {
  local dep="$1"

  case "$PKG_MGR" in
    brew)
      case "$dep" in
        curl)
          return 0
          ;;
        tar|sha256)
          return 1
          ;;
      esac
      ;;
    apt-get|dnf|yum|pacman)
      return 0
      ;;
  esac

  return 1
}

run_privileged() {
  if [ "${1:-}" = "--no-sudo" ]; then
    shift
    "$@"
    return
  fi

  if [ "$(id -u)" -eq 0 ]; then
    "$@"
  else
    sudo "$@"
  fi
}

install_dep_with_pkg_mgr() {
  local dep="$1"

  case "$PKG_MGR" in
    brew)
      case "$dep" in
        curl)
          run_privileged --no-sudo brew install curl
          ;;
        *)
          return 1
          ;;
      esac
      ;;
    apt-get)
      if [ "$APT_INDEX_UPDATED" != "1" ]; then
        run_privileged apt-get update -qq
        APT_INDEX_UPDATED="1"
      fi
      case "$dep" in
        sha256)
          run_privileged apt-get install -y -qq coreutils
          ;;
        *)
          run_privileged apt-get install -y -qq "$dep"
          ;;
      esac
      ;;
    dnf)
      case "$dep" in
        sha256)
          run_privileged dnf install -y -q coreutils
          ;;
        *)
          run_privileged dnf install -y -q "$dep"
          ;;
      esac
      ;;
    yum)
      case "$dep" in
        sha256)
          run_privileged yum install -y -q coreutils
          ;;
        *)
          run_privileged yum install -y -q "$dep"
          ;;
      esac
      ;;
    pacman)
      case "$dep" in
        sha256)
          run_privileged pacman -S --noconfirm coreutils
          ;;
        *)
          run_privileged pacman -S --noconfirm "$dep"
          ;;
      esac
      ;;
    *)
      return 1
      ;;
  esac
}

print_dependency_manual_help() {
  local dep="$1"

  case "$dep" in
    curl)
      print_error "Could not auto-install curl. Install curl manually and retry."
      ;;
    tar)
      print_error "Could not auto-install tar. Install tar manually and retry."
      ;;
    sha256)
      print_error "Neither sha256sum nor shasum was found. Install coreutils or Perl (shasum) and retry."
      ;;
  esac
}

ensure_dependency() {
  local dep="$1"
  local human_name="$2"
  local need_install="0"

  case "$dep" in
    curl)
      has_cmd curl || need_install="1"
      ;;
    tar)
      has_cmd tar || need_install="1"
      ;;
    sha256)
      has_checksum_tool || need_install="1"
      ;;
  esac

  if [ "$need_install" = "0" ]; then
    return 0
  fi

  print_warn "Missing required dependency: ${human_name}"

  if [ -z "$PKG_MGR" ] && ! detect_package_manager; then
    print_error "No compatible package manager detected for this system."
    print_dependency_manual_help "$dep"
    return 1
  fi

  if ! can_auto_install_dependency "$dep"; then
    print_error "Cannot auto-install ${human_name} with ${PKG_MGR} on this system."
    print_dependency_manual_help "$dep"
    return 1
  fi

  if [ "$AUTO_APPROVE" != "1" ]; then
    if ! is_interactive; then
      print_error "Non-interactive mode without --yes and required dependencies are missing."
      print_info "Re-run with --yes to auto-install, or install dependencies manually and retry."
      return 1
    fi
    if ! confirm "Install ${human_name} with ${PKG_MGR}?" "n"; then
      print_error "Installation cancelled: ${human_name} is required."
      return 1
    fi
  fi

  if [ "$OS_TYPE" = "linux" ] && [ "$(id -u)" -ne 0 ] && [ "$PKG_MGR" != "brew" ] && ! has_cmd sudo; then
    print_error "sudo is not available and root privileges are required to install ${human_name}."
    return 1
  fi

  print_info "Installing ${human_name} with ${PKG_MGR}..."
  if ! install_dep_with_pkg_mgr "$dep"; then
    print_dependency_manual_help "$dep"
    return 1
  fi

  case "$dep" in
    curl)
      has_cmd curl || return 1
      ;;
    tar)
      has_cmd tar || return 1
      ;;
    sha256)
      has_checksum_tool || return 1
      ;;
  esac

  print_success "Dependency ready: ${human_name}"
  return 0
}

preflight_dependencies() {
  detect_platform
  detect_package_manager || true

  if [ "$OS_TYPE" = "macos" ]; then
    print_info "Detected system: macOS"
    if [ "$PKG_MGR" = "brew" ]; then
      print_info "Detected package manager: Homebrew"
    else
      print_warn "Homebrew not detected. Only compatible dependencies can be auto-installed."
    fi
  else
    print_info "Detected system: Linux${LINUX_DISTRO:+ (${LINUX_DISTRO})}"
    if [ -n "$PKG_MGR" ]; then
      print_info "Detected package manager: ${PKG_MGR}"
    else
      print_warn "apt-get, dnf, yum, and pacman were not detected."
    fi
  fi

  ensure_dependency "curl" "curl" || return 1
  ensure_dependency "tar" "tar" || return 1
  ensure_dependency "sha256" "sha256sum or shasum" || return 1
}

parse_args() {
  while [ "$#" -gt 0 ]; do
    case "$1" in
      --method)
        [ "$#" -ge 2 ] || die "Missing value for --method"
        INSTALL_METHOD="$2"
        shift 2
        ;;
      --version)
        [ "$#" -ge 2 ] || die "Missing value for --version"
        REQUESTED_VERSION="$2"
        shift 2
        ;;
      --install-dir)
        [ "$#" -ge 2 ] || die "Missing value for --install-dir"
        REQUESTED_INSTALL_DIR="$2"
        shift 2
        ;;
      --yes)
        AUTO_APPROVE="1"
        shift
        ;;
      --no-onboard)
        SKIP_ONBOARD="1"
        shift
        ;;
      --force-onboard)
        FORCE_ONBOARD="1"
        SKIP_ONBOARD="0"
        shift
        ;;
      --skip-checksum)
        SKIP_CHECKSUM="1"
        shift
        ;;
      -h|--help)
        usage
        exit 0
        ;;
      *)
        die "Unknown argument: $1 (use --help)"
        ;;
    esac
  done
}

validate_install_method() {
  case "$1" in
    binary|pnpm|npm|yarn|bun)
      return 0
      ;;
    *)
      return 1
      ;;
  esac
}

detect_binary_asset() {
  local os=""
  local arch=""

  os="$(uname -s | tr '[:upper:]' '[:lower:]')"
  arch="$(uname -m)"

  case "$arch" in
    x86_64|amd64)
      arch="x64"
      ;;
    arm64|aarch64)
      arch="arm64"
      ;;
    *)
      die "Unsupported architecture: $arch"
      ;;
  esac

  case "$os" in
    darwin)
      printf "corvus-darwin-%s" "$arch"
      ;;
    linux)
      printf "corvus-linux-%s" "$arch"
      ;;
    *)
      die "Unsupported OS: $os"
      ;;
  esac
}

normalize_release_tag() {
  local raw_tag="$1"

  [ -n "$raw_tag" ] || return 1

  if [ "$raw_tag" = "latest" ]; then
    printf "%s" "$raw_tag"
    return 0
  fi

  if printf "%s" "$raw_tag" | grep -Eq '^[0-9A-Za-z._-]+$'; then
    case "$raw_tag" in
      v*) printf "%s" "$raw_tag" ;;
      *) printf "v%s" "$raw_tag" ;;
    esac
    return 0
  fi

  return 1
}

resolve_install_dir() {
  if [ -n "$REQUESTED_INSTALL_DIR" ]; then
    printf "%s" "$REQUESTED_INSTALL_DIR"
    return
  fi

  if [ -n "$EXISTING_INSTALL_DIR" ] && [ "$EXISTING_INSTALL_METHOD" = "binary" ]; then
    printf "%s" "$EXISTING_INSTALL_DIR"
    return
  fi

  if [ "$(id -u)" -eq 0 ]; then
    printf "%s" "$DEFAULT_INSTALL_DIR_ROOT"
  else
    printf "%s" "$DEFAULT_INSTALL_DIR_USER"
  fi
}

ensure_install_dir_writable() {
  local install_dir="$1"

  mkdir -p "$install_dir" || die "Could not create install directory: $install_dir"
  [ -w "$install_dir" ] || die "Install directory is not writable: $install_dir"
}

compute_sha256() {
  local file_path="$1"

  if has_cmd sha256sum; then
    sha256sum "$file_path" | awk '{print $1}'
    return 0
  fi

  if has_cmd shasum; then
    shasum -a 256 "$file_path" | awk '{print $1}'
    return 0
  fi

  return 1
}

download_binary_from_releases() {
  local release_tag="$1"
  local asset_base="$2"
  local target_path="$3"
  local repo_slug=""
  local candidate_asset=""
  local candidate_assets=()
  local candidate_url=""
  local candidate_checksum_url=""
  local selected_repo=""
  local downloaded=0

  RESOLVED_DOWNLOAD_URL=""
  RESOLVED_CHECKSUM_URL=""
  RESOLVED_ASSET_NAME=""

  candidate_assets=(
    "${asset_base}.tar.gz"
    "${asset_base}"
  )

  for repo_slug in "$PRIMARY_REPO_SLUG" "$LEGACY_REPO_SLUG"; do
    for candidate_asset in "${candidate_assets[@]}"; do
      if [ "$release_tag" = "latest" ]; then
        candidate_url="https://github.com/${repo_slug}/releases/latest/download/${candidate_asset}"
      else
        candidate_url="https://github.com/${repo_slug}/releases/download/${release_tag}/${candidate_asset}"
      fi
      candidate_checksum_url="${candidate_url}.sha256"

      if curl --proto '=https' --tlsv1.2 -fsSL "$candidate_url" -o "$target_path" 2>/dev/null; then
        RESOLVED_DOWNLOAD_URL="$candidate_url"
        RESOLVED_CHECKSUM_URL="$candidate_checksum_url"
        RESOLVED_ASSET_NAME="$candidate_asset"
        selected_repo="$repo_slug"
        downloaded=1
        break
      fi
    done

    [ "$downloaded" -eq 1 ] && break
  done

  if [ -z "$RESOLVED_DOWNLOAD_URL" ]; then
    print_error "Could not download Corvus binary."
    print_error "Tried: ${PRIMARY_REPO_SLUG} and ${LEGACY_REPO_SLUG}"
    print_error "You can force a version with CORVUS_VERSION=<version>."
    return 1
  fi

  print_info "Downloading ${RESOLVED_DOWNLOAD_URL}"
  if [ "$selected_repo" != "$PRIMARY_REPO_SLUG" ]; then
    print_warn "Primary release source unavailable, using ${selected_repo}."
  fi
}

verify_checksum() {
  local file_path="$1"
  local checksum_url="$2"
  local expected_checksum=""
  local computed_checksum=""

  if [ "$SKIP_CHECKSUM" = "1" ]; then
    print_warn "Skipping checksum verification (--skip-checksum)."
    return 0
  fi

  print_info "Verifying checksum..."

  if ! expected_checksum="$(curl --proto '=https' --tlsv1.2 -fsSL "$checksum_url" 2>/dev/null | awk '{print $1}')"; then
    die "Could not download checksum file. Refusing insecure install."
  fi

  [ -n "$expected_checksum" ] || die "Checksum file is empty. Refusing insecure install."

  computed_checksum="$(compute_sha256 "$file_path" || true)"
  [ -n "$computed_checksum" ] || die "No SHA-256 tool found (sha256sum/shasum)."

  if [ "$computed_checksum" != "$expected_checksum" ]; then
    print_error "Checksum verification failed!"
    print_error "Expected: $expected_checksum"
    print_error "Computed: $computed_checksum"
    return 1
  fi

  print_success "Checksum verified successfully"
}

extract_or_copy_binary() {
  local download_path="$1"
  local asset_base="$2"
  local target_path="$3"
  local extract_dir="$4"
  local parent_return_trap="$5"
  local archive_entry=""
  local staged_path=""
  local source_path=""

  staged_path="$(mktemp "${target_path}.tmp.XXXXXX")"
  trap '
    if [ -n "${staged_path:-}" ] && [ -e "$staged_path" ]; then
      rm -f "$staged_path"
    fi
    if [ -n "${parent_return_trap:-}" ]; then
      trap "$parent_return_trap" RETURN
    else
      trap - RETURN
    fi
  ' RETURN

  if [[ "$RESOLVED_ASSET_NAME" == *.tar.gz ]]; then
    has_cmd tar || die "tar is required to extract ${RESOLVED_ASSET_NAME}"

    archive_entry="$(tar -tzf "$download_path" | grep -E "^(\./)?${asset_base}$" | head -n 1 || true)"
    [ -n "$archive_entry" ] || die "Archive does not contain expected binary: ${asset_base}"

    tar -xzf "$download_path" -C "$extract_dir" "$archive_entry"
    source_path="${extract_dir}/${archive_entry#./}"
  else
    source_path="$download_path"
  fi

  if has_cmd install; then
    install -m 755 "$source_path" "$staged_path"
  else
    cp "$source_path" "$staged_path"
    chmod 755 "$staged_path"
  fi
  mv -f "$staged_path" "$target_path"
  staged_path=""
}

install_via_binary() {
  local asset=""
  local release_tag=""
  local normalized_release_tag=""
  local install_dir=""
  local target_path=""
  local download_path=""
  local extract_dir=""

  asset="$(detect_binary_asset)"
  release_tag="$REQUESTED_VERSION"

  if ! normalized_release_tag="$(normalize_release_tag "$release_tag")"; then
    die "Invalid version value: $release_tag"
  fi

  install_dir="$(resolve_install_dir)"
  ensure_install_dir_writable "$install_dir"

  target_path="${install_dir}/corvus"
  download_path="$(mktemp "${TMPDIR:-/tmp}/corvus-download.XXXXXX")"
  extract_dir="$(mktemp -d "${TMPDIR:-/tmp}/corvus-extract.XXXXXX")"
  cleanup_install_artifacts() {
    if [ -n "${download_path:-}" ] && [ -e "$download_path" ]; then
      rm -f "$download_path"
    fi
    if [ -n "${extract_dir:-}" ] && [ -d "$extract_dir" ]; then
      rm -rf "$extract_dir"
    fi
  }
  trap cleanup_install_artifacts EXIT INT TERM

  download_binary_from_releases "$normalized_release_tag" "$asset" "$download_path"
  verify_checksum "$download_path" "$RESOLVED_CHECKSUM_URL"
  extract_or_copy_binary "$download_path" "$asset" "$target_path" "$extract_dir" ""

  INSTALLED_BINARY_PATH="$target_path"

  print_success "Corvus binary installed at ${target_path}"
}

install_via_package_manager() {
  case "$INSTALL_METHOD" in
    pnpm)
      has_cmd pnpm || die "pnpm is not installed."
      pnpm add --global --ignore-scripts "$CLI_PACKAGE"
      ;;
    npm)
      has_cmd npm || die "npm is not installed."
      npm install --global --ignore-scripts "$CLI_PACKAGE"
      ;;
    yarn)
      has_cmd yarn || die "yarn is not installed."
      local yarn_version=""
      local yarn_major=""
      yarn_version="$(yarn --version 2>/dev/null || true)"
      yarn_major="${yarn_version%%.*}"
      if [ -n "$yarn_version" ] && [ "$yarn_major" -ge 2 ] 2>/dev/null; then
        yarn dlx "$CLI_PACKAGE"
      else
        yarn global add "$CLI_PACKAGE"
      fi
      ;;
    bun)
      has_cmd bun || die "bun is not installed."
      bun add --global "$CLI_PACKAGE"
      ;;
    *)
      die "Unknown install method: $INSTALL_METHOD"
      ;;
  esac
}

select_install_method() {
  local candidates=()
  local method=""
  local choice=""
  local index=1

  if [ -n "$INSTALL_METHOD" ]; then
    validate_install_method "$INSTALL_METHOD" || die "Unsupported method: $INSTALL_METHOD"
    return
  fi

  if [ "$ALREADY_INSTALLED" = "1" ] && [ "$EXISTING_INSTALL_METHOD" = "binary" ]; then
    INSTALL_METHOD="binary"
    print_info "Existing Corvus install detected at ${EXISTING_CMD_PATH}. Updating in place."
    return
  fi

  if [ "$ALREADY_INSTALLED" = "1" ] && [ -n "$EXISTING_INSTALL_METHOD" ] && [ "$EXISTING_INSTALL_METHOD" != "binary" ]; then
    case "$EXISTING_INSTALL_METHOD" in
      pnpm|npm|yarn|bun)
        INSTALL_METHOD="$EXISTING_INSTALL_METHOD"
        print_info "Existing Corvus install detected at ${EXISTING_CMD_PATH}."
        return
        ;;
      *)
        print_info "Existing Corvus install detected at ${EXISTING_CMD_PATH}."
        ;;
    esac
  fi

  candidates=("binary")
  has_cmd pnpm && candidates+=("pnpm")
  has_cmd npm && candidates+=("npm")
  has_cmd yarn && candidates+=("yarn")
  has_cmd bun && candidates+=("bun")

  if ! is_interactive; then
    if [ "$ALREADY_INSTALLED" = "1" ] && [ -n "$EXISTING_INSTALL_METHOD" ] && [ "$EXISTING_INSTALL_METHOD" != "binary" ]; then
      case "$EXISTING_INSTALL_METHOD" in
        pnpm|npm|yarn|bun)
          INSTALL_METHOD="$EXISTING_INSTALL_METHOD"
          ;;
        *)
          INSTALL_METHOD="binary"
          ;;
      esac
    else
      INSTALL_METHOD="binary"
    fi
    return
  fi

  print_info "Choose installation method:"
  for method in "${candidates[@]}"; do
    printf "  %s) %s\n" "$index" "$method"
    index=$((index + 1))
  done

  read -r -p "Method [1]: " choice || true
  choice="${choice:-1}"

  if [ "$choice" -ge 1 ] && [ "$choice" -lt "$index" ] 2>/dev/null; then
    INSTALL_METHOD="${candidates[$((choice - 1))]}"
  else
    print_warn "Invalid selection, using binary"
    INSTALL_METHOD="binary"
  fi
}

print_path_hint() {
  local install_dir="$1"
  local shell_name=""

  if printf "%s" ":$PATH:" | grep -q ":${install_dir}:"; then
    return
  fi

  shell_name="$(basename "${SHELL:-bash}")"

  print_warn "${install_dir} is not in PATH for this shell."
  print_warn "Add it now with: export PATH=\"${install_dir}:\$PATH\""

  case "$shell_name" in
    zsh)
      print_info "Persist for future sessions: echo 'export PATH=\"${install_dir}:\$PATH\"' >> ~/.zshrc"
      ;;
    *)
      print_info "Persist for future sessions: echo 'export PATH=\"${install_dir}:\$PATH\"' >> ~/.bashrc"
      ;;
  esac
}

post_install_steps() {
  local resolved_cmd=""
  local resolved_version=""

  if [ -n "$INSTALLED_BINARY_PATH" ] && [ -x "$INSTALLED_BINARY_PATH" ]; then
    resolved_cmd="$INSTALLED_BINARY_PATH"
  elif has_cmd corvus; then
    resolved_cmd="$(command -v corvus)"
  fi

  if [ -z "$resolved_cmd" ]; then
    print_warn "corvus command not found in PATH yet."
    print_warn "Open a new terminal and run: corvus --help"
    return
  fi

  resolved_version="$("$resolved_cmd" --version 2>/dev/null || echo 'installed')"
  print_success "Corvus CLI available: ${resolved_version}"

  if [ "$SKIP_ONBOARD" = "1" ]; then
    if [ -n "$SKIP_ONBOARD_REASON" ]; then
      print_info "Onboarding skipped (${SKIP_ONBOARD_REASON})."
    else
      print_info "Onboarding skipped (--no-onboard or CORVUS_SKIP_ONBOARD=1)."
    fi
    return
  fi

  if has_tty; then
    print_info "Launching interactive onboarding..."
    "$resolved_cmd" onboard --interactive </dev/tty || print_warn "Onboarding exited with a non-zero status."
  else
    print_warn "No TTY available to run interactive onboarding automatically."
    print_info "Run it later with: corvus onboard --interactive"
  fi
}

restart_daemon_if_available() {
  local resolved_cmd=""

  if [ -n "$INSTALLED_BINARY_PATH" ] && [ -x "$INSTALLED_BINARY_PATH" ]; then
    resolved_cmd="$INSTALLED_BINARY_PATH"
  elif has_cmd corvus; then
    resolved_cmd="$(command -v corvus)"
  fi

  if [ -z "$resolved_cmd" ]; then
    return 0
  fi

  if "$resolved_cmd" service status >/dev/null 2>&1; then
    print_info "Restarting Corvus service to apply updates..."
    if "$resolved_cmd" service restart >/dev/null 2>&1; then
      if "$resolved_cmd" doctor >/dev/null 2>&1; then
        print_success "Corvus service restarted"
      else
        print_warn "Corvus service restart completed, but health check failed"
        return 1
      fi
    else
      print_warn "Corvus service restart failed. Try: corvus service restart"
      return 1
    fi
  else
    print_info "Corvus service not installed; skipping restart."
  fi
}

main() {
  require_supported_shell
  parse_args "$@"
  detect_existing_install

  print_info "Corvus installer"
  print_info "Security note: HTTPS only + checksum verification enabled by default."
  print_preflight_notice
  preflight_dependencies || die "Preflight failed: required dependencies could not be prepared."

  select_install_method
  print_info "Using install method: ${INSTALL_METHOD}"

  case "$INSTALL_METHOD" in
    binary)
      install_via_binary
      print_path_hint "$(dirname "$INSTALLED_BINARY_PATH")"
      ;;
    pnpm|npm|yarn|bun)
      install_via_package_manager
      ;;
    *)
      die "Unsupported method: $INSTALL_METHOD"
      ;;
  esac

  post_install_steps
  restart_daemon_if_available
  print_success "Done. Try: corvus status"
}

main "$@"
