<?php

namespace App\Support\Integration;

use App\Contracts\WalletApiDnsResolver;

final class SystemWalletApiDnsResolver implements WalletApiDnsResolver
{
    public function resolveAll(string $hostname): array
    {
        $records = dns_get_record($hostname, DNS_A | DNS_AAAA);
        if (! is_array($records)) {
            return [];
        }

        $addresses = [];
        foreach ($records as $record) {
            $address = match ($record['type'] ?? null) {
                'A' => $record['ip'] ?? null,
                'AAAA' => $record['ipv6'] ?? null,
                default => null,
            };

            if (is_string($address) && $address !== '') {
                $addresses[] = $address;
            }
        }

        return array_values(array_unique($addresses));
    }
}
