$p = '19mNqNXIoZ4c3EriFNfzfkgpGNOvLjpOqqZnTFXKnzjxWJ5zURjgJQQJ99CBACAAAAAamI5kAAASAZDOAMPp'
$t = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(':' + $p))
$h = @{ Authorization = 'Basic ' + $t; 'Content-Type' = 'application/json' }
$base = 'https://dev.azure.com/CN-Squad/Invista%20FIDC%20-%20Nexus/_apis'

# 1. Listar aprovacoes pendentes
Write-Host "Buscando aprovacoes pendentes..." -ForegroundColor Cyan
$approvals = Invoke-RestMethod -Uri "$base/pipelines/approvals?state=pending&api-version=7.1-preview.1" -Headers $h

if ($approvals.count -eq 0) {
  Write-Host "Nenhuma aprovacao pendente no momento." -ForegroundColor Yellow
  exit 0
}

Write-Host "Encontradas $($approvals.count) aprovacao(oes) pendente(s):" -ForegroundColor Green
$approvals.value | ForEach-Object {
  Write-Host "  ID: $($_.id)  |  Pipeline: $($_.pipeline.name)  |  Stage: $($_.steps[0].actualApprover.displayName)" -ForegroundColor White
}

# 2. Aprovar todas
Write-Host ""
Write-Host "Aprovando todas..." -ForegroundColor Cyan

$items = @($approvals.value | ForEach-Object {
  @{ approvalId = $_.id; status = 'approved'; comment = 'Aprovado automaticamente via API' }
})
$body = ConvertTo-Json -InputObject $items -Depth 5

$result = Invoke-RestMethod `
  -Uri "$base/pipelines/approvals?api-version=7.1-preview.1" `
  -Method PATCH -Headers $h -Body $body

$result.value | ForEach-Object {
  Write-Host "  $($_.id) -> $($_.status)" -ForegroundColor Green
}

Write-Host ""
Write-Host "Concluido." -ForegroundColor Green
