# AcBinary Quick Benchmark Runner
# Run this script to execute all binary serialization benchmarks
# Usage: .\RunQuickBenchmark.ps1 [-All] [-Full] [-WithRef] [-Populate] [-StringIntern] [-MessagePack]

param(
    [switch]$All,
    [switch]$Full,
    [switch]$WithRef,
    [switch]$Populate,
    [switch]$StringIntern,
    [switch]$MessagePack,
    [switch]$Help
)

$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# Colors for output
function Write-ColorOutput($ForegroundColor, $Message) {
    $fc = $host.UI.RawUI.ForegroundColor
    $host.UI.RawUI.ForegroundColor = $ForegroundColor
    Write-Output $Message
    $host.UI.RawUI.ForegroundColor = $fc
}

function Show-Help {
    Write-Host ""
    Write-Host "????????????????????????????????????????????????????????????????????????????????" -ForegroundColor Cyan
    Write-Host "?           AcBinary Quick Benchmark Runner                                    ?" -ForegroundColor Cyan
    Write-Host "????????????????????????????????????????????????????????????????????????????????" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "Usage: .\RunQuickBenchmark.ps1 [options]" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "Options:" -ForegroundColor Green
    Write-Host "  -All           Run all benchmark tests"
    Write-Host "  -Full          Run full AcBinary vs MessagePack comparison"
    Write-Host "  -WithRef       Run WithRef vs NoRef comparison"
    Write-Host "  -Populate      Run Populate & Merge benchmarks"
    Write-Host "  -StringIntern  Run String Interning benchmarks"
    Write-Host "  -MessagePack   Run MessagePack comparison"
    Write-Host "  -Help          Show this help message"
    Write-Host ""
    Write-Host "Examples:" -ForegroundColor Green
    Write-Host "  .\RunQuickBenchmark.ps1 -All                    # Run all tests"
    Write-Host "  .\RunQuickBenchmark.ps1 -Full                   # Full benchmark comparison"
    Write-Host "  .\RunQuickBenchmark.ps1 -WithRef -Populate      # Multiple specific tests"
    Write-Host ""
}

function Run-DotNetTest {
    param(
        [string]$Filter,
        [string]$Description
    )
    
    Write-Host ""
    Write-Host "Running: $Description" -ForegroundColor Yellow
    Write-Host ("=" * 80) -ForegroundColor DarkGray
    
    $testProject = Join-Path $ScriptDir "AyCode.Core.Tests\AyCode.Core.Tests.csproj"
    
    if (-not (Test-Path $testProject)) {
        Write-Host "Error: Test project not found at $testProject" -ForegroundColor Red
        return $false
    }
    
    $result = dotnet test $testProject `
        --filter "FullyQualifiedName~$Filter" `
        --configuration Release `
        --logger "console;verbosity=detailed" `
        --no-build 2>&1
    
    $result | ForEach-Object { Write-Host $_ }
    
    return $LASTEXITCODE -eq 0
}

# Check for help
if ($Help -or ($PSBoundParameters.Count -eq 0)) {
    Show-Help
    if ($PSBoundParameters.Count -eq 0) {
        Write-Host "No options specified. Running full benchmark..." -ForegroundColor Yellow
        $Full = $true
    } else {
        exit 0
    }
}

Write-Host ""
Write-Host "????????????????????????????????????????????????????????????????????????????????" -ForegroundColor Cyan
Write-Host "?           AcBinary Quick Benchmark                                           ?" -ForegroundColor Cyan
Write-Host "????????????????????????????????????????????????????????????????????????????????" -ForegroundColor Cyan
Write-Host ""
Write-Host "Building solution in Release mode..." -ForegroundColor Yellow

# Build first
$buildResult = dotnet build (Join-Path $ScriptDir "AyCode.Core.sln") --configuration Release --verbosity minimal 2>&1
if ($LASTEXITCODE -ne 0) {
    Write-Host "Build failed!" -ForegroundColor Red
    $buildResult | ForEach-Object { Write-Host $_ }
    exit 1
}

Write-Host "Build successful!" -ForegroundColor Green

$testsRun = 0
$testsPassed = 0

# Run requested tests
if ($All) {
    $Full = $true
    $WithRef = $true
    $Populate = $true
    $StringIntern = $true
    $MessagePack = $true
}

if ($Full) {
    $testsRun++
    if (Run-DotNetTest "QuickBenchmark.RunFullBenchmarkComparison" "Full AcBinary vs MessagePack Comparison") {
        $testsPassed++
    }
}

if ($WithRef) {
    $testsRun++
    if (Run-DotNetTest "QuickBenchmark.RunWithRefVsNoRefComparison" "WithRef vs NoRef Comparison") {
        $testsPassed++
    }
}

if ($Populate) {
    $testsRun++
    if (Run-DotNetTest "QuickBenchmark.RunPopulateAndMergeBenchmark" "Populate & Merge Benchmark") {
        $testsPassed++
    }
}

if ($StringIntern) {
    $testsRun++
    if (Run-DotNetTest "QuickBenchmark.RunStringInterningBenchmark" "String Interning Benchmark") {
        $testsPassed++
    }
    $testsRun++
    if (Run-DotNetTest "QuickBenchmark.RunStringInterningVsMessagePack" "String Interning vs MessagePack") {
        $testsPassed++
    }
}

if ($MessagePack) {
    $testsRun++
    if (Run-DotNetTest "QuickBenchmark.RunMessagePackComparison" "MessagePack Comparison") {
        $testsPassed++
    }
}

# Summary
Write-Host ""
Write-Host ("=" * 80) -ForegroundColor DarkGray
Write-Host ""
if ($testsPassed -eq $testsRun) {
    Write-Host "All $testsRun benchmark(s) completed successfully!" -ForegroundColor Green
} else {
    Write-Host "$testsPassed of $testsRun benchmark(s) completed." -ForegroundColor Yellow
}
Write-Host ""
