@echo off
REM Build script for SecondVoice on Windows using MinGW (no Visual Studio!)

echo ========================================
echo SecondVoice - MinGW Build Script
echo ========================================
echo.

REM Check if MinGW/GCC is installed
where gcc >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] GCC not found in PATH
    echo.
    echo Please run setup_mingw.bat first to install MinGW
    echo   .\setup_mingw.bat
    echo.
    pause
    exit /b 1
)

echo [INFO] GCC found:
gcc --version | findstr "gcc"
echo.

REM Check if vcpkg is installed
if not defined VCPKG_ROOT (
    echo [ERROR] VCPKG_ROOT environment variable not set
    echo.
    echo Please run setup_mingw.bat first to install vcpkg
    echo   .\setup_mingw.bat
    echo.
    pause
    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 CMake
where cmake >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] CMake not found
    echo Please run setup_mingw.bat first
    pause
    exit /b 1
)

REM Check for Ninja
where ninja >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Ninja not found
    echo Please run setup_mingw.bat first
    pause
    exit /b 1
)

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\mingw-%BUILD_TYPE%" (
        rmdir /s /q "build\mingw-%BUILD_TYPE%"
    )
    echo.
)

REM Set preset name based on build type
if /i "%BUILD_TYPE%"=="Debug" (
    set PRESET=mingw-debug
) else (
    set PRESET=mingw-release
)

REM Configure with CMake - force MinGW triplet via environment variable
echo [INFO] Configuring CMake for MinGW build...
echo [INFO] Forcing vcpkg triplet: x64-mingw-static
set VCPKG_DEFAULT_TRIPLET=x64-mingw-static
set VCPKG_DEFAULT_HOST_TRIPLET=x64-mingw-static

cmake -B build/mingw-%BUILD_TYPE% ^
  -G Ninja ^
  -DCMAKE_BUILD_TYPE=%BUILD_TYPE% ^
  -DCMAKE_TOOLCHAIN_FILE=%VCPKG_ROOT%/scripts/buildsystems/vcpkg.cmake ^
  -DVCPKG_TARGET_TRIPLET=x64-mingw-static ^
  -DVCPKG_HOST_TRIPLET=x64-mingw-static ^
  -DCMAKE_C_COMPILER=gcc ^
  -DCMAKE_CXX_COMPILER=g++ ^
  -DCMAKE_MAKE_PROGRAM=ninja

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

echo.
echo [INFO] Building with Ninja...
cmake --build build/mingw-%BUILD_TYPE%

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

echo.
echo ========================================
echo [SUCCESS] Build completed!
echo ========================================
echo.
echo Executable location:
echo   build\mingw-%BUILD_TYPE%\SecondVoice.exe
echo.
echo To run the application:
echo   cd build\mingw-%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
