# Find truly conflicting paths between fs-admin-saas @Profile("admin") controllers
# and fs-company own controllers

# Step 1: Get all @RequestMapping paths from fs-company's OWN source
$companyPath = "d:\ylrz\ylrz_saas_his_scrm\fs-company\src\main\java\com\fs"
$companyControllers = Get-ChildItem -Path $companyPath -Recurse -Filter "*Controller.java"

$companyPaths = @{}
foreach ($file in $companyControllers) {
    $content = Get-Content $file.FullName -Raw -Encoding UTF8
    if ($content -match '@RequestMapping\s*\(\s*(?:value\s*=\s*)?"/([^"]+)"') {
        $path = "/" + $Matches[1]
        $relPath = $file.FullName.Substring($companyPath.Length + 1)
        $companyPaths[$path] = $relPath
    }
}

Write-Host "fs-company own controller paths: $($companyPaths.Count)"

# Step 2: Get all @RequestMapping paths from fs-admin-saas controllers that have @Profile("admin")
$saasPath = "d:\ylrz\ylrz_saas_his_scrm\fs-admin-saas\src\main\java\com\fs"
$saasControllers = Get-ChildItem -Path $saasPath -Recurse -Filter "*Controller.java"

$adminProfilePaths = @{}
$companyProfilePaths = @{}
$noProfilePaths = @{}

foreach ($file in $saasControllers) {
    $content = Get-Content $file.FullName -Raw -Encoding UTF8
    $path = ""
    if ($content -match '@RequestMapping\s*\(\s*(?:value\s*=\s*)?"/([^"]+)"') {
        $path = "/" + $Matches[1]
    }
    if ($path -eq "") { continue }
    
    $relPath = $file.FullName.Substring($saasPath.Length + 1)
    
    if ($content -match '@Profile\s*\(\s*"admin"\s*\)') {
        $adminProfilePaths[$path] = $relPath
    } elseif ($content -match '@Profile\s*\(\s*"company"\s*\)') {
        $companyProfilePaths[$path] = $relPath
    } else {
        $noProfilePaths[$path] = $relPath
    }
}

Write-Host "fs-admin-saas @Profile(admin) paths: $($adminProfilePaths.Count)"
Write-Host "fs-admin-saas @Profile(company) paths: $($companyProfilePaths.Count)"
Write-Host "fs-admin-saas no @Profile paths: $($noProfilePaths.Count)"

# Step 3: Find conflicts - paths in @Profile("admin") that ALSO exist in fs-company own source
$conflicts = @{}
foreach ($path in $adminProfilePaths.Keys) {
    if ($companyPaths.ContainsKey($path)) {
        $conflicts[$path] = "admin: $($adminProfilePaths[$path]) vs company: $($companyPaths[$path])"
    }
    # Also check against @Profile("company") paths
    if ($companyProfilePaths.ContainsKey($path)) {
        $conflicts[$path] = "admin: $($adminProfilePaths[$path]) vs company-profile: $($companyProfilePaths[$path])"
    }
}

Write-Host "`n=== CONFLICTING PATHS (same path in admin + company) ==="
Write-Host "Total conflicts: $($conflicts.Count)"
$conflicts.GetEnumerator() | Sort-Object Name | ForEach-Object { Write-Host "  $($_.Name): $($_.Value)" }

# Step 4: Non-conflicting admin paths that can be safely changed to @Profile({"admin","company"})
$nonConflicting = @{}
foreach ($path in $adminProfilePaths.Keys) {
    if (-not $conflicts.ContainsKey($path)) {
        $nonConflicting[$path] = $adminProfilePaths[$path]
    }
}

Write-Host "`n=== NON-CONFLICTING ADMIN PATHS (can add 'company' profile) ==="
Write-Host "Total: $($nonConflicting.Count)"

# Group by module
$ncByMod = @{}
foreach ($path in $nonConflicting.Keys) {
    if ($path -match "^/([^/]+)") {
        $mod = $Matches[1]
        if (-not $ncByMod.ContainsKey($mod)) { $ncByMod[$mod] = @() }
        $ncByMod[$mod] += $path
    }
}

Write-Host "`nNon-conflicting by module:"
$ncByMod.GetEnumerator() | Sort-Object { $_.Value.Count } -Descending | ForEach-Object { Write-Host "  $($_.Key): $($ncByMod[$_.Key].Count)" }

# Step 5: For the conflicting paths, decide which version should win
Write-Host "`n=== CONFLICT RESOLUTION NEEDED ==="
$conflicts.GetEnumerator() | Sort-Object Name | ForEach-Object { Write-Host "  $($_.Name)" }
