@echo off
setlocal
chcp 65001 >nul 2>nul
cd /d "%~dp0"

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

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

py -3 --version >nul 2>nul
if %errorlevel%==0 goto run_with_py

python --version >nul 2>nul
if %errorlevel%==0 goto run_with_python

if not exist "%FIELDREPLAY_INSTALL_OUT%" mkdir "%FIELDREPLAY_INSTALL_OUT%" >nul 2>nul
set "FIELDREPLAY_REPORT=%FIELDREPLAY_INSTALL_OUT%\install_report.latest.json"
set "FIELDREPLAY_SUMMARY=%FIELDREPLAY_INSTALL_OUT%\install_summary.latest.txt"
set "FIELDREPLAY_SUMMARY_HTML=%FIELDREPLAY_INSTALL_OUT%\install_summary.latest.html"
set "FIELDREPLAY_LOG=%FIELDREPLAY_INSTALL_OUT%\install.latest.log"

> "%FIELDREPLAY_REPORT%" echo {
>> "%FIELDREPLAY_REPORT%" echo   "schema": "field_replay.install_report.v1",
>> "%FIELDREPLAY_REPORT%" echo   "status": "failed",
>> "%FIELDREPLAY_REPORT%" echo   "error": "Python executable not found",
>> "%FIELDREPLAY_REPORT%" echo   "summary_path": "install_summary.latest.txt",
>> "%FIELDREPLAY_REPORT%" echo   "summary_html_path": "install_summary.latest.html",
>> "%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 Install 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. 重新双击 Install BestPerf。
>> "%FIELDREPLAY_SUMMARY%" echo 3. 如果还是失败，把整个文件夹压缩后连同截图发给支持人员。
>> "%FIELDREPLAY_SUMMARY%" echo 问题原因: Python
>> "%FIELDREPLAY_SUMMARY%" echo.
>> "%FIELDREPLAY_SUMMARY%" echo 支持人员再看:
>> "%FIELDREPLAY_SUMMARY%" echo 给支持人员: install_report.latest.json 和 install.latest.log
>> "%FIELDREPLAY_SUMMARY%" echo.
>> "%FIELDREPLAY_SUMMARY%" echo BestPerf Install Summary
>> "%FIELDREPLAY_SUMMARY%" echo.
>> "%FIELDREPLAY_SUMMARY%" echo Status: failed
>> "%FIELDREPLAY_SUMMARY%" echo Next action: Install Python
>> "%FIELDREPLAY_SUMMARY%" echo Next action detail: Install Python 3.9 or newer, then run Install BestPerf again.
>> "%FIELDREPLAY_SUMMARY%" echo.
>> "%FIELDREPLAY_SUMMARY%" echo Install dir: unavailable
>> "%FIELDREPLAY_SUMMARY%" echo Desktop: unavailable
>> "%FIELDREPLAY_SUMMARY%" echo Desktop entries:
>> "%FIELDREPLAY_SUMMARY%" echo - none
>> "%FIELDREPLAY_SUMMARY%" echo.
>> "%FIELDREPLAY_SUMMARY%" echo Checks: pass=0 warn=0 fail=1
>> "%FIELDREPLAY_SUMMARY%" echo First issue: Python executable not found
> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<!doctype html^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<html lang="zh-CN"^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<head^>^<meta charset="utf-8"^>^<meta name="viewport" content="width=device-width, initial-scale=1"^>^<title^>BestPerf 安装结果^</title^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<style^>body { margin: 0; background: #f5f7fa; color: #17212b; font-family: "Segoe UI", sans-serif; line-height: 1.5; } main { max-width: 860px; margin: 0 auto; padding: 28px 18px; } .operator-card { border: 2px solid #a83d3d; background: #ffffff; padding: 20px; } h1 { margin: 0; font-size: 28px; } .next-card { margin-top: 16px; border: 2px solid #a83d3d; background: #fff4f4; padding: 16px; } .next-card span { display: block; color: #657282; font-weight: 800; } .primary-action { display: flex; align-items: center; justify-content: center; min-height: 56px; margin-top: 6px; background: #a83d3d; color: #ffffff; padding: 12px; font-size: 24px; font-weight: 900; text-align: center; } .operator-note { color: #657282; font-size: 13px; } .support-details { margin-top: 14px; border: 1px solid #cfd8e1; background: #ffffff; } .support-details summary { cursor: pointer; padding: 14px 16px; color: #657282; font-weight: 800; } .support-details div { padding: 0 16px 16px; }^</style^>^</head^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<body^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<main class="install-wizard"^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<section class="operator-card level-fail"^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<h1^>BestPerf 安装结果^</h1^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<h2^>安装失败^</h2^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<div class="next-card"^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<span^>普通用户只看这一段 · 下一步只点这个 · 现在只需要做这一步^</span^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<div class="primary-action"^>安装 Python 3.9 或更新版本^</div^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<p^>安装完成后，重新双击 Install BestPerf。^</p^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<p class="operator-note"^>普通用户不用看下面的报告路径。^</p^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^</div^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<p^>如果还是失败，把整个文件夹压缩后连同截图发给支持人员。^</p^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^</section^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^<details class="support-details"^>^<summary^>支持人员才需要看^</summary^>^<div^>^<p^>给支持人员: install_report.latest.json 和 install.latest.log^</p^>^</div^>^</details^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^</main^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^</body^>
>> "%FIELDREPLAY_SUMMARY_HTML%" echo ^</html^>
> "%FIELDREPLAY_LOG%" echo Python 3.9 or newer is required. Install Python, then run Install BestPerf again.
echo Python 3.9 or newer is required. Install Python, then run Install BestPerf again.
echo.
echo Install summary:      %FIELDREPLAY_SUMMARY%
echo Install summary HTML: %FIELDREPLAY_SUMMARY_HTML%
echo Install report:       %FIELDREPLAY_REPORT%
echo Install log:          %FIELDREPLAY_LOG%
set "FIELDREPLAY_SUMMARY_HTML_TO_OPEN=%FIELDREPLAY_SUMMARY_HTML%"
call :open_install_summary
exit /b 1

:run_with_bundled_python
"%FIELDREPLAY_BUNDLED_PYTHON%" scripts\bestperf_install.py %*
set "FIELDREPLAY_INSTALL_STATUS=%errorlevel%"
set "FIELDREPLAY_SUMMARY_HTML_TO_OPEN=%FIELDREPLAY_INSTALL_OUT%\install_summary.latest.html"
call :open_install_summary
exit /b %FIELDREPLAY_INSTALL_STATUS%

:run_with_py
py -3 scripts\bestperf_install.py %*
set "FIELDREPLAY_INSTALL_STATUS=%errorlevel%"
set "FIELDREPLAY_SUMMARY_HTML_TO_OPEN=%FIELDREPLAY_INSTALL_OUT%\install_summary.latest.html"
call :open_install_summary
exit /b %FIELDREPLAY_INSTALL_STATUS%

:run_with_python
python scripts\bestperf_install.py %*
set "FIELDREPLAY_INSTALL_STATUS=%errorlevel%"
set "FIELDREPLAY_SUMMARY_HTML_TO_OPEN=%FIELDREPLAY_INSTALL_OUT%\install_summary.latest.html"
call :open_install_summary
exit /b %FIELDREPLAY_INSTALL_STATUS%

:open_install_summary
if defined FIELDREPLAY_SKIP_OPEN_SUMMARY exit /b 0
if not defined FIELDREPLAY_SUMMARY_HTML_TO_OPEN exit /b 0
if exist "%FIELDREPLAY_SUMMARY_HTML_TO_OPEN%" start "" "%FIELDREPLAY_SUMMARY_HTML_TO_OPEN%"
exit /b 0
