#!/usr/bin/env bash

set -euo pipefail

show_help() {
  cat <<'EOF'
Usage: moviepilot [BOOTSTRAP COMMAND] | [RUNTIME COMMAND]
       moviepilot help [COMMAND ...]
       moviepilot commands

Bootstrap Commands:
  moviepilot install deps [--python PYTHON] [--venv PATH] [--recreate] [--config-dir PATH]
  moviepilot install frontend [--version latest] [--node-version 20.12.1] [--config-dir PATH]
  moviepilot install resources [--resources-repo PATH] [--resource-dir PATH] [--config-dir PATH]
  moviepilot init [--skip-resources] [--force-token] [--wizard] [--config-dir PATH]
  moviepilot setup [--python PYTHON] [--venv PATH] [--recreate] [--frontend-version latest] [--node-version 20.12.1] [--wizard] [--config-dir PATH]
  moviepilot update {backend|frontend|all} [OPTIONS]
  moviepilot agent [OPTIONS] MESSAGE...

Runtime Commands:
  moviepilot start|stop|restart|status|logs|version
  moviepilot config ...
  moviepilot tool ...
  moviepilot scheduler ...

Discovery Commands:
  moviepilot help
  moviepilot help config
  moviepilot help install
  moviepilot help update
  moviepilot commands

Examples:
  moviepilot install deps
  moviepilot install frontend
  moviepilot install resources
  moviepilot setup --wizard
  moviepilot update all
  moviepilot agent 帮我分析最近一次搜索失败
  moviepilot help config
  moviepilot config keys
  moviepilot start
  moviepilot tool list
EOF
}

show_commands() {
  cat <<'EOF'
Bootstrap Commands
  install deps
  install frontend
  install resources
  init
  setup
  update backend
  update frontend
  update all
  agent

Runtime Commands
  start
  stop
  restart
  status
  logs
  version
  config path
  config list
  config get
  config set
  config keys
  config describe
  tool list
  tool show
  tool run
  scheduler list
  scheduler run

Discovery Commands
  help
  commands
EOF
}

show_install_help() {
  cat <<'EOF'
Usage:
  moviepilot install deps [OPTIONS]
  moviepilot install frontend [OPTIONS]
  moviepilot install resources [OPTIONS]

Options:
  deps:
    --python PYTHON   用于创建虚拟环境的 Python 解释器
    --venv PATH       虚拟环境目录，默认 ./venv
    --recreate        删除并重建虚拟环境
    --config-dir PATH 指定配置目录

  frontend:
    --version TAG         前端版本，默认 latest
    --node-version VER    本地 Node 运行时版本，默认 20.12.1
    --config-dir PATH     指定配置目录

  resources:
    --resources-repo PATH 本地 MoviePilot-Resources 仓库路径
    --resource-dir PATH   直接指定 resources.v2 目录
    --config-dir PATH     指定配置目录

  -h, --help              显示帮助
EOF
}

show_init_help() {
  cat <<'EOF'
Usage: moviepilot init [OPTIONS]

Options:
  --skip-resources          跳过资源同步
  --force-token             强制重置 API_TOKEN
  --wizard                  启动交互式初始化向导
  --config-dir PATH         指定配置目录
  -h, --help                显示帮助
EOF
}

show_setup_help() {
  cat <<'EOF'
Usage: moviepilot setup [OPTIONS]

Options:
  --python PYTHON           用于创建虚拟环境的 Python 解释器
  --venv PATH               虚拟环境目录，默认 ./venv
  --recreate                删除并重建虚拟环境
  --frontend-version TAG    前端版本，默认 latest
  --node-version VER        本地 Node 运行时版本，默认 20.12.1
  --skip-resources          跳过资源同步
  --force-token             强制重置 API_TOKEN
  --wizard                  安装完成后启动交互式初始化向导
  --config-dir PATH         指定配置目录
  -h, --help                显示帮助
EOF
}

