@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion

echo ==========================================
echo  Acoustic Module - One-Click Demo
echo ==========================================

set BUILD_DIR=%~dp0build
set MODEL=models\gunshot_classifier.onnx
set VAL_DIR=dataset\binary\val

REM --- Ensure executables exist ---
if not exist "%BUILD_DIR%\test_core_lib.exe" (
    echo [INFO] test_core_lib.exe not found, building core tests first...
    call "%~dp0build_core_test.bat"
    if %ERRORLEVEL% NEQ 0 exit /b 1
)

if not exist "%BUILD_DIR%\demo_offline.exe" (
    echo [INFO] demo_offline.exe not found, building demo first...
    call "%~dp0build_demo.bat"
    if %ERRORLEVEL% NEQ 0 exit /b 1
)

REM --- Step 1: Core unit tests ---
echo.
echo [Step 1/3] Running core unit tests...
"%BUILD_DIR%\test_core_lib.exe"
if %ERRORLEVEL% NEQ 0 (
    echo [FAIL] Core unit tests failed.
    exit /b 1
)

REM --- Step 2: ONNX quick inference ---
echo.
echo [Step 2/3] Running ONNX classifier test...
if exist "%MODEL%" (
    if exist "%VAL_DIR%\ambient" (
        for %%F in ("%VAL_DIR%\ambient\*.wav") do (
            echo Testing: %%F
            "%BUILD_DIR%\test_classifier_cpp.exe" --model "%MODEL%" --wav "%%F"
            goto :break_onnx
        )
        :break_onnx
    ) else (
        echo [SKIP] No val data found for ONNX test
    )
) else (
    echo [SKIP] ONNX model not found: %MODEL%
)

REM --- Step 3: Offline pipeline demo ---
echo.
echo [Step 3/3] Running offline pipeline demo...
if exist "%VAL_DIR%" (
    "%BUILD_DIR%\demo_offline.exe" "%VAL_DIR%"
) else (
    echo [SKIP] Val dataset not found: %VAL_DIR%
)

echo.
echo [OK] Demo complete.
endlocal
