# Classify errors from api_errors_final.txt and save to file
$lines = Get-Content 'd:\ylrz\saasadminui\api_errors_final.txt' -Encoding UTF8

$err404 = [System.Collections.ArrayList]::new()
$err500 = [System.Collections.ArrayList]::new()
$errTimeout = [System.Collections.ArrayList]::new()
$err403 = [System.Collections.ArrayList]::new()
$errOther = [System.Collections.ArrayList]::new()

foreach ($line in $lines) {
    if ($line -match '^404\|') { [void]$err404.Add($line) }
    elseif ($line -match '^500\|') { [void]$err500.Add($line) }
    elseif ($line -match '^0\|') { [void]$errTimeout.Add($line) }
    elseif ($line -match '^403\|') { [void]$err403.Add($line) }
    elseif ($line -match '^\d+\|') { [void]$errOther.Add($line) }
}

$out = [System.Collections.ArrayList]::new()
[void]$out.Add("=== ERROR SUMMARY ===")
[void]$out.Add("404 NOT FOUND: $($err404.Count)")
[void]$out.Add("500 SERVER ERROR: $($err500.Count)")
[void]$out.Add("0 TIMEOUT/CONNECTION: $($errTimeout.Count)")
[void]$out.Add("403 FORBIDDEN: $($err403.Count)")
[void]$out.Add("OTHER: $($errOther.Count)")
[void]$out.Add("")

# 404 prefix groups
[void]$out.Add("=== 404 URL PREFIX GROUPS ===")
$prefix404 = @{}
foreach ($line in $err404) {
    if ($line -match '404\|(/[^/]+/[^/]+)') { 
        $p = $matches[1]
        if (-not $prefix404.ContainsKey($p)) { $prefix404[$p] = 0 }
        $prefix404[$p]++
    } elseif ($line -match '404\|(/[^/]+)') {
        $p = $matches[1]
        if (-not $prefix404.ContainsKey($p)) { $prefix404[$p] = 0 }
        $prefix404[$p]++
    }
}
$prefix404.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object { [void]$out.Add("$($_.Key): $($_.Value)") }

# Timeout prefix groups
[void]$out.Add("")
[void]$out.Add("=== TIMEOUT URL PREFIX GROUPS ===")
$prefixTimeout = @{}
foreach ($line in $errTimeout) {
    if ($line -match '0\|([^/|]+)') {
        $p = '/' + $matches[1]
        if (-not $prefixTimeout.ContainsKey($p)) { $prefixTimeout[$p] = 0 }
        $prefixTimeout[$p]++
    }
}
$prefixTimeout.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object { [void]$out.Add("$($_.Key): $($_.Value)") }

# 500 error types
[void]$out.Add("")
[void]$out.Add("=== 500 ERROR TYPES ===")
$err500Types = @{}
foreach ($line in $err500) {
    if ($line -match "Request method 'GET' not supported") { $t = 'GET_NOT_SUPPORTED' }
    elseif ($line -match 'Failed to convert.*NumberFormatException') { $t = 'PATH_PARAM_CONFLICT' }
    elseif ($line -match 'Unknown column') { $t = 'SQL_COLUMN_MISSING' }
    elseif ($line -match 'Required request parameter') { $t = 'MISSING_PARAM' }
    elseif ($line -match 'bad SQL grammar') { $t = 'SQL_ERROR' }
    else { $t = 'OTHER' }
    if (-not $err500Types.ContainsKey($t)) { $err500Types[$t] = 0 }
    $err500Types[$t]++
}
$err500Types.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object { [void]$out.Add("$($_.Key): $($_.Value)") }

# All 404
[void]$out.Add("")
[void]$out.Add("=== ALL 404 ERRORS ===")
$err404 | ForEach-Object { [void]$out.Add($_) }

# All 500
[void]$out.Add("")
[void]$out.Add("=== ALL 500 ERRORS ===")
$err500 | ForEach-Object { [void]$out.Add($_) }

# All timeout
[void]$out.Add("")
[void]$out.Add("=== ALL TIMEOUT ERRORS ===")
$errTimeout | ForEach-Object { [void]$out.Add($_) }

# All 403
[void]$out.Add("")
[void]$out.Add("=== ALL 403 ERRORS ===")
$err403 | ForEach-Object { [void]$out.Add($_) }

$out | Out-File -FilePath 'd:\ylrz\saasadminui\error_analysis.txt' -Encoding UTF8
Write-Host "Analysis saved to error_analysis.txt"
Write-Host "404: $($err404.Count), 500: $($err500.Count), Timeout: $($errTimeout.Count), 403: $($err403.Count), Other: $($errOther.Count)"