show_update_help() {
  cat <<'EOF'
Usage:
  moviepilot update backend [OPTIONS]
  moviepilot update frontend [OPTIONS]
  moviepilot update all [OPTIONS]

Options:
  --ref REF                 后端 Git 版本，默认 latest
  --frontend-version TAG    前端版本，默认 latest
  --node-version VER        本地 Node 运行时版本，默认 20.12.1
  --python PYTHON           用于安装后端依赖的 Python 解释器
  --venv PATH               虚拟环境目录，默认 ./venv
  --recreate                删除并重建虚拟环境
  --skip-resources          更新 all 时跳过资源同步
  --config-dir PATH         指定配置目录
  -h, --help                显示帮助
EOF
}

show_agent_help() {
  cat <<'EOF'
Usage:
  moviepilot agent [OPTIONS] MESSAGE...

Options:
  --session ID              指定会话 ID
  --new-session             强制创建新会话
  --user-id ID              智能体上下文中的用户 ID，默认 cli
  --config-dir PATH         指定配置目录
  -h, --help                显示帮助
EOF
}

python_version_ok() {
  local python_bin="$1"
  "$python_bin" - <<'PY' >/dev/null 2>&1
import sys
raise SystemExit(0 if sys.version_info >= (3, 12) else 1)
PY
}

find_system_python() {
  if command -v python3 >/dev/null 2>&1; then
    local python3_bin
    python3_bin="$(command -v python3)"
    if python_version_ok "$python3_bin"; then
      printf '%s\n' "$python3_bin"
      return 0
    fi
  fi
  if command -v python >/dev/null 2>&1; then
    local python_bin
    python_bin="$(command -v python)"
    if python_version_ok "$python_bin"; then
      printf '%s\n' "$python_bin"
      return 0
    fi
  fi
  local uv_bin
  for uv_bin in "$(command -v uv 2>/dev/null || true)" "$HOME/.local/bin/uv"; do
    if [ -n "$uv_bin" ] && [ -x "$uv_bin" ]; then
      if "$uv_bin" python find 3.12 >/dev/null 2>&1; then
        "$uv_bin" python find 3.12
        return 0
      fi
    fi
  done
  return 1
}

default_config_dir() {
  case "$(uname -s)" in
    Darwin)
      printf '%s\n' "$HOME/Library/Application Support/MoviePilot"
      ;;
    *)
      printf '%s\n' "${XDG_CONFIG_HOME:-$HOME/.config}/moviepilot"
      ;;
  esac
}

legacy_config_exists() {
  local legacy_dir="$ROOT/config"
  [[ -f "$legacy_dir/app.env" ]] || [[ -f "$legacy_dir/user.db" ]] || [[ -d "$legacy_dir/logs" ]] || [[ -d "$legacy_dir/temp" ]] || [[ -d "$legacy_dir/cache" ]] || [[ -d "$legacy_dir/cookies" ]] || [[ -d "$legacy_dir/sites" ]]
}

run_runtime_cli() {
  if [ ! -x "$VENV_PYTHON" ]; then
    echo "未找到项目虚拟环境，请先执行 moviepilot install deps 或 moviepilot setup" >&2
    exit 1
  fi
  exec "$VENV_PYTHON" -m app.cli "$@"
}

show_command_help() {
  case "${1:-}" in
    ""|-h|--help|help)
      show_help
      exit 0
      ;;
    install)
      shift
      case "${1:-}" in
        ""|deps|-h|--help)
          show_install_help
          exit 0
          ;;
        frontend)
          show_install_help
          exit 0
          ;;
        resources)
          show_install_help
          exit 0
          ;;
        *)
          echo "仅支持：moviepilot help install、moviepilot help install deps、moviepilot help install frontend、moviepilot help install resources" >&2
          exit 1
          ;;
      esac
      ;;
    init)
      show_init_help
      exit 0
      ;;
    setup)
      show_setup_help
      exit 0
      ;;
    agent)
      show_agent_help
      exit 0
      ;;
    update)
      show_update_help
      exit 0
      ;;
    commands)
      show_commands
      exit 0
      ;;
    *)
      run_runtime_cli "$@" --help
      ;;
  esac
}

