# Better extraction: properly combine class + method request mappings
$basePath = "d:\ylrz\ylrz_saas_his_scrm\fs-admin-saas\src\main\java\com\fs"
$companyBasePath = "d:\ylrz\ylrz_saas_his_scrm\fs-company\src\main\java\com\fs"

$backendMappings = @{}

function Process-Controllers {
    param($searchPath, $label)
    $controllers = Get-ChildItem -Path $searchPath -Recurse -Filter "*Controller.java"
    
    foreach ($file in $controllers) {
        $content = Get-Content $file.FullName -Raw -Encoding UTF8
        
        # Skip admin-profiled controllers when we care about company
        $hasAdminProfile = $content -match '@Profile\s*\(\s*["\x27]admin["\x27]\s*\)'
        $hasCompanyProfile = $content -match '@Profile\s*\(\s*["\x27]company["\x27]\s*\)'
        
        # Extract class-level @RequestMapping
        $classMapping = ""
        if ($content -match '(?s)@RequestMapping\s*\(\s*(?:value\s*=\s*)?["\x27](/[^"\x27]+)["\x27]') {
            $classMapping = $Matches[1]
        }
        
        if ($classMapping -eq "") { continue }
        
        $profile = if ($hasAdminProfile) { "admin" } elseif ($hasCompanyProfile) { "company" } else { "none" }
        
        # Get all method mappings with their HTTP methods
        # Split content by method annotations to get individual methods
        $methods = $content -split '@(GetMapping|PostMapping|PutMapping|DeleteMapping|RequestMapping)'
        
        $i = 0
        while ($i -lt $methods.Count) {
            $annotation = $methods[$i]
            $i++
            if ($i -ge $methods.Count) { break }
            $methodBody = $methods[$i]
            $i++
            
            # Extract path from annotation value
            $methodPath = ""
            if ($methodBody -match '^\s*\(\s*(?:value\s*=\s*)?["\x27]([^"\x27]+)["\x27]') {
                $methodPath = $Matches[1]
            } elseif ($methodBody -match '^\s*\(\s*(?:path|value)\s*=\s*["\x27]([^"\x27]+)["\x27]') {
                $methodPath = $Matches[1]
            }
            
            if ($methodPath -eq "") { continue }
            
            $fullPath = "$classMapping$methodPath"
            $fullPath = $fullPath -replace '/+', '/'
            
            $httpMethod = switch ($annotation) {
                "GetMapping" { "GET" }
                "PostMapping" { "POST" }
                "PutMapping" { "PUT" }
                "DeleteMapping" { "DELETE" }
                "RequestMapping" { "ANY" }
                default { "?" }
            }
            
            $key = "$httpMethod $fullPath"
            $relPath = "${label}:" + $file.FullName.Substring($searchPath.Length + 1)
            if (-not $backendMappings.ContainsKey($key)) {
                $backendMappings[$key] = "$profile | $relPath"
            }
        }
        
        # If class has @RequestMapping but no method paths, add the class path itself
        # This handles cases where methods use inherited paths
    }
}

Process-Controllers -searchPath $basePath -label "saas"
Process-Controllers -searchPath $companyBasePath -label "company"

Write-Host "Total unique mappings: $($backendMappings.Count)"

# Group by module (first segment of path)
$byModule = @{}
foreach ($key in $backendMappings.Keys) {
    if ($key -match '^\w+\s+(/[^/]+)') {
        $mod = $Matches[1]
        if (-not $byModule.ContainsKey($mod)) { $byModule[$mod] = 0 }
        $byModule[$mod]++
    }
}

Write-Host "`n=== Mappings by Module ==="
$byModule.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object { Write-Host "  $($_.Key): $($_.Value)" }

# Count active (non-admin) mappings
$activeMappings = $backendMappings.Keys | Where-Object { $backendMappings[$_] -notmatch "^admin" }
Write-Host "`nActive (company) mappings: $($activeMappings.Count)"

# List company mappings
Write-Host "`n=== Company/Active Mappings (first 50) ==="
$activeMappings | Sort-Object | Select-Object -First 50 | ForEach-Object { Write-Host "  $_ => $($backendMappings[$_])" }

# Save
$out = [System.Collections.ArrayList]::new()
[void]$out.Add("Total: $($backendMappings.Count)")
foreach ($key in ($backendMappings.Keys | Sort-Object)) {
    [void]$out.Add("$key => $($backendMappings[$key])")
}
[System.IO.File]::WriteAllLines("d:\ylrz\saasadminui\backend_mappings_v2.txt", $out, (New-Object System.Text.UTF8Encoding($false)))
Write-Host "`nSaved to backend_mappings_v2.txt"
