@echo off
title WebRadarCPP Build

echo [WebRadarCPP] ========================================
echo [WebRadarCPP] Building C++ WebRadar Backend
echo [WebRadarCPP] ========================================
echo.

set PROJECT_DIR=%~dp0

REM ── Tìm Visual Studio ─────────────────────────────────
set VSPATH=
if exist "%ProgramFiles%\Microsoft Visual Studio\Installer\vswhere.exe" (
    for /f "tokens=* usebackq" %%a in (
        `"%ProgramFiles%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath`
    ) do set VSPATH=%%a
)
if "%VSPATH%"=="" (
    if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
        for /f "tokens=* usebackq" %%a in (
            `"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath`
        ) do set VSPATH=%%a
    )
)

REM ── Fallback: tìm trong các thư mục phổ biến ──────────
if "%VSPATH%"=="" (
    if exist "C:\Program Files\Microsoft Visual Studio\18\Community" set VSPATH=C:\Program Files\Microsoft Visual Studio\18\Community
)
if "%VSPATH%"=="" (
    if exist "C:\Program Files\Microsoft Visual Studio\2022\Community" set VSPATH=C:\Program Files\Microsoft Visual Studio\2022\Community
)
if "%VSPATH%"=="" (
    echo [WebRadarCPP] ERROR: Cannot find Visual Studio 2022/2026.
    pause
    exit /b 1
)
echo [WebRadarCPP] Found Visual Studio at: %VSPATH%

REM ── Load VS environment (x64) ──────────────────────────
call "%VSPATH%\VC\Auxiliary\Build\vcvarsall.bat" x64
if %ERRORLEVEL% neq 0 (
    echo [WebRadarCPP] ERROR: Failed to load VS environment!
    pause
    exit /b 1
)
echo [WebRadarCPP] VS environment loaded (x64).

REM ── Step 1: Vcpkg deps ─────────────────────────────────
echo [WebRadarCPP] Step 1/3: Installing vcpkg dependencies...
cd /d "%PROJECT_DIR%"

REM Tìm vcpkg (trong VS hoặc PATH)
set VCPKG_ROOT=
if exist "%VSPATH%\VC\vcpkg\vcpkg.exe" set VCPKG_ROOT=%VSPATH%\VC\vcpkg
if "%VCPKG_ROOT%"=="" (
    where vcpkg >nul 2>&1
    if not errorlevel 1 set VCPKG_ROOT=vcpkg
)

if not "%VCPKG_ROOT%"=="" (
    echo [WebRadarCPP] vcpkg found, installing deps (one-time)...
    if "%VCPKG_ROOT%"=="vcpkg" (
        vcpkg install --triplet x64-windows-static
    ) else (
        "%VCPKG_ROOT%\vcpkg.exe" install --triplet x64-windows-static
    )
    if errorlevel 1 (
        echo [WebRadarCPP] WARNING: vcpkg install failed. Trying MSBuild anyway.
    ) else (
        echo [WebRadarCPP] vcpkg deps installed.
    )
) else (
    echo [WebRadarCPP] vcpkg not found. MSBuild will auto-install via manifest mode.
)

REM ── Step 2: Find MSBuild ───────────────────────────────
set MSBUILD=
if exist "%VSPATH%\MSBuild\Current\Bin\MSBuild.exe" (
    set MSBUILD="%VSPATH%\MSBuild\Current\Bin\MSBuild.exe"
)
if "%MSBUILD%"=="" (
    where msbuild >nul 2>&1
    if not errorlevel 1 set MSBUILD=msbuild
)
if "%MSBUILD%"=="" (
    echo [WebRadarCPP] ERROR: Cannot find MSBuild!
    pause
    exit /b 1
)
echo [WebRadarCPP] Using: %MSBUILD%

REM ── Step 3: Build ──────────────────────────────────────
echo [WebRadarCPP] Step 3/3: Building with MSBuild (release|x64)...
cd /d "%PROJECT_DIR%"
%MSBUILD% webradar_cpp.vcxproj /p:Configuration=Release /p:Platform=x64
if errorlevel 1 (
    echo [WebRadarCPP] ERROR: Build failed!
    echo [WebRadarCPP]
    echo [WebRadarCPP] Common issues:
    echo [WebRadarCPP]   - Windows SDK not installed (Run "Visual Studio Installer" ^> Modify ^> Windows SDK)
    echo [WebRadarCPP]   - vcpkg failed to install dependencies
    pause
    exit /b 1
)

echo.
echo [WebRadarCPP] ========================================
echo [WebRadarCPP] BUILD SUCCESSFUL
echo [WebRadarCPP] ========================================
echo.
echo Binary: %PROJECT_DIR%build\webradar_cpp.exe
echo.
echo To run:
echo   1. Make sure CS2 is running
echo   2. cd %PROJECT_DIR%
echo   3. build\webradar_cpp.exe
echo   4. Open browser to http://localhost:8082
echo.

pause
