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

set "UC_SCALE=%~1"
if not defined UC_SCALE set "UC_SCALE=smoke"
if /I not "%UC_SCALE%"=="smoke" if /I not "%UC_SCALE%"=="quick" if /I not "%UC_SCALE%"=="full" (
    echo Usage: benchmark.bat [smoke^|quick^|full]
    exit /b 2
)

set "UC_PROFILES=scalar baseline"
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "tools\test_avx2.ps1"
if not errorlevel 1 set "UC_PROFILES=scalar baseline avx2"

for %%P in (%UC_PROFILES%) do (
    call build.bat %%P test
    if errorlevel 1 exit /b 1
)

for /f "delims=" %%I in ('powershell.exe -NoProfile -Command "Get-Date -Format yyyyMMdd-HHmmss"') do set "UC_RUN_ID=%%I-%UC_SCALE%"
set "UC_RESULT_DIR=results\benchmarks\%UC_RUN_ID%"
mkdir "%UC_RESULT_DIR%" 2>nul

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "tools\write_benchmark_environment.ps1" -ResultDirectory "%UC_RESULT_DIR%" -RunId "%UC_RUN_ID%" -Scale "%UC_SCALE%" -Profiles "%UC_PROFILES%"
if errorlevel 1 exit /b 1

for %%P in (%UC_PROFILES%) do (
    echo.
    echo === Tuning %%P /O2 profile ===
    "out\bin\%%P\Release\uc_bench.exe" --suite tune --scale "%UC_SCALE%" --fanout 64 --output "%UC_RESULT_DIR%\%%P-tune.csv" --summary "%UC_RESULT_DIR%\%%P-tune-summary.csv"
    if errorlevel 1 exit /b 1

    echo === Adaptive phase transitions for %%P ===
    "out\bin\%%P\Release\uc_bench.exe" --suite adapt --scale "%UC_SCALE%" --leaf 512 --fanout 64 --levels 4 --output "%UC_RESULT_DIR%\%%P-adapt.csv" --summary "%UC_RESULT_DIR%\%%P-adapt-summary.csv"
    if errorlevel 1 exit /b 1

    echo === Comparing %%P /O2 profile ===
    "out\bin\%%P\Release\uc_bench.exe" --suite compare --scale "%UC_SCALE%" --leaf 512 --fanout 64 --levels 4 --output "%UC_RESULT_DIR%\%%P-compare.csv" --summary "%UC_RESULT_DIR%\%%P-compare-summary.csv"
    if errorlevel 1 exit /b 1

    echo === Hash index and stable-ID overhead for %%P ===
    "out\bin\%%P\Release\uc_bench.exe" --suite index --scale "%UC_SCALE%" --leaf 512 --fanout 64 --levels 4 --output "%UC_RESULT_DIR%\%%P-index.csv" --summary "%UC_RESULT_DIR%\%%P-index-summary.csv"
    if errorlevel 1 exit /b 1
)

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "tools\analyze_results.ps1" -InputDirectory "%UC_RESULT_DIR%" -Pattern "*-compare.csv" -OutputFile "%UC_RESULT_DIR%\aggregate-compare.csv" -CellMedianFile "%UC_RESULT_DIR%\cell-medians-compare.csv"
if errorlevel 1 exit /b 1

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "tools\analyze_results.ps1" -InputDirectory "%UC_RESULT_DIR%" -Pattern "*-adapt.csv" -OutputFile "%UC_RESULT_DIR%\aggregate-adapt.csv" -CellMedianFile "%UC_RESULT_DIR%\cell-medians-adapt.csv"
if errorlevel 1 exit /b 1

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "tools\validate_adaptation.ps1" -CompareCellMedianFile "%UC_RESULT_DIR%\cell-medians-compare.csv" -AdaptCellMedianFile "%UC_RESULT_DIR%\cell-medians-adapt.csv"
if errorlevel 1 exit /b 1

echo.
echo Benchmark run completed: %UC_RESULT_DIR%
exit /b 0
