# Analyze 404 and 500 errors by module
$content = Get-Content "d:\ylrz\saasadminui\api_test_results.txt" -Raw

# Parse sections
$sections = $content -split "=== "

$notFound = @()
$serverErr = @()
$current = $null

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

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

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

# Now check which 404 paths map to actual controller request mappings
# Look for patterns: paths with trailing /, paths with variables
Write-Host "`n=== 404 Path Pattern Analysis ==="
$trailingSlash = 0
$variablePath = 0
$normalPath = 0
foreach ($line in $notFound) {
    if ($line -match "//") { $trailingSlash++ }
    elseif ($line -match "/\d+") { $variablePath++ }
    else { $normalPath++ }
}
Write-Host "  Path with double slash: $trailingSlash"
Write-Host "  Path with numeric ID: $variablePath"
Write-Host "  Normal path: $normalPath"

# Check if 404 paths with trailing / also fail without /
Write-Host "`n=== Sample 404 paths (first 30) ==="
$notFound | Select-Object -First 30 | ForEach-Object { Write-Host "  $_" }

# Check if 500 paths are consistent (same base path always 500)
Write-Host "`n=== Sample 500 paths (first 30) ==="
$serverErr | Select-Object -First 30 | ForEach-Object { Write-Host "  $_" }
