@echo off
rem ============================================================
rem Chanora Windows smoke build + launch script (L9 / L10 / L11
rem of the P0 release plan).
rem
rem Runs end-to-end: vcvars64 + Flutter + Rust on PATH, cargo
rem build the bridge, flutter build the Windows runner, sanity-
rem check the output sizes, launch the runner for 5s, then grep
rem the log for the bridge-initialised line and for panics.
rem
rem Hard-coded paths assume the Korean test host layout:
rem   C:\Users\admin\dev\flutter\bin
rem   C:\Users\admin\.cargo\bin
rem   C:\Program Files\Microsoft Visual Studio\2022\Community
rem   Repo at C:\Users\admin\chanora (script invoked from there).
rem
rem Exit codes:
rem   0 = PASS
rem   non-zero = the step number that failed (1..10)
rem ============================================================

setlocal EnableDelayedExpansion
set "REPO_ROOT=%~dp0.."
pushd "%REPO_ROOT%" || exit /b 1

rem --- Step 1: PATH + vcvars64 ---------------------------------
set "PATH=C:\Users\admin\dev\flutter\bin;C:\Users\admin\.cargo\bin;%PATH%"
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" >nul
if errorlevel 1 (
    echo FAIL: vcvars64.bat returned non-zero
    popd & exit /b 1
)

rem --- Step 2: cargo build bridge ------------------------------
echo [smoke] cargo build -p chanora_bridge --release
cargo build -p chanora_bridge --release
if errorlevel 1 (
    echo FAIL: cargo build -p chanora_bridge --release
    popd & exit /b 2
)

rem --- Step 3: bridge DLL exists and is >5 MB ------------------
set "BRIDGE_DLL=target\release\chanora_bridge.dll"
if not exist "%BRIDGE_DLL%" (
    echo FAIL: %BRIDGE_DLL% does not exist
    popd & exit /b 3
)
for %%A in ("%BRIDGE_DLL%") do set "BRIDGE_SIZE=%%~zA"
if !BRIDGE_SIZE! LSS 5242880 (
    echo FAIL: %BRIDGE_DLL% smaller than 5 MB ^(!BRIDGE_SIZE! bytes^)
    popd & exit /b 3
)

rem --- Step 4: flutter build windows --release -----------------
echo [smoke] flutter build windows --release
pushd "apps\chanora_flutter" || (popd & exit /b 4)
flutter build windows --release
if errorlevel 1 (
    echo FAIL: flutter build windows --release
    popd & popd & exit /b 4
)
popd

rem --- Step 5: chanora_flutter.exe exists ----------------------
set "RUNNER_DIR=apps\chanora_flutter\build\windows\x64\runner\Release"
set "RUNNER_EXE=%RUNNER_DIR%\chanora_flutter.exe"
if not exist "%RUNNER_EXE%" (
    echo FAIL: %RUNNER_EXE% does not exist
    popd & exit /b 5
)

rem --- Step 6: app.so exists and is >1 MB ----------------------
set "APP_SO=%RUNNER_DIR%\data\app.so"
if not exist "%APP_SO%" (
    echo FAIL: %APP_SO% does not exist
    popd & exit /b 6
)
for %%A in ("%APP_SO%") do set "APP_SO_SIZE=%%~zA"
if !APP_SO_SIZE! LSS 1048576 (
    echo FAIL: %APP_SO% smaller than 1 MB ^(!APP_SO_SIZE! bytes^)
    popd & exit /b 6
)

rem --- Step 7: copy bridge DLL into Release dir ----------------
copy /Y "%BRIDGE_DLL%" "%RUNNER_DIR%\chanora_bridge.dll" >nul
if errorlevel 1 (
    echo FAIL: copy chanora_bridge.dll into Release dir
    popd & exit /b 7
)

rem --- Step 8: launch runner for 5s ----------------------------
set "SMOKE_LOG=%TEMP%\chanora-smoke.log"
if exist "%SMOKE_LOG%" del "%SMOKE_LOG%"
echo [smoke] launching %RUNNER_EXE% for 5s, stderr -^> %SMOKE_LOG%
start "" /B "%RUNNER_EXE%" 2> "%SMOKE_LOG%"
timeout /t 5 /nobreak >nul
taskkill /F /IM chanora_flutter.exe >nul 2>&1

rem --- Step 9: grep for bridge initialised ---------------------
findstr /C:"bridge initialised" "%SMOKE_LOG%" >nul
if errorlevel 1 (
    echo FAIL: "bridge initialised" not present in %SMOKE_LOG%
    type "%SMOKE_LOG%"
    popd & exit /b 9
)

rem --- Step 10: grep for panic (must be ABSENT) ----------------
findstr /C:"panicked" "%SMOKE_LOG%" >nul
if not errorlevel 1 (
    echo FAIL: "panicked" found in %SMOKE_LOG%
    type "%SMOKE_LOG%"
    popd & exit /b 10
)

echo PASS: Chanora Windows smoke build + launch
popd
endlocal
exit /b 0
