@echo off
setlocal
cd /d "%~dp0"

if not defined FIELDREPLAY_BOOTSTRAP_OUT set "FIELDREPLAY_BOOTSTRAP_OUT=%CD%"

set "FIELDREPLAY_BUNDLED_PYTHON=%~dp0runtime\python\python.exe"
if exist "%FIELDREPLAY_BUNDLED_PYTHON%" goto run_with_bundled_python

where py >nul 2>nul
if %errorlevel%==0 (
  py -3 scripts\bestperf_bootstrap.py
  goto done
)

where python >nul 2>nul
if %errorlevel%==0 (
  python scripts\bestperf_bootstrap.py
  goto done
)

goto python_missing

:run_with_bundled_python
"%FIELDREPLAY_BUNDLED_PYTHON%" scripts\bestperf_bootstrap.py
goto done

:python_missing
if not exist "%FIELDREPLAY_BOOTSTRAP_OUT%" mkdir "%FIELDREPLAY_BOOTSTRAP_OUT%" >nul 2>nul
set "FIELDREPLAY_REPORT=%FIELDREPLAY_BOOTSTRAP_OUT%\bootstrap_report.latest.json"
set "FIELDREPLAY_SUMMARY=%FIELDREPLAY_BOOTSTRAP_OUT%\bootstrap_summary.latest.txt"
set "FIELDREPLAY_LOG=%FIELDREPLAY_BOOTSTRAP_OUT%\bootstrap.latest.log"

> "%FIELDREPLAY_REPORT%" echo {
>> "%FIELDREPLAY_REPORT%" echo   "schema": "field_replay.bootstrap_report.v1",
>> "%FIELDREPLAY_REPORT%" echo   "status": "blocked",
>> "%FIELDREPLAY_REPORT%" echo   "dry_run": false,
>> "%FIELDREPLAY_REPORT%" echo   "target": {
>> "%FIELDREPLAY_REPORT%" echo     "latest_report_path": "bootstrap_report.latest.json",
>> "%FIELDREPLAY_REPORT%" echo     "latest_summary_path": "bootstrap_summary.latest.txt",
>> "%FIELDREPLAY_REPORT%" echo     "latest_log_path": "bootstrap.latest.log"
>> "%FIELDREPLAY_REPORT%" echo   },
>> "%FIELDREPLAY_REPORT%" echo   "checks": [
>> "%FIELDREPLAY_REPORT%" echo     {"id": "python", "label": "Python", "status": "fail", "detail": "Python executable not found", "action": "Install Python 3.9 or newer, then run BestPerf again"}
>> "%FIELDREPLAY_REPORT%" echo   ],
>> "%FIELDREPLAY_REPORT%" echo   "steps": [
>> "%FIELDREPLAY_REPORT%" echo     {"id": "start_controller", "label": "Start GUI controller", "status": "fail", "detail": "Python executable not found"}
>> "%FIELDREPLAY_REPORT%" echo   ],
>> "%FIELDREPLAY_REPORT%" echo   "action_plan": [
>> "%FIELDREPLAY_REPORT%" echo     {"id": "fix_python", "label": "Install Python", "severity": "blocker", "action": "Install Python 3.9 or newer, then run BestPerf again"}
>> "%FIELDREPLAY_REPORT%" echo   ]
>> "%FIELDREPLAY_REPORT%" echo }

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

> "%FIELDREPLAY_LOG%" echo Python 3.9 or newer is required.
>> "%FIELDREPLAY_LOG%" echo Neither py nor python was found in PATH.
>> "%FIELDREPLAY_LOG%" echo Install Python 3.9 or newer, then run BestPerf again.

echo Python 3.9 or newer is required. Install Python, then run BestPerf again.
echo.
echo Startup report: %FIELDREPLAY_REPORT%
echo Startup summary: %FIELDREPLAY_SUMMARY%
echo Startup log:    %FIELDREPLAY_LOG%
pause
exit /b 2

:done
if errorlevel 1 pause
