# Count how many 404s are caused by @Profile("admin") exclusion
# by checking if the same path exists in fs-admin-saas with admin profile

# Read backend_mappings.txt which has the mapping info
$mappings = Get-Content "d:\ylrz\saasadminui\backend_mappings.txt" -Encoding UTF8

# Parse admin-profiled mappings
$adminMappings = @{}
$companyMappings = @{}

foreach ($line in $mappings) {
    if ($line -match "^\s*(\S+)\s+=>\s+admin\s+\|") {
        $path = $Matches[1]
        $adminMappings[$path] = $line
    } elseif ($line -match "^\s*(\S+)\s+=>\s+company\s+\|") {
        $path = $Matches[1]
        $companyMappings[$path] = $line
    }
}

Write-Host "Admin-only mappings: $($adminMappings.Count)"
Write-Host "Company mappings: $($companyMappings.Count)"

# Read 404 results
$content = Get-Content "d:\ylrz\saasadminui\api_test_v2_results.txt" -Raw
$sections = $content -split "=== "
$notFound = @()
foreach ($section in $sections) {
    if ($section.StartsWith("404")) {
        $notFound = ($section -split "`n" | Where-Object { $_.Trim() -ne "" -and $_ -notmatch "^404" })
    }
}

# Filter GET/POST 404s only
$meaningful404 = $notFound | Where-Object { $_ -match "^(GET|POST)\s+" }

Write-Host "`nGET/POST 404 count: $($meaningful404.Count)"

# Categorize each 404
$profileExcluded = @()
$pathMismatch = @()
$trulyMissing = @()

foreach ($line in $meaningful404) {
    if ($line -match "^\w+\s+(/\S+)(?:\s|\|)") {
        $path = $Matches[1]
        # Remove trailing /
        $cleanPath = $path.TrimEnd('/')
        if ($cleanPath -eq "") { $cleanPath = $path }
        
        # Check if this path exists in admin mappings
        $found = $false
        foreach ($key in $adminMappings.Keys) {
            if ($key.StartsWith($cleanPath) -or $cleanPath.StartsWith($key)) {
                $profileExcluded += "$line => ADMIN: $($adminMappings[$key])"
                $found = $true
                break
            }
        }
        
        if (-not $found) {
            # Check if similar path exists in company mappings
            $similar = $false
            foreach ($key in $companyMappings.Keys) {
                # Extract base module path
                if ($key -match "^/([^/]+)/" -and $cleanPath -match "^/([^/]+)/") {
                    if ($Matches[1] -eq $Matches[2]) {
                        # Same module - could be path mismatch
                        break
                    }
                }
            }
            $trulyMissing += $line
        }
    }
}

Write-Host "`n=== 404 Root Cause Analysis ==="
Write-Host "Caused by @Profile('admin') exclusion: $($profileExcluded.Count)"
Write-Host "Truly missing (no backend controller): $($trulyMissing.Count)"

# Show profile-excluded details by module
Write-Host "`n=== Profile-Excluded by Module ==="
$peByMod = @{}
foreach ($line in $profileExcluded) {
    if ($line -match "^\w+\s+/([^/]+)/") {
        $mod = $Matches[1]
        if (-not $peByMod.ContainsKey($mod)) { $peByMod[$mod] = 0 }
        $peByMod[$mod]++
    }
}
$peByMod.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object { Write-Host "  $($_.Key): $($_.Value)" }

# Show truly missing by module
Write-Host "`n=== Truly Missing by Module ==="
$tmByMod = @{}
foreach ($line in $trulyMissing) {
    if ($line -match "^\w+\s+/([^/]+)/") {
        $mod = $Matches[1]
        if (-not $tmByMod.ContainsKey($mod)) { $tmByMod[$mod] = 0 }
        $tmByMod[$mod]++
    }
}
$tmByMod.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object { Write-Host "  $($_.Key): $($_.Value)" }

# Show sample profile-excluded 404s
Write-Host "`n=== Sample Profile-Excluded 404s ==="
$profileExcluded | Select-Object -First 20 | ForEach-Object { Write-Host "  $_" }

# Show sample truly missing 404s
Write-Host "`n=== Sample Truly Missing 404s ==="
$trulyMissing | Select-Object -First 20 | ForEach-Object { Write-Host "  $_" }
