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

# Pegar o ID do projeto
$proj = Invoke-RestMethod -Uri "$baseOrg/projects/Invista%20FIDC%20-%20Nexus?api-version=7.0" -Headers $h
Write-Host "Projeto: $($proj.name) | ID: $($proj.id)"

$projectRef = @(
  @{
    projectReference = @{ id = $proj.id; name = $proj.name }
    name = 'oci-terraform'
  }
)

$vgs = Invoke-RestMethod -Uri "$base/distributedtask/variablegroups?api-version=7.0" -Headers $h
$vg = $vgs.value | Where-Object { $_.name -eq 'oci-terraform' }
$vgId = $vg.id

# =====================================================================
# 1. Remover OCIR_REPOSITORY_NAME do VG oci-terraform
# =====================================================================
Write-Host "`n=== 1. Removendo OCIR_REPOSITORY_NAME do oci-terraform ===" -ForegroundColor Cyan

$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
  projectReferences = $projectRef
} | 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" -ForegroundColor Yellow
  } else {
    Write-Host "  OK - OCIR_REPOSITORY_NAME removido" -ForegroundColor Green
    Write-Host "  Variaveis restantes: $($result.variables.PSObject.Properties.Name -join ', ')"
  }
} catch {
  Write-Host "  ERRO: $_" -ForegroundColor Red
}

# =====================================================================
# 2. Criar VG ms-parameters-dev
# =====================================================================
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 "  Ja existe (ID: $($existingMsParams.id))" -ForegroundColor Yellow
} else {
  $msParamsRef = @(
    @{
      projectReference = @{ id = $proj.id; name = $proj.name }
      name = 'ms-parameters-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 }
    }
    projectReferences = $msParamsRef
  } | 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 - criado: $($created.name) (ID: $($created.id))" -ForegroundColor Green
  } catch {
    Write-Host "  ERRO: $_" -ForegroundColor Red
  }
}
