@echo off
setlocal EnableExtensions

title AI Voice Bridge 3.0 Whisper

echo ===============================================
echo AI Voice Bridge 3.0 Whisper
echo Windows Launcher
echo ===============================================
echo.

set "SCRIPT_DIR=%~dp0"
set "VENV_PYTHON=%SCRIPT_DIR%.venv\Scripts\python.exe"
set "PYTHON_CMD="
set "RATE=0"

set "PORT=5055"
set "BRIDGE_HEALTH="
for /f "delims=" %%H in ('powershell -NoProfile -Command "try { $r = Invoke-RestMethod -Uri 'http://127.0.0.1:5055/health' -TimeoutSec 1; if ($r.service -eq 'AI Voice Bridge') { 'AI Voice Bridge version ' + $r.version } } catch { }"') do set "BRIDGE_HEALTH=%%H"

if not "%BRIDGE_HEALTH%"=="" (
    echo [ALREADY RUNNING] AI Voice Bridge is already running on port %PORT%.
    echo Running: %BRIDGE_HEALTH%
    echo.
    echo You do not need to start another server window.
    echo Keep the existing AI Voice Bridge window open and use Chrome normally.
    echo.
    echo This duplicate launcher will now wait for you to close it.
    echo.
    pause
    exit /b 0
)

set "EXISTING_PID="
for /f "delims=" %%P in ('powershell -NoProfile -Command "try { $p = Get-NetTCPConnection -LocalPort 5055 -State Listen -ErrorAction SilentlyContinue ^| Select-Object -First 1 -ExpandProperty OwningProcess; if ($p) { $p } } catch { }"') do set "EXISTING_PID=%%P"

if not "%EXISTING_PID%"=="" (
    echo [PORT IN USE] Something is already listening on port %PORT%.
    echo AI Voice Bridge cannot start a second server on the same port.
    echo.
    echo PID: %EXISTING_PID%
    powershell -NoProfile -Command "try { Get-CimInstance Win32_Process -Filter 'ProcessId=%EXISTING_PID%' ^| Select-Object -ExpandProperty CommandLine } catch { }"
    echo.
    echo Close the existing app/server using port %PORT%, then run this launcher again.
    echo.
    pause
    exit /b 1
)


if exist "%VENV_PYTHON%" (
    set "PYTHON_CMD=%VENV_PYTHON%"
    echo [OK] Using local app Python environment:
    echo %VENV_PYTHON%
    goto PythonDone
)

echo Checking for Python 3...
py -3 -c "import sys; sys.exit(0 if sys.version_info[0] == 3 else 1)" >nul 2>nul
if %ERRORLEVEL%==0 set "PYTHON_CMD=py -3"

if "%PYTHON_CMD%"=="" (
    python -c "import sys; sys.exit(0 if sys.version_info[0] == 3 else 1)" >nul 2>nul
    if %ERRORLEVEL%==0 set "PYTHON_CMD=python"
)

if "%PYTHON_CMD%"=="" (
    python3 -c "import sys; sys.exit(0 if sys.version_info[0] == 3 else 1)" >nul 2>nul
    if %ERRORLEVEL%==0 set "PYTHON_CMD=python3"
)

:PythonDone
if "%PYTHON_CMD%"=="" (
    echo [MISSING] Python 3 was not detected.
    echo Please install Python 3, then run this launcher again.
    echo.
    pause
    exit /b 1
)

echo.
echo Starting AI Voice Bridge 3.0 Whisper...
echo Server: http://127.0.0.1:5055
echo Voice speed: Windows default
echo Dictation backend: Whisper / faster-whisper
echo Dictation: click the Mic button, or use Ctrl+Shift+V
echo Readback: green Repeat replays the latest response, red Stop stops speech
echo If server says dictation unavailable, run Setup-Local-Dictation-Windows.bat first.
echo Stop spoken AI output: Esc
echo.
echo Keep this window open while using AI Voice Bridge.
echo.

%PYTHON_CMD% "%SCRIPT_DIR%ai_voice_bridge_server.py" --port 5055 --rate %RATE% --volume 100
set "EXITCODE=%ERRORLEVEL%"

echo.
echo Server stopped or failed with exit code: %EXITCODE%
echo If this window appeared after a crash, screenshot the error text above.
echo.
pause
exit /b %EXITCODE%
