#!/usr/bin/env bash
# deploy/lib/install.sh — 一键部署
set -e
set -u
if [ -n "${BASH_VERSION:-}" ]; then
  set -o pipefail
fi

LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=common.sh
source "${LIB_DIR}/common.sh"

install_fresh() {
  step "克隆仓库"
  if [[ -d "${INSTALL_ROOT}" ]]; then
    die "目录已存在: ${INSTALL_ROOT},请选修复环境或深度重装"
  fi
  mkdir -p "$(dirname "${INSTALL_ROOT}")"
  git clone -b "${GIT_BRANCH}" "${GIT_URL}" "${INSTALL_ROOT}"
}

install_repair() {
  step "修复环境(保留数据与配置)"
  bash "${REPO_ROOT}/deploy/setup_env.sh" --install-system-deps
}

install_deep() {
  step "深度重装(保留 .env,清库)"
  bash "${REPO_ROOT}/deploy/reinstall.sh" --yes
  verify_deployment "$(detect_server_ip)" || true
  print_post_install_guide "$(detect_server_ip)"
}

run_install_pipeline() {
  step "环境部署 setup_env.sh"
  bash "${REPO_ROOT}/deploy/setup_env.sh" --install-system-deps

  step "启动 PM2 全部进程"
  bash "${REPO_ROOT}/deploy/pm2_start_all.sh"

  pm2_save_startup
  install_backup_cron_all

  verify_deployment "$(detect_server_ip)" || true
  print_post_install_guide "$(detect_server_ip)"
}

handle_existing_install() {
  echo ""
  echo "检测到已部署安装: ${INSTALL_ROOT}"
  echo "  a) 取消"
  echo "  b) 修复环境(重建 venv,保留 .env 与数据库)"
  echo "  c) 深度重装(保留 .env,清库,见 reinstall.sh)"
  local choice=""
  cm_read choice "请选择 [a/b/c]: "
  case "${choice}" in
    b|B)
      install_repair
      if command -v pm2 >/dev/null 2>&1; then
        bash "${REPO_ROOT}/deploy/pm2_start_all.sh" 2>/dev/null || pm2 restart all 2>/dev/null || true
        pm2_save_startup
      fi
      verify_deployment "$(detect_server_ip)" || true
      print_post_install_guide "$(detect_server_ip)"
      ;;
    c|C)
      install_deep
      ;;
    *)
      log "已取消"
      ;;
  esac
}

main_install() {
  require_root
  require_ubuntu

  if repo_ready "${INSTALL_ROOT}"; then
    REPO_ROOT="${INSTALL_ROOT}"
  elif repo_ready "${REPO_ROOT}"; then
    :
  else
    REPO_ROOT=""
  fi

  if [[ -n "${REPO_ROOT}" ]] && is_deployed "${REPO_ROOT}"; then
    handle_existing_install
    return 0
  fi

  if [[ -z "${REPO_ROOT}" ]]; then
    install_system_packages
    install_node_pm2
    install_fresh
    REPO_ROOT="${INSTALL_ROOT}"
  else
    install_system_packages
    install_node_pm2
  fi

  run_install_pipeline
}

main_install "$@"
