@echo off
setlocal enabledelayedexpansion

:: AdvChkSys Build Script
:: ======================

:: Set build parameters
set "PROJECT_DIR=src\AdvChkSys"
set "BENCHMARKS_DIR=src\AdvChkSys.Benchmarks"
set "OUTPUT_DIR=build"
set "VERSION=0.1.8"

:: Create build directory if it doesn't exist
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"

:: Display header
echo ╔══════════════════════════════════════════════════════════════╗
echo ║                 ADVANCED CHUNK SYSTEM BUILD                   ║
echo ╚══════════════════════════════════════════════════════════════╝
echo.
echo Version: %VERSION%
echo Date: %DATE% %TIME%
echo.

:: Check for .NET SDK
echo Checking for .NET SDK...
dotnet --version > nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo Error: .NET SDK not found. Please install the .NET SDK.
    exit /b 1
)
echo ✓ .NET SDK found
echo.

:: Parse command line arguments
set "BUILD_WINDOWS=true"
set "BUILD_LINUX=false"
set "BUILD_MAC=false"
set "BUILD_BENCHMARKS=false"
set "BUILD_NUGET=false"
set "BUILD_DOCS=false"
set "CONFIG=Release"

:parse_args
if "%~1"=="" goto end_parse_args
if /i "%~1"=="--all" (
    set "BUILD_WINDOWS=true"
    set "BUILD_LINUX=true"
    set "BUILD_MAC=true"
    set "BUILD_BENCHMARKS=true"
    set "BUILD_NUGET=true"
    set "BUILD_DOCS=true"
) else if /i "%~1"=="--windows" (
    set "BUILD_WINDOWS=true"
) else if /i "%~1"=="--linux" (
    set "BUILD_LINUX=true"
) else if /i "%~1"=="--mac" (
    set "BUILD_MAC=true"
) else if /i "%~1"=="--benchmarks" (
    set "BUILD_BENCHMARKS=true"
) else if /i "%~1"=="--nuget" (
    set "BUILD_NUGET=true"
) else if /i "%~1"=="--docs" (
    set "BUILD_DOCS=true"
) else if /i "%~1"=="--debug" (
    set "CONFIG=Debug"
)
shift
goto parse_args
:end_parse_args

:: Display build configuration
echo Build Configuration:
echo • Configuration: %CONFIG%
echo • Windows: %BUILD_WINDOWS%
echo • Linux: %BUILD_LINUX%
echo • macOS: %BUILD_MAC%
echo • Benchmarks: %BUILD_BENCHMARKS%
echo • NuGet Package: %BUILD_NUGET%
echo • Documentation: %BUILD_DOCS%
echo.

:: Clean previous builds
echo Cleaning previous builds...
if exist "%PROJECT_DIR%\bin" rd /s /q "%PROJECT_DIR%\bin"
if exist "%PROJECT_DIR%\obj" rd /s /q "%PROJECT_DIR%\obj"
if exist "%BENCHMARKS_DIR%\bin" rd /s /q "%BENCHMARKS_DIR%\bin"
if exist "%BENCHMARKS_DIR%\obj" rd /s /q "%BENCHMARKS_DIR%\obj"
echo ✓ Clean completed
echo.

:: Restore packages
echo Restoring packages...
dotnet restore "%PROJECT_DIR%\AdvChkSys.csproj"
if %ERRORLEVEL% neq 0 (
    echo ✗ Package restore failed
    exit /b 1
)
echo ✓ Packages restored
echo.

:: Build the library (platform-agnostic)
echo Building AdvChkSys library...
dotnet build "%PROJECT_DIR%\AdvChkSys.csproj" -c %CONFIG% --no-restore
if %ERRORLEVEL% neq 0 (
    echo ✗ Library build failed
    exit /b 1
)

:: Copy library to output directories
if not exist "%OUTPUT_DIR%\lib" mkdir "%OUTPUT_DIR%\lib"
copy "%PROJECT_DIR%\bin\%CONFIG%\netstandard2.1\AdvChkSys.dll" "%OUTPUT_DIR%\lib\"
copy "%PROJECT_DIR%\bin\%CONFIG%\netstandard2.1\AdvChkSys.xml" "%OUTPUT_DIR%\lib\"

:: Create platform-specific directories
if "%BUILD_WINDOWS%"=="true" (
    if not exist "%OUTPUT_DIR%\windows" mkdir "%OUTPUT_DIR%\windows"
    copy "%PROJECT_DIR%\bin\%CONFIG%\netstandard2.1\AdvChkSys.dll" "%OUTPUT_DIR%\windows\"
    copy "%PROJECT_DIR%\bin\%CONFIG%\netstandard2.1\AdvChkSys.xml" "%OUTPUT_DIR%\windows\"
    echo ✓ Windows build completed
)

if "%BUILD_LINUX%"=="true" (
    if not exist "%OUTPUT_DIR%\linux" mkdir "%OUTPUT_DIR%\linux"
    copy "%PROJECT_DIR%\bin\%CONFIG%\netstandard2.1\AdvChkSys.dll" "%OUTPUT_DIR%\linux\"
    copy "%PROJECT_DIR%\bin\%CONFIG%\netstandard2.1\AdvChkSys.xml" "%OUTPUT_DIR%\linux\"
    echo ✓ Linux build completed
)

