param([int]$Port = 4000, [int]$MaxWaitSec = 90)

$url = "http://127.0.0.1:$Port/health"

for ($i = 1; $i -le $MaxWaitSec; $i++) {
  try {
    $resp = Invoke-WebRequest -Uri $url -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop
    if ($resp.StatusCode -eq 200) { return 0 }
  } catch { }

  if ($i -eq 1) {
    Write-Host "  Waiting for API on port $Port ..."
  }
  Start-Sleep -Seconds 1
}

Write-Host "[ERROR] API not ready on port $Port"
return 1
