# Filter meaningful 404 and 500 errors (exclude DELETE without ID, trailing slash, etc.)
$content = Get-Content "d:\ylrz\saasadminui\api_test_v2_results.txt" -Raw
$sections = $content -split "=== "

$notFound = @()
$serverErr = @()
foreach ($section in $sections) {
    if ($section.StartsWith("404")) {
        $notFound = ($section -split "`n" | Where-Object { $_.Trim() -ne "" -and $_ -notmatch "^404" })
    } elseif ($section.StartsWith("500")) {
        $serverErr = ($section -split "`n" | Where-Object { $_.Trim() -ne "" -and $_ -notmatch "^500" })
    }
}

# Filter 404: only GET and POST (these are the real page/list queries)
$meaningful404 = $notFound | Where-Object { $_ -match "^(GET|POST)\s+" }
$delete404 = $notFound | Where-Object { $_ -match "^DELETE\s+" }
$put404 = $notFound | Where-Object { $_ -match "^PUT\s+" }

Write-Host "=== 404 Breakdown ==="
Write-Host "GET/POST 404: $($meaningful404.Count)  <-- REAL ISSUES"
Write-Host "DELETE 404: $($delete404.Count)"
Write-Host "PUT 404: $($put404.Count)"

# Filter 500: categorize by error type
$methodNotSupported = $serverErr | Where-Object { $_ -match "not supported" }
$numberFormat = $serverErr | Where-Object { $_ -match "NumberFormatException|Failed to convert" }
$bodyMissing = $serverErr | Where-Object { $_ -match "Required request body is missing|Required request parameter" }
$businessErr = $serverErr | Where-Object { $_ -match "操作失败|业务" }
$mybatisErr = $serverErr | Where-Object { $_ -match "mybatis|BuilderException|ReflectionException" }
$jsonErr = $serverErr | Where-Object { $_ -match "JSON parse error|Cannot deserialize" }
$other500 = $serverErr | Where-Object { 
    $_ -notmatch "not supported" -and 
    $_ -notmatch "NumberFormatException|Failed to convert" -and 
    $_ -notmatch "Required request body|Required request parameter" -and
    $_ -notmatch "操作失败" -and
    $_ -notmatch "mybatis|BuilderException|ReflectionException" -and
    $_ -notmatch "JSON parse error|Cannot deserialize"
}

Write-Host "`n=== 500 Breakdown ==="
Write-Host "Method not supported: $($methodNotSupported.Count)  <-- Test artifact, not real error"
Write-Host "NumberFormatException (path var mismatch): $($numberFormat.Count)  <-- Path variable issue"
Write-Host "Missing request body/param: $($bodyMissing.Count)  <-- Test artifact, needs proper params"
Write-Host "Business error: $($businessErr.Count)  <-- REAL ISSUES"
Write-Host "MyBatis error: $($mybatisErr.Count)  <-- REAL ISSUES (SQL/mapping problems)"
Write-Host "JSON deserialize error: $($jsonErr.Count)  <-- REAL ISSUES"
Write-Host "Other 500: $($other500.Count)  <-- NEEDS INVESTIGATION"

# Show GET/POST 404 by module
Write-Host "`n=== GET/POST 404 by Module ==="
$nfByMod = @{}
foreach ($line in $meaningful404) {
    if ($line -match "\s+/([^/]+)/") {
        $mod = $Matches[1]
        if (-not $nfByMod.ContainsKey($mod)) { $nfByMod[$mod] = 0 }
        $nfByMod[$mod]++
    }
}
$nfByMod.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object { Write-Host "  $($_.Key): $($_.Value)" }

# Show MyBatis errors detail
Write-Host "`n=== MyBatis Errors ==="
$mybatisErr | ForEach-Object { Write-Host "  $_" }

# Show real business errors
Write-Host "`n=== Business Errors ==="
$businessErr | Select-Object -First 20 | ForEach-Object { Write-Host "  $_" }

# Show other 500 that need investigation
Write-Host "`n=== Other 500 Errors (need investigation) ==="
$other500 | Select-Object -First 20 | ForEach-Object { Write-Host "  $_" }

# Show meaningful 404 samples
Write-Host "`n=== GET/POST 404 Samples ==="
$meaningful404 | Sort-Object | Select-Object -First 40 | ForEach-Object { Write-Host "  $_" }

# Real error count summary
$real404 = $meaningful404.Count
$real500 = $businessErr.Count + $mybatisErr.Count + $jsonErr.Count + $other500.Count
$testArtifact500 = $methodNotSupported.Count + $numberFormat.Count + $bodyMissing.Count

Write-Host "`n========================================"
Write-Host "=== REAL ERROR SUMMARY ==="
Write-Host "========================================"
Write-Host "Real 404 (GET/POST paths missing): $real404"
Write-Host "Real 500 (business/mybatis/json/other): $real500"
Write-Host "Test artifact 500 (method/param mismatch): $testArtifact500"
Write-Host ""
Write-Host "Total real issues to fix: $($real404 + $real500)"
