#!/usr/bin/env bash

bestperf_bundled_python() {
  for candidate in \
    "$PWD/runtime/python/bin/python3" \
    "$PWD/runtime/python/bin/python" \
    "$PWD/runtime/python/python3" \
    "$PWD/runtime/python/python"; do
    if [[ -x "$candidate" ]]; then
      printf '%s\n' "$candidate"
      return 0
    fi
  done
  return 1
}

bestperf_python() {
  local bundled_python
  bundled_python="$(bestperf_bundled_python || true)"
  if [[ -n "$bundled_python" ]]; then
    printf '%s\n' "$bundled_python"
    return 0
  fi
  if command -v python3 >/dev/null 2>&1; then
    command -v python3
    return 0
  fi
  if command -v python >/dev/null 2>&1; then
    command -v python
    return 0
  fi
  return 1
}

bestperf_write_python_missing_report() {
  local dry_run="$1"
  local action="$2"
  local report_path="$FIELDREPLAY_BOOTSTRAP_OUT/bootstrap_report.latest.json"
  local summary_path="$FIELDREPLAY_BOOTSTRAP_OUT/bootstrap_summary.latest.txt"
  local log_path="$FIELDREPLAY_BOOTSTRAP_OUT/bootstrap.latest.log"

  /bin/mkdir -p "$FIELDREPLAY_BOOTSTRAP_OUT"

  {
    printf '{\n'
    printf '  "schema": "field_replay.bootstrap_report.v1",\n'
    printf '  "status": "blocked",\n'
    printf '  "dry_run": %s,\n' "$dry_run"
    printf '  "target": {\n'
    printf '    "latest_report_path": "bootstrap_report.latest.json",\n'
    printf '    "latest_summary_path": "bootstrap_summary.latest.txt",\n'
    printf '    "latest_log_path": "bootstrap.latest.log"\n'
    printf '  },\n'
    printf '  "checks": [\n'
    printf '    {"id": "python", "label": "Python", "status": "fail", "detail": "Python executable not found", "action": "%s"}\n' "$action"
    printf '  ],\n'
    printf '  "steps": [\n'
    printf '    {"id": "start_controller", "label": "Start GUI controller", "status": "fail", "detail": "Python executable not found"}\n'
    printf '  ],\n'
    printf '  "action_plan": [\n'
    printf '    {"id": "fix_python", "label": "Install Python", "severity": "blocker", "action": "%s"}\n' "$action"
    printf '  ]\n'
    printf '}\n'
  } > "$report_path"

  {
    printf 'BestPerf 启动摘要\n\n'
    printf '状态: 启动失败\n'
    printf '普通用户只看这一段:\n'
    printf '1. 安装 Python 3.9 或更新版本。\n'
    printf '2. 重新双击 BestPerf 启动入口。\n'
    printf '3. 如果还是失败，把整个文件夹压缩后连同截图发给支持人员。\n\n'
    printf '问题原因: Python\n'
    printf '支持人员再看:\n'
    printf '给支持人员: bootstrap_report.latest.json 和 bootstrap.latest.log\n'
    printf 'BestPerf Startup Summary\n\n'
    printf 'Status: blocked\n'
    printf 'Next action: Install Python\n'
    printf 'Next action detail: %s\n\n' "$action"
    printf 'Controller URL: unavailable\n'
    printf 'Report: %s\n' "$report_path"
    printf 'Log: %s\n\n' "$log_path"
    printf 'Checks: pass=0 warn=0 fail=1\n'
    printf 'Steps: pass=0 warn=0 fail=1\n'
    printf 'First issue: Python: Python executable not found\n'
  } > "$summary_path"

  {
    printf 'Python 3.9 or newer is required.\n'
    printf 'Neither python3 nor python was found in PATH.\n'
    printf '%s\n' "$action"
  } > "$log_path"

  FIELDREPLAY_LAUNCHER_REPORT="$report_path"
  FIELDREPLAY_LAUNCHER_SUMMARY="$summary_path"
  FIELDREPLAY_LAUNCHER_LOG="$log_path"
}
