@echo off
setlocal enabledelayedexpansion
title Church Transcription — Installation

echo.
echo ============================================================
echo  Church Live Transcription Display — One-time Setup
echo ============================================================
echo.
echo This will install all required software into a local
echo virtual environment (.venv). It will NOT affect other
echo Python programs on this computer.
echo.
echo Estimated time: 10-20 minutes (depends on internet speed).
echo.
pause

:: ── Check Python ────────────────────────────────────────────────────────────

echo [1/6] Checking Python version (3.12+ required)...

:: Prefer 3.12 then 3.13 — PyTorch does not yet publish wheels for 3.14+
set PYEXE=
for %%v in (3.12 3.13) do (
    if not defined PYEXE (
        py -%%v --version >nul 2>&1
        if not errorlevel 1 set PYEXE=py -%%v
    )
)

if not defined PYEXE (
    echo.
    echo ERROR: Python 3.12 or 3.13 not found.
    echo.
    echo PyTorch ^(required by WhisperLiveKit^) does not yet publish wheels
    echo for Python 3.14. Please install Python 3.12 alongside any newer
    echo version — they coexist safely on the same PC.
    echo.
    echo Download: https://www.python.org/downloads/release/python-31210/
    echo Tick "Add Python to PATH" during install.
    echo.
    pause
    exit /b 1
)

for /f "tokens=2 delims= " %%v in ('%PYEXE% --version 2^>^&1') do set PYVER=%%v
echo Found Python %PYVER% ^(using %PYEXE%^)

:: ── Create virtual environment ───────────────────────────────────────────────

echo.
echo [2/6] Creating virtual environment in .venv\ ...
if exist .venv (
    echo .venv already exists — delete it and re-run if you need a clean install.
) else (
    %PYEXE% -m venv .venv
    if errorlevel 1 (
        echo ERROR: Failed to create virtual environment.
        pause
        exit /b 1
    )
)

call .venv\Scripts\activate.bat

:: ── Upgrade pip ──────────────────────────────────────────────────────────────

echo.
echo [3/6] Upgrading pip...
python -m pip install --upgrade pip --quiet

:: ── Install PyTorch with CUDA ─────────────────────────────────────────────────

echo.
echo [4/6] Installing PyTorch with CUDA support (~2.5 GB download)...
echo This is the longest step. Please wait.
echo.
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
if errorlevel 1 (
    echo cu124 index failed, trying cu121...
    pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
    if errorlevel 1 (
        echo.
        echo ERROR: PyTorch CUDA installation failed on both cu124 and cu121 indexes.
        echo Check your internet connection and try again.
        pause
        exit /b 1
    )
)

:: Confirm the CUDA build was installed, not the CPU fallback
for /f "delims=" %%v in ('python -c "import torch; print(torch.__version__)"') do set TORCH_VER=%%v
echo Installed PyTorch %TORCH_VER%
echo %TORCH_VER% | find "+cpu" >nul
if not errorlevel 1 (
    echo.
    echo WARNING: CPU-only PyTorch was installed instead of the CUDA build.
    echo Transcription will work but will be slow without GPU acceleration.
    echo See SETUP.md Part 2b for CUDA Toolkit installation instructions.
    echo.
    pause
)

:: ── Install WhisperLiveKit ────────────────────────────────────────────────────

echo.
echo [5/6] Installing WhisperLiveKit and dependencies...
echo.
pip install whisperlivekit pyannote.audio python-multipart diart
if errorlevel 1 (
    echo.
    echo ERROR: WhisperLiveKit installation failed.
    pause
    exit /b 1
)

:: ── Install bridge dependencies ───────────────────────────────────────────────

echo.
echo [6/6] Installing bridge script dependencies...
pip install -r bridge\requirements.txt
if errorlevel 1 (
    echo.
    echo ERROR: Bridge dependencies failed to install.
    pause
    exit /b 1
)

:: ── Pre-download Whisper model ────────────────────────────────────────────────

echo.
echo Downloading Whisper large-v3 model (~3 GB) — this only happens once.
echo.
python -c "from faster_whisper import WhisperModel; WhisperModel('large-v3', device='cuda', compute_type='float16')"
if errorlevel 1 (
    echo.
    echo WARNING: Model pre-download failed. It will download on first start instead.
    echo This is not critical — continuing.
)

:: ── Done ─────────────────────────────────────────────────────────────────────

echo.
echo ============================================================
echo  Installation complete.
echo ============================================================
echo.
echo Next steps:
echo   1. Edit start.bat and add your HuggingFace token
echo      (see SETUP.md Part 7 for instructions)
echo   2. Double-click start.bat to launch the system
echo.
pause
