@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo ========================================
echo   Agentic QE Fleet - Execute 环境一键配置
echo ========================================
echo.

:: ── 1. 自动检测 Android SDK 路径 ──
set "SDK_PATH="

:: 默认路径
if exist "%LOCALAPPDATA%\Android\Sdk" (
    set "SDK_PATH=%LOCALAPPDATA%\Android\Sdk"
)

:: Android Studio 自定义路径
if exist "C:\Android\Sdk" (
    set "SDK_PATH=C:\Android\Sdk"
)

:: 从 Android Studio 配置文件读取
if exist "%APPDATA%\Google\AndroidStudio*\options\sdkManager.xml" (
    for /f "tokens=*" %%a in ('findstr /c:"sdk.root" "%APPDATA%\Google\AndroidStudio*\options\sdkManager.xml" 2^>nul') do (
        echo %%a | findstr "sdk.root" >nul
    )
)

if "%SDK_PATH%"=="" (
    echo [WARN] 未自动检测到 Android SDK 路径
    echo        请手动输入 SDK 路径
    echo        示例: C:\Users\23184\AppData\Local\Android\Sdk
    set /p SDK_PATH="SDK 路径: "
)

echo [INFO] Android SDK: %SDK_PATH%
echo.

:: ── 2. 统一设置环境变量 ──
echo [STEP] 设置环境变量...

set "ANDROID_HOME=%SDK_PATH%"
set "ANDROID_SDK_ROOT=%SDK_PATH%"

:: SDK 内部工具路径
set "SDK_CMDLINE=%SDK_PATH%\cmdline-tools\latest\bin"
set "SDK_TOOLS_BIN=%SDK_PATH%\tools\bin"
set "SDK_EMULATOR=%SDK_PATH%\emulator"
set "SDK_PLATFORM_TOOLS=%SDK_PATH%\platform-tools"

:: Java (自动搜索)
set "JAVA_PATH="
if exist "C:\Program Files\Android\Android Studio\jbr\bin" (
    set "JAVA_PATH=C:\Program Files\Android\Android Studio\jbr\bin"
)
if exist "C:\Program Files\Java\jdk-17\bin" (
    set "JAVA_PATH=C:\Program Files\Java\jdk-17\bin"
)

:: ── 3. 追加到当前会话 PATH ──
set "NEW_PATH=%SDK_CMDLINE%;%SDK_TOOLS_BIN%;%SDK_EMULATOR%;%SDK_PLATFORM_TOOLS%"
if not "%JAVA_PATH%"=="" set "NEW_PATH=%NEW_PATH%;%JAVA_PATH%"
set "PATH=%PATH%;%NEW_PATH%"

echo [OK] 当前窗口 PATH 已配置（关窗后失效）
echo.

:: ── 4. 永久写入用户 PATH（这样以后每个新窗口都能用） ──
echo [STEP] 永久写入系统 PATH...

set "PERMANENT=%SDK_CMDLINE%;%SDK_TOOLS_BIN%;%SDK_EMULATOR%;%SDK_PLATFORM_TOOLS%"
if not "%JAVA_PATH%"=="" set "PERMANENT=%PERMANENT%;%JAVA_PATH%"

for /f "usebackq tokens=2,*" %%A in (`reg query "HKCU\Environment" /v PATH 2^>nul`) do set "USER_PATH=%%B"
echo !USER_PATH! | findstr /c:"%SDK_CMDLINE%" >nul
if %errorlevel% neq 0 (
    reg add "HKCU\Environment" /v PATH /t REG_EXPAND_SZ /d "!USER_PATH!;%PERMANENT%" /f >nul 2>&1
    echo [OK] 已永久写入（重启终端生效）
) else (
    echo [OK] PATH 已存在，跳过写入
)
echo.

:: ── 5. 验证环境 ──
echo ========================================
echo   环境验证
echo ========================================
echo.

echo [1/7] Python...
python --version 2>nul && echo   [OK] || echo   [FAIL] Python 未安装
echo.

echo [2/7] Playwright...
python -c "from playwright.sync_api import sync_playwright; print('  [OK]')" 2>nul || (
    echo   [WARN] Playwright 未安装，正在安装...
    pip install playwright
    playwright install chromium
)
echo.

echo [3/7] Java...
java -version 2>&1 | findstr "version" >nul && echo   [OK] || echo   [WARN] Java 未找到（Appium 需要）
echo.

echo [4/7] adb...
adb --version 2>nul | findstr "Android" >nul && echo   [OK] || echo   [WARN] adb 未找到（Android 设备连接需要）
adb devices 2>nul
echo.

echo [5/7] Appium Server...
appium --version 2>nul && echo   [OK] || echo   [WARN] Appium 未安装
echo.

echo [6/7] Appium Driver (uiautomator2)...
appium driver list --installed 2>nul | findstr "uiautomator2" >nul && echo   [OK] || (
    echo   [WARN] uiautomator2 驱动未安装
    echo         安装命令: appium driver install uiautomator2
)
echo.

echo [7/7] Fleet Agent 验证...
cd /d "%~dp0\.."
python scripts\fleet_runner.py validate 2>nul && echo   [OK] || echo   [FAIL] 请确认在项目根目录下
echo.

:: ── 6. 输出快捷命令卡片 ──
echo ========================================
echo   常用命令速查
echo ========================================
echo.
echo  启动 Appium Server:
echo    appium --allow-insecure "uiautomator2:chromedriver_autodownload"
echo.
echo  查看已连接设备:
echo    adb devices
echo.
echo  创建 Android 模拟器（只需一次）:
echo    avdmanager create avd -n test_device -k "system-images;android-34;google_apis;x86_64" -d pixel_6
echo.
echo  启动模拟器:
echo    emulator -avd test_device
echo.
echo  运行 Execute 战区:
echo    /qe-fleet execute source_docs/requirements_raw/需求.docx
echo.
echo  一键验证环境:
echo    %~dp0setup_execute_env.bat
echo.

echo ========================================
echo   配置完成！
echo ========================================
echo   如果 Android 模拟器还没创建，现在执行:
echo     avdmanager create avd -n test_device -k "system-images;android-34;google_apis;x86_64" -d pixel_6
echo.
echo   如果还没装 uiautomator2 驱动:
echo     appium driver install uiautomator2
echo.

pause
