@echo off
REM Build script for SecondVoice on Windows

echo ========================================
echo SecondVoice - Windows Build Script
echo ========================================
echo.

REM Check if vcpkg is installed
if not defined VCPKG_ROOT (
    echo [ERROR] VCPKG_ROOT environment variable not set
    echo.
    echo Please install vcpkg and set VCPKG_ROOT:
    echo   1. git clone https://github.com/microsoft/vcpkg.git C:\vcpkg
    echo   2. cd C:\vcpkg
    echo   3. .\bootstrap-vcpkg.bat
    echo   4. setx VCPKG_ROOT "C:\vcpkg"
    echo   5. Restart your terminal
    echo.
    exit /b 1
)

echo [INFO] vcpkg found at: %VCPKG_ROOT%
echo.

REM Check if .env exists
if not exist ".env" (
    echo [WARNING] .env file not found
    echo Please create .env from .env.example and add your API keys:
    echo   copy .env.example .env
    echo   notepad .env
    echo.
)

REM Check for Visual Studio or build tools
where cl >nul 2>&1
if %errorlevel% neq 0 (
    echo [WARNING] Visual Studio compiler not found in PATH
    echo.
    echo Trying to find Visual Studio...

    REM Try to find vcvarsall.bat
    set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"

    if exist "%VSWHERE%" (
        for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
            set "VS_PATH=%%i"
        )

        if defined VS_PATH (
            echo [INFO] Found Visual Studio at: %VS_PATH%
            call "%VS_PATH%\VC\Auxiliary\Build\vcvars64.bat"
        ) else (
            echo [ERROR] Could not find Visual Studio
            echo.
            echo Please install Visual Studio 2019 or later with C++ tools:
            echo   https://visualstudio.microsoft.com/downloads/
            echo.
            exit /b 1
        )
    ) else (
        echo [ERROR] Could not find vswhere.exe
        echo.
        echo Please install Visual Studio 2019 or later:
        echo   https://visualstudio.microsoft.com/downloads/
        echo.
        exit /b 1
    )
)

REM Check for Ninja
where ninja >nul 2>&1
if %errorlevel% neq 0 (
    echo [INFO] Ninja not found, installing via vcpkg...
    %VCPKG_ROOT%\vcpkg install ninja
)

REM Parse command line arguments
set BUILD_TYPE=Release
set CLEAN_BUILD=0

:parse_args
if "%~1"=="" goto :done_parsing
if /i "%~1"=="--debug" (
    set BUILD_TYPE=Debug
    shift
    goto :parse_args
)
if /i "%~1"=="--release" (
    set BUILD_TYPE=Release
    shift
    goto :parse_args
)
if /i "%~1"=="--clean" (
    set CLEAN_BUILD=1
    shift
    goto :parse_args
)
echo [WARNING] Unknown argument: %~1
shift
goto :parse_args

:done_parsing

echo [INFO] Build type: %BUILD_TYPE%
echo.

REM Clean build if requested
if %CLEAN_BUILD%==1 (
    echo [INFO] Cleaning build directory...
    if exist "build\windows-%BUILD_TYPE%" (
        rmdir /s /q "build\windows-%BUILD_TYPE%"
    )
    echo.
)

REM Configure with CMake
echo [INFO] Configuring CMake...
cmake --preset windows-%BUILD_TYPE%

if %errorlevel% neq 0 (
    echo.
    echo [ERROR] CMake configuration failed
    exit /b 1
)

echo.
echo [INFO] Building...
cmake --build build/windows-%BUILD_TYPE% --config %BUILD_TYPE%

if %errorlevel% neq 0 (
    echo.
    echo [ERROR] Build failed
    exit /b 1
)

echo.
echo ========================================
echo [SUCCESS] Build completed!
echo ========================================
echo.
echo Executable location:
echo   build\windows-%BUILD_TYPE%\%BUILD_TYPE%\SecondVoice.exe
echo.
echo To run the application:
echo   cd build\windows-%BUILD_TYPE%\%BUILD_TYPE%
echo   SecondVoice.exe
echo.
echo Make sure you have:
echo   1. Created .env with your API keys
echo   2. config.json is present (copied automatically)
echo   3. A microphone connected
echo.

exit /b 0
