# Extract all @RequestMapping paths from fs-admin-saas source code
# and compare with frontend API paths

$basePath = "d:\ylrz\ylrz_saas_his_scrm\fs-admin-saas\src\main\java\com\fs"
$controllers = Get-ChildItem -Path $basePath -Recurse -Filter "*Controller.java"

$backendMappings = @{}

foreach ($file in $controllers) {
    $content = Get-Content $file.FullName -Raw -Encoding UTF8
    
    # Extract class-level @RequestMapping
    $classMapping = ""
    if ($content -match '@RequestMapping\(["\s]*\(\s*["\x27]([^"\x27]+)["\x27]') {
        $classMapping = $Matches[1]
    } elseif ($content -match '@RequestMapping\s*\(\s*value\s*=\s*["\x27]([^"\x27]+)["\x27]') {
        $classMapping = $Matches[1]
    }
    
    # Check if controller has @Profile that excludes it from company
    $hasAdminProfile = $content -match '@Profile\s*\(\s*["\x27]admin["\x27]\s*\)'
    
    # Extract method-level mappings
    $methodMatches = [regex]::Matches($content, '@(GetMapping|PostMapping|PutMapping|DeleteMapping|RequestMapping)\s*\(\s*(?:value\s*=\s*)?["\x27]([^"\x27]+)["\x27]')
    
    foreach ($m in $methodMatches) {
        $methodPath = $m.Groups[2].Value
        $fullPath = "$classMapping$methodPath"
        # Normalize: remove trailing slash, ensure starts with /
        $fullPath = $fullPath -replace '/+', '/'
        if (-not $fullPath.StartsWith('/')) { $fullPath = "/$fullPath" }
        
        $profile = if ($hasAdminProfile) { "admin" } else { "company" }
        $relPath = $file.FullName.Substring($basePath.Length + 1)
        if (-not $backendMappings.ContainsKey($fullPath)) {
            $backendMappings[$fullPath] = "$profile | $relPath"
        }
    }
    
    # Also handle class-level only (no method paths)
    if ($classMapping -and $methodMatches.Count -eq 0) {
        $fullPath = $classMapping -replace '/+', '/'
        if (-not $fullPath.StartsWith('/')) { $fullPath = "/$fullPath" }
        $profile = if ($hasAdminProfile) { "admin" } else { "company" }
        $relPath = $file.FullName.Substring($basePath.Length + 1)
        if (-not $backendMappings.ContainsKey($fullPath)) {
            $backendMappings[$fullPath] = "$profile | $relPath"
        }
    }
}

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

# Also scan fs-company's own controllers
$companyPath = "d:\ylrz\ylrz_saas_his_scrm\fs-company\src\main\java\com\fs"
$companyControllers = Get-ChildItem -Path $companyPath -Recurse -Filter "*Controller.java"

foreach ($file in $companyControllers) {
    $content = Get-Content $file.FullName -Raw -Encoding UTF8
    
    $classMapping = ""
    if ($content -match '@RequestMapping\(["\s]*\(\s*["\x27]([^"\x27]+)["\x27]') {
        $classMapping = $Matches[1]
    } elseif ($content -match '@RequestMapping\s*\(\s*value\s*=\s*["\x27]([^"\x27]+)["\x27]') {
        $classMapping = $Matches[1]
    }
    
    $methodMatches = [regex]::Matches($content, '@(GetMapping|PostMapping|PutMapping|DeleteMapping|RequestMapping)\s*\(\s*(?:value\s*=\s*)?["\x27]([^"\x27]+)["\x27]')
    
    foreach ($m in $methodMatches) {
        $methodPath = $m.Groups[2].Value
        $fullPath = "$classMapping$methodPath"
        $fullPath = $fullPath -replace '/+', '/'
        if (-not $fullPath.StartsWith('/')) { $fullPath = "/$fullPath" }
        
        $relPath = "company:" + $file.FullName.Substring($companyPath.Length + 1)
        if (-not $backendMappings.ContainsKey($fullPath)) {
            $backendMappings[$fullPath] = "company | $relPath"
        }
    }
}

Write-Host "Total backend mappings (with fs-company): $($backendMappings.Count)"

# Count by profile
$adminMappings = ($backendMappings.Values | Where-Object { $_ -match "^admin" }).Count
$companyMappings = ($backendMappings.Values | Where-Object { $_ -match "^company" }).Count
Write-Host "Admin-only mappings: $adminMappings"
Write-Host "Company mappings: $companyMappings"

# List all mappings sorted
$backendMappings.Keys | Sort-Object | Select-Object -First 50 | ForEach-Object { Write-Host "  $_ => $($backendMappings[$_])" }

# Save all mappings
$out = [System.Collections.ArrayList]::new()
[void]$out.Add("Total backend mappings: $($backendMappings.Count)")
[void]$out.Add("Admin-only: $adminMappings")
[void]$out.Add("Company: $companyMappings")
[void]$out.Add("")
foreach ($key in ($backendMappings.Keys | Sort-Object)) {
    [void]$out.Add("$key => $($backendMappings[$key])")
}
[System.IO.File]::WriteAllLines("d:\ylrz\saasadminui\backend_mappings.txt", $out, (New-Object System.Text.UTF8Encoding($false)))
Write-Host "`nMappings saved to d:\ylrz\saasadminui\backend_mappings.txt"
