<#
.SYNOPSIS
  Full live collection from ctl.Servers catalog (data-driven) with optional local discovery fallback.
.EXAMPLE
  .\scripts\run-full-collection.ps1
  .\scripts\run-full-collection.ps1 -FallbackDiscover
  .\scripts\run-full-collection.ps1 -ServerList "localhost\SQLEXPRESS"  # explicit override
#>
[CmdletBinding()]
param(
    [string[]]$ServerList = @(),
    [switch]$NoAutoDiscover,
    [switch]$FallbackDiscover,
    [string]$SovereignApi = 'http://localhost:3041',
    [string]$PilotConfigPath
)

$ErrorActionPreference = 'Stop'

if ($ServerList.Count -gt 0) {
    $repoRoot = Split-Path $PSScriptRoot -Parent
    $collector = Join-Path $repoRoot 'src\collectors\DBAOps-Sovereign-Collector.ps1'
    Write-Host '=== DBAOps Sovereign — Explicit server list collection ===' -ForegroundColor Cyan
    try {
        Invoke-RestMethod -Uri "$SovereignApi/api/health" -TimeoutSec 8 | Out-Null
    } catch {
        throw "API not running at $SovereignApi - run npm start first."
    }
    & $collector -ServerList $ServerList -SovereignApi $SovereignApi -SubmitAll
    Write-Host ''
    Write-Host 'Collection complete. Open http://localhost:3000' -ForegroundColor Green
    exit 0
}

$invokeArgs = @{
    SovereignApi = $SovereignApi
    SubmitAll = $true
}
if ($FallbackDiscover -or -not $NoAutoDiscover) {
    $invokeArgs.FallbackDiscover = $true
}

& (Join-Path $PSScriptRoot 'Invoke-SovereignCollection.ps1') @invokeArgs

Write-Host 'Open http://localhost:3000' -ForegroundColor Green
