<?php

declare(strict_types=1);

namespace App\Infrastructure\Enrichment;

/**
 * IPinfo Lite MMDB schema: flat records.
 *   - country: `country_code` (uppercase ISO-3166)
 *   - asn:     `asn` as string like "AS13335"; `as_name` as the org name
 */
final class IpinfoRecordAdapter implements RecordAdapter
{
    public function extractCountryCode(array $record): ?string
    {
        $code = $record['country_code'] ?? null;

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

    public function extractAsn(array $record): ?int
    {
        $raw = $record['asn'] ?? null;
        if (!is_string($raw) || $raw === '') {
            return null;
        }
        $digits = strncasecmp($raw, 'AS', 2) === 0 ? substr($raw, 2) : $raw;

        return ctype_digit($digits) ? (int) $digits : null;
    }

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

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