<?php

declare(strict_types=1);

namespace App\Infrastructure\Enrichment;

/**
 * MaxMind-shape MMDB records (also DB-IP — same schema by deliberate
 * compatibility). Country DB nests `country.iso_code`; ASN DB has
 * top-level `autonomous_system_number` and `autonomous_system_organization`.
 */
final class MaxMindRecordAdapter implements RecordAdapter
{
    public function extractCountryCode(array $record): ?string
    {
        $country = $record['country'] ?? null;
        if (!is_array($country)) {
            return null;
        }
        $iso = $country['iso_code'] ?? null;

        return is_string($iso) && $iso !== '' ? strtoupper($iso) : null;
    }

    public function extractAsn(array $record): ?int
    {
        $asn = $record['autonomous_system_number'] ?? null;
        if (is_int($asn)) {
            return $asn;
        }
        if (is_string($asn) && ctype_digit($asn)) {
            return (int) $asn;
        }

        return null;
    }

    public function extractAsOrg(array $record): ?string
    {
        $org = $record['autonomous_system_organization'] ?? null;

        return is_string($org) && $org !== '' ? $org : null;
    }
}
