#!/bin/bash
# omarchy:summary=First-run wizard: language, timezone, scale, Chinese env, mirrors, apps, AI
# omarchy:examples=omarchycn setup

set -euo pipefail

if [[ ! -t 0 ]]; then
  echo "omarchycn setup 需要交互终端" >&2
  exit 1
fi

STATE_DIR="$HOME/.local/state/omarchycn/setup-done"
mkdir -p "$STATE_DIR"

run_step() {
  local step="$1" title="$2"
  shift 2

  if [[ -f $STATE_DIR/$step ]]; then
    gum confirm "「$title」已完成，重新运行？" --default=false || return 0
  elif ! gum confirm "运行「$title」？（可跳过，稍后重进）"; then
    return 0
  fi
  echo "==> $title"
  if "$@"; then
    touch "$STATE_DIR/$step"
  else
    gum confirm "「$title」失败，继续后面的步骤？" || exit 1
  fi
}

step_language() {
  local choice env_file="$HOME/.config/environment.d/90-omarchycn-lang.conf"
  choice=$(gum choose --header "界面语言 LANG" "zh_CN.UTF-8" "en_US.UTF-8" "保持当前")
  if [[ $choice == "保持当前" ]]; then
    return 0
  fi
  mkdir -p "${env_file%/*}"
  echo "LANG=$choice" > "$env_file"
  echo "LANG=$choice 写入 $env_file（重登录生效）"
}

step_timezone() {
  local choice
  choice=$(gum choose --header "时区" "Asia/Shanghai" "保持当前" "手动输入")
  if [[ $choice == "保持当前" ]]; then
    return 0
  fi
  if [[ $choice == "手动输入" ]]; then
    choice=$(timedatectl list-timezones | gum filter --header "选择时区")
  fi
  sudo timedatectl set-timezone "$choice"
  echo "时区: $(timedatectl show -p Timezone --value)"
}

step_scale() {
  local choice
  choice=$(gum choose --header "显示缩放" "1.0" "1.25" "1.5" "1.6" "1.75" "2.0" "保持当前")
  if [[ $choice == "保持当前" ]]; then
    return 0
  fi
  omarchy-cn-display-scale "$choice"
}

step_hotkey() {
  local choice
  choice=$(gum choose --header "输入法切换键" "ctrl-space" "super-space")
  omarchy-cn-ime-hotkey "$choice"
}

step_mirror() {
  local choice
  choice=$(gum choose --header "pacman 镜像策略" "china" "official")
  omarchy-cn-mirror-apply "$choice"
}

step_dev_mirror() {
  local choice
  choice=$(gum choose --header "开发工具镜像（npm/pip/cargo/go/gem）" "china" "official")
  omarchy-cn-dev-mirror-apply "$choice" --target npm,pip,cargo,go,gem
}

step_apps() {
  local catalog picks app rc=0
  catalog=$(omarchy-cn-app-list | tail -n +2 | awk '{print $1}') || return 1
  picks=$(gum choose --no-limit --header "选择要安装的国内应用（空格多选，回车确认）" <<<"$catalog" || true)
  for app in $picks; do
    omarchy-cn-app-install "$app" || rc=1
  done
  return $rc
}

step_privacy() {
  echo "隐私状态（无可配置项，如实告知）："
  echo "  - OmarchyCN 不含遥测代码，默认无任何数据上报"
  echo "  - 诊断信息仅在你手动运行 doctor 时本地生成，不上传"
  gum confirm "已了解" --affirmative "了解" --negative "" || true
}

run_step language "语言 Language" step_language
run_step timezone "时区 Timezone" step_timezone
run_step scale "显示缩放 Display Scale" step_scale
run_step locale "中文 Locale 生成" omarchy-cn-locale-apply
run_step fonts "中文字体与 fallback" omarchy-cn-font-apply
run_step ime "Fcitx5 + Rime 输入法" omarchy-cn-ime-apply
run_step hotkey "输入法切换键" step_hotkey
run_step mirror "pacman 镜像" step_mirror
run_step dev-mirror "开发工具镜像" step_dev_mirror
run_step apps "国内应用" step_apps
run_step ai "AI Hub（Harness/Provider/Key）" omarchy-cn-ai-setup
run_step privacy "隐私与诊断" step_privacy

echo
echo "==> 最终检查"
if omarchy-cn-doctor all; then
  echo "OmarchyCN 初始化完成。随时可用 omarchycn setup 重进任一步骤。"
else
  echo "初始化步骤已执行，但诊断存在失败项（见上），修复后可重跑 omarchycn setup" >&2
  exit 1
fi
