param(
    [string]$UiBaseUrl = "http://192.168.220.91:8765",
    [string]$ModelId = "qwen3-coder-30b-a3b-instruct-q6_k",
    [string]$Plugin = "1c",
    [string[]]$Profiles = @("gpu-fast", "cpu-test"),
    [string]$Report = "reports/benchmarks/runtime-preflight-latest.json",
    [int]$TimeoutSec = 60
)

$ErrorActionPreference = "Stop"

$root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
$reportPath = Join-Path $root $Report
$url = "$($UiBaseUrl.TrimEnd('/'))/api/benchmark/preflight"
$payload = @{
    model_id = $ModelId
    plugin = $Plugin
    profiles = $Profiles
} | ConvertTo-Json -Depth 8

Write-Host "Runtime benchmark preflight"
Write-Host "URL: $url"
Write-Host "Model: $ModelId"
Write-Host "Plugin: $Plugin"
Write-Host "Profiles: $($Profiles -join ', ')"
Write-Host ""

try {
    $result = Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json" -Body $payload -TimeoutSec $TimeoutSec
} catch {
    $response = $_.Exception.Response
    if ($response) {
        try {
            $stream = $response.GetResponseStream()
            $reader = [System.IO.StreamReader]::new($stream)
            $body = $reader.ReadToEnd()
            if ($body) {
                $result = $body | ConvertFrom-Json
            } else {
                throw
            }
        } catch {
            throw $_.Exception
        }
    } else {
        throw
    }
}

New-Item -ItemType Directory -Force (Split-Path -Parent $reportPath) | Out-Null
$result | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $reportPath -Encoding UTF8

foreach ($check in @($result.checks)) {
    $target = $check.target
    $served = if ($target) { $target.served_model_name } else { "-" }
    $endpoint = if ($target) { $target.base_url } else { "-" }
    $latency = if ($check.latency_ms -ne $null) { "$($check.latency_ms)ms" } else { "-" }
    $status = if ($check.available) { "ready" } else { $check.status }
    Write-Host ("{0,-12} {1,-14} {2,6} {3} {4}" -f $check.profile_id, $status, $latency, $endpoint, $served)
    if (-not $check.available -and $check.error) {
        Write-Host "  error: $($check.error)"
    }
}

Write-Host ""
Write-Host "Status: $($result.status)"
Write-Host "Report: $reportPath"

if (-not $result.ready) {
    exit 1
}
