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

foreach ($buildId in @(6181, 6182, 6185, 6186)) {
  $b  = Invoke-RestMethod -Uri "$base/build/builds/$buildId`?api-version=7.0" -Headers $h
  $tl = Invoke-RestMethod -Uri "$base/build/builds/$buildId/timeline?api-version=7.0" -Headers $h

  $icon = if ($b.result -eq 'succeeded') { 'OK' } else { 'FALHOU' }
  Write-Host "[$icon] $($b.definition.name) (build $buildId): $($b.result)" -ForegroundColor $(if ($b.result -eq 'succeeded') { 'Green' } else { 'Red' })

  $tl.records | Where-Object { $_.type -eq 'Stage' } |
    Sort-Object order |
    ForEach-Object {
      $icon2 = if ($_.result -eq 'succeeded') { 'v' } elseif ($_.result -eq 'failed') { 'x' } else { '-' }
      Write-Host "    [$icon2] $($_.name): $($_.result)"
    }

  # Se falhou, mostrar erro
  if ($b.result -eq 'failed') {
    if ($b.validationResults) {
      $b.validationResults | Select-Object -First 1 | ForEach-Object { Write-Host "    ERRO: $($_.message)" -ForegroundColor Yellow }
    } else {
      $failedTask = $tl.records | Where-Object { $_.result -eq 'failed' -and $_.type -eq 'Task' } | Select-Object -First 1
      if ($failedTask) {
        Write-Host "    Task falhou: $($failedTask.name)" -ForegroundColor Yellow
        if ($failedTask.issues) {
          $failedTask.issues | Select-Object -First 3 | ForEach-Object { Write-Host "    $($_.message)" -ForegroundColor Yellow }
        }
      }
    }
  }
  Write-Host ""
}
