# Scan all saasadminui API files and extract URLs
$apiDir = 'd:\ylrz\saasadminui\src\api'
$allUrls = [System.Collections.ArrayList]::new()

Get-ChildItem -Path $apiDir -Recurse -Include '*.js' | ForEach-Object {
    $file = $_.FullName
    $relPath = $_.FullName.Replace($apiDir + '\', '')
    $content = Get-Content $file -Raw -Encoding UTF8
    
    # Match url patterns like: url: '/xxx/yyy/zzz' or url: "/xxx/yyy/zzz"
    $matches = [regex]::Matches($content, "url:\s*['""]([^'""]+)['""]")
    foreach ($m in $matches) {
        $url = $m.Groups[1].Value
        # Skip variable interpolation like `${xxx}`
        if ($url -notmatch '\$\{') {
            [void]$allUrls.Add([PSCustomObject]@{
                File = $relPath
                URL = $url
            })
        }
    }
}

Write-Host "Total API URLs found: $($allUrls.Count)"

# Group by URL prefix (first 2 path segments)
$grouped = $allUrls | Group-Object { ($_.URL -split '/')[0..2] -join '/' } | Sort-Object Count -Descending
Write-Host "`n=== URL Prefix Groups ==="
$grouped | Select-Object @{N='Prefix';E={$_.Name}}, Count | Format-Table -AutoSize

# Save all URLs
$allUrls | ConvertTo-Json -Depth 3 | Out-File -FilePath 'd:\ylrz\saasadminui\all_api_urls.json' -Encoding UTF8
Write-Host "`nAll URLs saved to all_api_urls.json"

# Extract unique URL prefixes for testing
$uniquePrefixes = $allUrls | ForEach-Object { ($_.URL -split '/')[0..1] -join '/' } | Sort-Object -Unique
Write-Host "`n=== Unique URL Prefixes (top 2 segments) ==="
$uniquePrefixes
