$tokenRede5 = '5dcfd89a9d4ec330dede0d4074a518f26818e'
$emailRede5 = 'yamamoto@rede5.com.br'

$tokenInvent = '7ae5565ab2dcdfdfdd66efb1105e27d18d186'
$emailInvent = 'tiago.ribeiro@inventcloud.com.br'

function Check-Cloudflare {
    param($email, $token)
    
    $headers = @{
        "X-Auth-Email" = $email
        "X-Auth-Key" = $token
        "Content-Type" = "application/json"
    }

    try {
        $zonesResponse = Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones?per_page=50" -Headers $headers -Method Get
        $found = $false
        foreach ($zone in $zonesResponse.result) {
            if ($zone.name.Contains('q1food.com')) {
                $found = $true
                Write-Host "Found zone $($zone.name) with ID $($zone.id) in account $email"
                $dnsUrl = "https://api.cloudflare.com/client/v4/zones/$($zone.id)/dns_records?name=dev.q1food.com"
                $dnsResponse = Invoke-RestMethod -Uri $dnsUrl -Headers $headers -Method Get
                
                if ($dnsResponse.result) {
                    Write-Host "DNS Records for dev.q1food.com:"
                    $dnsResponse.result | Select-Object name, type, content, proxied | Format-Table -AutoSize
                } else {
                    Write-Host "No DNS records found for dev.q1food.com in this zone."
                }
                
                # Check root domain as well just in case
                $dnsUrlRoot = "https://api.cloudflare.com/client/v4/zones/$($zone.id)/dns_records?name=q1food.com"
                $dnsResponseRoot = Invoke-RestMethod -Uri $dnsUrlRoot -Headers $headers -Method Get
                if ($dnsResponseRoot.result) {
                    Write-Host "DNS Records for q1food.com:"
                    $dnsResponseRoot.result | Select-Object name, type, content, proxied | Format-Table -AutoSize
                }
            }
        }
        if (-not $found) {
            Write-Host "Zone q1food.com not found in account $email."
        }
    } catch {
        Write-Host "Error checking account $email : $($_.Exception.Message)"
    }
}

Write-Host "Checking Rede5 Account..."
Check-Cloudflare -email $emailRede5 -token $tokenRede5

Write-Host ""
Write-Host "Checking Inventcloud Account..."
Check-Cloudflare -email $emailInvent -token $tokenInvent
