param(
    [string[]]$ProviderNames = @()
)

. (Join-Path $PSScriptRoot "Common.ps1")

function Get-ResponseText {
    param($Response)

    if ($null -eq $Response) {
        return ""
    }

    if ($Response.content) {
        $textBlocks = @($Response.content | Where-Object { $_.type -eq "text" -and $_.text })
        if ($textBlocks.Count -gt 0) {
            return (($textBlocks | ForEach-Object { [string]$_.text }) -join " ").Trim()
        }

        $fallbackBlocks = @($Response.content | Where-Object { $_.thinking -or $_.text })
        if ($fallbackBlocks.Count -gt 0) {
            $parts = foreach ($block in $fallbackBlocks) {
                if ($block.text) { [string]$block.text }
                elseif ($block.thinking) { [string]$block.thinking }
            }
            return (($parts | Where-Object { $_ }) -join " ").Trim()
        }
    }

    return (($Response | ConvertTo-Json -Depth 20) -replace "\s+", " ").Trim()
}

$config = Get-ProvidersConfig
if ($ProviderNames.Count -eq 0) {
    $ProviderNames = @($config.providers.PSObject.Properties.Name)
}
else {
    $expanded = @()
    foreach ($entry in $ProviderNames) {
        if ($null -ne $entry) {
            $expanded += ([string]$entry).Split(",") | ForEach-Object { $_.Trim() } | Where-Object { $_ }
        }
    }
    $ProviderNames = $expanded
}

$results = @()

foreach ($name in $ProviderNames) {
    $provider = Get-ProviderConfig -Name $name
    $endpoint = ([string]$provider.base_url).TrimEnd("/") + "/v1/messages"

    $headers = @{
        "x-api-key" = [string]$provider.auth_token
        "anthropic-version" = "2023-06-01"
        "content-type" = "application/json"
    }

    $body = @{
        model = [string]$provider.model
        max_tokens = 16
        messages = @(
            @{
                role = "user"
                content = "Respond with exactly OK and nothing else."
            }
        )
    } | ConvertTo-Json -Depth 10

    $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
    try {
        $response = Invoke-RestMethod -Method Post -Uri $endpoint -Headers $headers -Body $body -TimeoutSec 120
        $stopwatch.Stop()
        $text = Get-ResponseText -Response $response
        $semanticOk = ($text.Trim() -eq "OK")

        $results += [ordered]@{
            provider = $name
            model = [string]$provider.model
            endpoint = $endpoint
            http_ok = $true
            semantic_ok = $semanticOk
            ok = $semanticOk
            latency_ms = $stopwatch.ElapsedMilliseconds
            response = $text.Trim()
        }
    }
    catch {
        $stopwatch.Stop()
        $results += [ordered]@{
            provider = $name
            model = [string]$provider.model
            endpoint = $endpoint
            http_ok = $false
            semantic_ok = $false
            ok = $false
            latency_ms = $stopwatch.ElapsedMilliseconds
            response = $_.Exception.Message
        }
    }
}

$outputPath = Join-Path (Get-RalphRoot) "state\provider_smoke.json"
Write-Utf8File -Path $outputPath -Content ((@($results) | ConvertTo-Json -Depth 10) + "`n")
Get-Content -Raw -Path $outputPath