if "%BUILD_MAC%"=="true" (
    if not exist "%OUTPUT_DIR%\macos" mkdir "%OUTPUT_DIR%\macos"
    copy "%PROJECT_DIR%\bin\%CONFIG%\netstandard2.1\AdvChkSys.dll" "%OUTPUT_DIR%\macos\"
    copy "%PROJECT_DIR%\bin\%CONFIG%\netstandard2.1\AdvChkSys.xml" "%OUTPUT_DIR%\macos\"
    echo ✓ macOS build completed
)
echo.

:: Build benchmarks
if "%BUILD_BENCHMARKS%"=="true" (
    echo Building ChunkMark benchmarks...
    
    :: Restore benchmark packages
    dotnet restore "%BENCHMARKS_DIR%\AdvChkSys.Benchmarks.csproj"
    
    :: Build for Windows
    if "%BUILD_WINDOWS%"=="true" (
        dotnet publish "%BENCHMARKS_DIR%\AdvChkSys.Benchmarks.csproj" -c %CONFIG% -r win-x64 --self-contained true -p:PublishSingleFile=true -p:PublishTrimmed=true
        if %ERRORLEVEL% neq 0 (
            echo ✗ Windows benchmark build failed
        ) else (
            if not exist "%OUTPUT_DIR%\benchmarks\windows" mkdir "%OUTPUT_DIR%\benchmarks\windows"
            copy "%BENCHMARKS_DIR%\bin\%CONFIG%\net6.0\win-x64\publish\ChunkMark.exe" "%OUTPUT_DIR%\benchmarks\windows\"
            echo ✓ Windows benchmarks built
        )
    )
    
    :: Build for Linux
    if "%BUILD_LINUX%"=="true" (
        dotnet publish "%BENCHMARKS_DIR%\AdvChkSys.Benchmarks.csproj" -c %CONFIG% -r linux-x64 --self-contained true -p:PublishSingleFile=true -p:PublishTrimmed=true
        if %ERRORLEVEL% neq 0 (
            echo ✗ Linux benchmark build failed
        ) else (
            if not exist "%OUTPUT_DIR%\benchmarks\linux" mkdir "%OUTPUT_DIR%\benchmarks\linux"
            copy "%BENCHMARKS_DIR%\bin\%CONFIG%\net6.0\linux-x64\publish\ChunkMark" "%OUTPUT_DIR%\benchmarks\linux\"
            echo ✓ Linux benchmarks built
        )
    )
    
    :: Build for macOS
    if "%BUILD_MAC%"=="true" (
        dotnet publish "%BENCHMARKS_DIR%\AdvChkSys.Benchmarks.csproj" -c %CONFIG% -r osx-x64 --self-contained true -p:PublishSingleFile=true -p:PublishTrimmed=true
        if %ERRORLEVEL% neq 0 (
            echo ✗ macOS benchmark build failed
        ) else (
            if not exist "%OUTPUT_DIR%\benchmarks\macos" mkdir "%OUTPUT_DIR%\benchmarks\macos"
            copy "%BENCHMARKS_DIR%\bin\%CONFIG%\net6.0\osx-x64\publish\ChunkMark" "%OUTPUT_DIR%\benchmarks\macos\"
            echo ✓ macOS benchmarks built
        )
    )
    echo.
)

:: Build NuGet package
if "%BUILD_NUGET%"=="true" (
    echo Building NuGet package...
    dotnet pack "%PROJECT_DIR%\AdvChkSys.csproj" -c %CONFIG% --no-build --output "%OUTPUT_DIR%\nuget"
    if %ERRORLEVEL% neq 0 (
        echo ✗ NuGet package creation failed
        exit /b 1
    )
    echo ✓ NuGet package created
    echo.
)

:: Build documentation
if "%BUILD_DOCS%"=="true" (
    echo Building documentation...
    echo Documentation generation not implemented yet.
    echo.
)

:: Display summary
echo ╔══════════════════════════════════════════════════════════════╗
echo ║                       BUILD SUMMARY                          ║
echo ╚══════════════════════════════════════════════════════════════╝
echo.
echo Build completed successfully!
echo.
echo Output files:
echo • Library: %OUTPUT_DIR%\lib\AdvChkSys.dll

if "%BUILD_WINDOWS%"=="true" (
    echo • Windows: %OUTPUT_DIR%\windows\AdvChkSys.dll
    if "%BUILD_BENCHMARKS%"=="true" (
        echo • Windows Benchmark: %OUTPUT_DIR%\benchmarks\windows\ChunkMark.exe
    )
)
if "%BUILD_LINUX%"=="true" (
    echo • Linux: %OUTPUT_DIR%\linux\AdvChkSys.dll
    if "%BUILD_BENCHMARKS%"=="true" (
        echo • Linux Benchmark: %OUTPUT_DIR%\benchmarks\linux\ChunkMark
    )
)
if "%BUILD_MAC%"=="true" (
    echo • macOS: %OUTPUT_DIR%\macos\AdvChkSys.dll
    if "%BUILD_BENCHMARKS%"=="true" (
        echo • macOS Benchmark: %OUTPUT_DIR%\benchmarks\macos\ChunkMark
    )
)
if "%BUILD_NUGET%"=="true" (
    echo • NuGet: %OUTPUT_DIR%\nuget\AdvChkSys.%VERSION%.nupkg
)
echo.
echo Run the build script with --help for more options.
echo.

endlocal