param(
    [ValidateSet("install", "check", "remove")]
    [string] $Mode = "install"
)

$ErrorActionPreference = "Continue"
$root = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
Write-Host "RepoKey MCP registration (all servers)"
Write-Host ""

& powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot "register_mcp.ps1") -Mode $Mode
if ($LASTEXITCODE -ne 0) { Write-Host "[WARN] repokey-vivado registration exited $LASTEXITCODE" }

& powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot "register_debug_mcp.ps1") -Mode $Mode
if ($LASTEXITCODE -ne 0) { Write-Host "[WARN] vivado-debug registration exited $LASTEXITCODE" }

& powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot "register_stack_mcp.ps1") -Mode $Mode
if ($LASTEXITCODE -ne 0) { Write-Host "[WARN] AI-stack MCP registration exited $LASTEXITCODE" }

if ($Mode -eq "install") {
    Write-Host ""
    Write-Host "Verifying MCP server reachability..."
    $client = Join-Path $root "repokey-client.cmd"
    if (Test-Path -LiteralPath $client) {
        & $client mcp-selfcheck
        if ($LASTEXITCODE -ne 0) { Write-Host "[WARN] mcp-selfcheck failed (exit $LASTEXITCODE); see diagnose for support." }
    } else {
        Write-Host "[WARN] repokey-client.cmd not found; cannot self-check."
    }
}

Write-Host ""
Write-Host "[OK] Registration pass finished."
Write-Host "[INFO] Restart the AI client once so it reloads MCP configuration."
