# Analyze 404 APIs - check which ones are due to excludeFilters vs truly missing controllers

$notFound = Get-Content "d:\ylrz\saasadminui\api_404.txt"

# Check which of these paths exist in fs-admin-saas controllers but are excluded by @Profile("admin") or excludeFilters
$saasSrc = "d:\ylrz\ylrz_saas_his_scrm\fs-admin-saas\src\main\java\com\fs"
$companySrc = "d:\ylrz\ylrz_saas_his_scrm\fs-company\src\main\java\com\fs"
$serviceSrc = "d:\ylrz\ylrz_saas_his_scrm\fs-service\src\main\java\com\fs"

# Excluded packages by excludeFilters
$excludedPackages = @(
    "admin\controller",
    "web\controller",
    "api\controller",
    "crm\controller",
    "qw\controller",
    "live\controller",
    "course\controller",
    "fastGpt",
    "fastgptApi",
    "user\controller"
)

# Find all @RequestMapping paths in fs-admin-saas
Write-Host "=== Scanning fs-admin-saas controller paths ==="
$saasMappings = @{}
Get-ChildItem -Path $saasSrc -Filter "*.java" -Recurse | ForEach-Object {
    $content = Get-Content $_.FullName -Raw -Encoding UTF8
    $relPath = $_.FullName.Substring($saasSrc.Length + 1)
    
    # Get class-level @RequestMapping
    $classMapping = ""
    if ($content -match '@RequestMapping\("([^"]+)"\)') {
        $classMapping = $matches[1]
    }
    
    # Check if excluded by excludeFilters
    $isExcluded = $false
    foreach ($pkg in $excludedPackages) {
        if ($relPath -like "$pkg*") {
            $isExcluded = $true
            break
        }
    }
    
    # Check @Profile
    $profile = ""
    if ($content -match '@Profile\("([^"]+)"\)') {
        $profile = $matches[1]
    } elseif ($content -match '@Profile\(\{([^}]+)\}\)') {
        $profile = $matches[1]
    }
    
    if ($classMapping -ne "") {
        $saasMappings[$classMapping] = @{
            File = $relPath
            Excluded = $isExcluded
            Profile = $profile
        }
    }
}

# Check which 404 paths match saas controllers
$excluded404 = @{}
$missing404 = @{}

foreach ($path in $notFound) {
    if ($path -match "^(/[^/]+/[^\?]+)") {
        $basePath = $matches[1]
    } else {
        $basePath = $path
    }
    
    $found = $false
    foreach ($mapping in $saasMappings.Keys) {
        if ($path -like "$mapping*" -or $basePath -like "$mapping*") {
            $found = $true
            if ($saasMappings[$mapping].Excluded -or $saasMappings[$mapping].Profile -eq "admin") {
                if (-not $excluded404.ContainsKey($mapping)) {
                    $excluded404[$mapping] = @{ Count = 0; File = $saasMappings[$mapping].File; Profile = $saasMappings[$mapping].Profile }
                }
                $excluded404[$mapping].Count++
            }
            break
        }
    }
    
    if (-not $found) {
        $prefix = if ($path -match "^(/[^/]+/[^\?/]+)") { $matches[1] } else { $path }
        if (-not $missing404.ContainsKey($prefix)) {
            $missing404[$prefix] = 0
        }
        $missing404[$prefix]++
    }
}

Write-Host "`n=== 404s caused by excluded @Profile('admin') controllers ==="
$excluded404.GetEnumerator() | Sort-Object { $_.Value.Count } -Descending | ForEach-Object {
    Write-Host "  $($_.Key) ($($_.Value.Count) APIs) - File: $($_.Value.File) - Profile: $($_.Value.Profile)"
}

Write-Host "`n=== 404s with NO matching controller (truly missing) ==="
$missing404.GetEnumerator() | Sort-Object Value -Descending | Select-Object -First 30 | ForEach-Object {
    Write-Host "  $($_.Key): $($_.Value)"
}

Write-Host "`nTotal excluded-profile 404s: $(($excluded404.GetEnumerator() | Measure-Object -Property { $_.Value.Count } -Sum).Sum)"
Write-Host "Total truly-missing 404s: $(($missing404.GetEnumerator() | Measure-Object -Property Value -Sum).Sum)"
