@echo off
setlocal EnableExtensions

title AI Voice Bridge Dictation Setup

set "AUTO=0"
if /I "%~1"=="/auto" set "AUTO=1"

set "TARGET=%USERPROFILE%\Documents\RITS-AI-Voice-Bridge"
set "VENV_DIR=%TARGET%\.venv"
set "VENV_PYTHON=%VENV_DIR%\Scripts\python.exe"
set "PYTHON_CMD="

echo ===============================================
echo AI Voice Bridge Dictation Setup
echo Whisper accuracy backend
echo ===============================================
echo.
echo This installs/checks local speech-to-text for private dictation.
echo Default backend: Whisper base.en model through faster-whisper.
echo.
echo Script folder:  %~dp0
echo Target folder:  %TARGET%
echo.

if not exist "%TARGET%\ai_voice_bridge_server.py" (
    echo ERROR: Main AI Voice Bridge app is not installed in Documents yet.
    echo Run Run-AI-Voice-Bridge-Windows-Installer.bat first.
    goto Fail
)

echo Checking 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"
)

if "%PYTHON_CMD%"=="" (
    echo [MISSING] Python 3 was not detected.
    goto Fail
)

echo [OK] Python 3 detected: %PYTHON_CMD%
echo.

echo [CHECK] Local Python environment
if not exist "%VENV_PYTHON%" (
    echo [NEW] Creating local Python virtual environment inside the app folder.
    %PYTHON_CMD% -m venv "%VENV_DIR%"
    if errorlevel 1 (
        echo ERROR: Could not create local Python environment.
        goto Fail
    )
)

if not exist "%VENV_PYTHON%" (
    echo ERROR: Local Python environment was not created.
    goto Fail
)

echo [OK] Local Python environment: %VENV_DIR%
echo.

echo [CHECK] Whisper Python package
"%VENV_PYTHON%" -c "import faster_whisper, sys; sys.exit(0)" >nul 2>nul
if %ERRORLEVEL%==0 (
    echo [OK] faster-whisper already installed in local environment.
) else (
    echo [WARN] faster-whisper not found. Installing into local environment.
    "%VENV_PYTHON%" -m pip install --upgrade pip
    "%VENV_PYTHON%" -m pip install --upgrade faster-whisper
    if errorlevel 1 (
        echo.
        echo ERROR: pip install faster-whisper failed.
        echo Try installing Python 3.10, 3.11, or 3.12, then rerun this setup.
        goto Fail
    )
    "%VENV_PYTHON%" -c "import faster_whisper, sys; sys.exit(0)" >nul 2>nul
    if errorlevel 1 (
        echo.
        echo ERROR: faster-whisper installed, but local Python still cannot import it.
        goto Fail
    )
    echo [OK] faster-whisper installed in local environment.
)

echo.
echo [CHECK] Whisper model cache
echo This step is allowed to warn/fail without killing the install.
echo The server can retry the model download on first dictation.
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0Setup-Local-Dictation-Windows.ps1"
if errorlevel 1 (
    echo.
    echo [WARN] Whisper model pre-cache did not complete.
    echo The bridge is still installed. First dictation can retry the model download.
)

echo.
echo ===============================================
echo LOCAL DICTATION SETUP COMPLETE
echo ===============================================
echo Backend: Whisper
echo Model: base.en
echo Python environment: %VENV_DIR%
echo.
echo Next step: restart Start-AI-Voice-Bridge.bat
echo The server should say: Local dictation: available (Whisper accuracy backend)
echo.
if not "%AUTO%"=="1" pause
exit /b 0

:Fail
echo.
echo Local dictation setup failed.
echo.
if not "%AUTO%"=="1" pause
exit /b 1
