$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'

$vgs = Invoke-RestMethod -Uri "$base/distributedtask/variablegroups?api-version=7.0" -Headers $h

# =====================================================================
# 1. Remover OCIR_REPOSITORY_NAME do VG oci-terraform (ID 34)
# =====================================================================
Write-Host "=== 1. Removendo OCIR_REPOSITORY_NAME do oci-terraform ===" -ForegroundColor Cyan

$vg = $vgs.value | Where-Object { $_.name -eq 'oci-terraform' }
$vgId = $vg.id

# Reconstruir o objeto de variaveis sem OCIR_REPOSITORY_NAME
$newVars = @{}
$vg.variables.PSObject.Properties | Where-Object { $_.Name -ne 'OCIR_REPOSITORY_NAME' } | ForEach-Object {
  $newVars[$_.Name] = $_.Value
}

$updateBody = @{
  id          = $vgId
  name        = $vg.name
  type        = $vg.type
  variables   = $newVars
  description = $vg.description
} | ConvertTo-Json -Depth 10

try {
  $result = Invoke-RestMethod -Uri "$base/distributedtask/variablegroups/$vgId`?api-version=7.0" -Method PUT -Headers $h -Body $updateBody -ErrorAction Stop
  $stillHas = $result.variables.PSObject.Properties | Where-Object { $_.Name -eq 'OCIR_REPOSITORY_NAME' }
  if ($stillHas) {
    Write-Host "  AVISO: variavel ainda presente apos update" -ForegroundColor Yellow
  } else {
    Write-Host "  OK - OCIR_REPOSITORY_NAME removido" -ForegroundColor Green
  }
} catch {
  Write-Host "  ERRO: $_" -ForegroundColor Red
}

# =====================================================================
# 2. Criar VG ms-parameters-dev (ID 34 nao existe, criar novo)
# =====================================================================
Write-Host "`n=== 2. Criando VG ms-parameters-dev ===" -ForegroundColor Cyan

$existingMsParams = $vgs.value | Where-Object { $_.name -eq 'ms-parameters-dev' }
if ($existingMsParams) {
  Write-Host "  VG ms-parameters-dev ja existe (ID: $($existingMsParams.id))" -ForegroundColor Yellow
} else {
  # Pegar ms-notify-dev como modelo de estrutura
  $modelo = $vgs.value | Where-Object { $_.name -eq 'ms-notify-dev' }

  $newVgBody = @{
    name      = 'ms-parameters-dev'
    type      = 'Vsts'
    variables = @{
      CLUSTER_NAME           = @{ value = 'NexusCluster'; isSecret = $false }
      LOG_GROUP_NAME         = @{ value = '/ecs/logs'; isSecret = $false }
      TASK_CONTAINER_NAME    = @{ value = 'ms-parameters'; isSecret = $false }
      TASK_FAMILY            = @{ value = 'ms-parameters'; isSecret = $false }
      TASK_EXECUTION_ROLE_ARN = @{ value = ''; isSecret = $true }
      TASK_ROLE_ARN          = @{ value = ''; isSecret = $true }
      LAST_SWAGGER_HASH      = @{ value = ''; isSecret = $false }
    }
  } | ConvertTo-Json -Depth 5

  try {
    $created = Invoke-RestMethod -Uri "$base/distributedtask/variablegroups?api-version=7.0" -Method POST -Headers $h -Body $newVgBody -ErrorAction Stop
    Write-Host "  OK - VG criado: $($created.name) (ID: $($created.id))" -ForegroundColor Green
  } catch {
    Write-Host "  ERRO: $_" -ForegroundColor Red
  }
}