SOURCE="${BASH_SOURCE[0]}"
while [ -L "$SOURCE" ]; do
  SOURCE_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
  SOURCE_TARGET="$(readlink "$SOURCE")"
  if [[ "$SOURCE_TARGET" != /* ]]; then
    SOURCE="$SOURCE_DIR/$SOURCE_TARGET"
  else
    SOURCE="$SOURCE_TARGET"
  fi
done

ROOT="$(cd -P "$(dirname "$SOURCE")" && pwd)"
VENV_PYTHON="$ROOT/venv/bin/python"
SETUP_SCRIPT="$ROOT/scripts/local_setup.py"

if [ -z "${CONFIG_DIR:-}" ] && [ -f "$ROOT/.moviepilot.env" ]; then
  # shellcheck disable=SC1090
  . "$ROOT/.moviepilot.env"
fi

if [ -z "${CONFIG_DIR:-}" ]; then
  if legacy_config_exists; then
    CONFIG_DIR="$ROOT/config"
  else
    CONFIG_DIR="$(default_config_dir)"
  fi
fi
export CONFIG_DIR

BOOTSTRAP_PYTHON=""
if [ -x "$VENV_PYTHON" ]; then
  BOOTSTRAP_PYTHON="$VENV_PYTHON"
else
  BOOTSTRAP_PYTHON="$(find_system_python || true)"
fi

cd "$ROOT"

case "${1:-}" in
  ""|-h|--help|help)
    if [ "${1:-}" = "help" ]; then
      shift
      show_command_help "$@"
    fi
    show_help
    exit 0
    ;;
  commands)
    show_commands
    exit 0
    ;;
  install)
    shift
    if [ -z "$BOOTSTRAP_PYTHON" ]; then
      echo "未找到可用的 Python 解释器，请先安装 Python 3" >&2
      exit 1
    fi
    case "${1:-}" in
      deps)
        shift
        exec "$BOOTSTRAP_PYTHON" "$SETUP_SCRIPT" install-deps "$@"
        ;;
      frontend)
        shift
        exec "$BOOTSTRAP_PYTHON" "$SETUP_SCRIPT" install-frontend "$@"
        ;;
      resources)
        shift
        exec "$BOOTSTRAP_PYTHON" "$SETUP_SCRIPT" install-resources "$@"
        ;;
      *)
        echo "支持的命令：moviepilot install deps|frontend|resources" >&2
        exit 1
        ;;
    esac
    ;;
  init)
    shift
    if [ -z "$BOOTSTRAP_PYTHON" ]; then
      echo "未找到可用的 Python 解释器，请先安装 Python 3" >&2
      exit 1
    fi
    exec "$BOOTSTRAP_PYTHON" "$SETUP_SCRIPT" init "$@"
    ;;
  setup)
    shift
    if [ -z "$BOOTSTRAP_PYTHON" ]; then
      echo "未找到可用的 Python 解释器，请先安装 Python 3" >&2
      exit 1
    fi
    exec "$BOOTSTRAP_PYTHON" "$SETUP_SCRIPT" setup "$@"
    ;;
  update)
    shift
    if [ -z "$BOOTSTRAP_PYTHON" ]; then
      echo "未找到可用的 Python 解释器，请先安装 Python 3" >&2
      exit 1
    fi
    exec "$BOOTSTRAP_PYTHON" "$SETUP_SCRIPT" update "$@"
    ;;
  agent)
    shift
    if [ -z "$BOOTSTRAP_PYTHON" ]; then
      echo "未找到可用的 Python 解释器，请先安装 Python 3" >&2
      exit 1
    fi
    exec "$BOOTSTRAP_PYTHON" "$SETUP_SCRIPT" agent "$@"
    ;;
esac

if [ ! -x "$VENV_PYTHON" ]; then
  echo "未找到项目虚拟环境，请先执行 moviepilot install deps 或 moviepilot setup" >&2
  exit 1
fi

exec "$VENV_PYTHON" -m app.cli "$@"
