<?php

declare(strict_types=1);

namespace App\Tests\Unit\Enrichment;

use App\Infrastructure\Enrichment\MaxMindRecordAdapter;
use PHPUnit\Framework\TestCase;

final class MaxMindRecordAdapterTest extends TestCase
{
    public function testCountryNested(): void
    {
        $adapter = new MaxMindRecordAdapter();
        $record = ['country' => ['iso_code' => 'gb', 'names' => ['en' => 'United Kingdom']]];
        self::assertSame('GB', $adapter->extractCountryCode($record));
    }

    public function testCountryMissingReturnsNull(): void
    {
        $adapter = new MaxMindRecordAdapter();
        self::assertNull($adapter->extractCountryCode([]));
        self::assertNull($adapter->extractCountryCode(['country' => null]));
        self::assertNull($adapter->extractCountryCode(['country' => ['names' => ['en' => 'X']]]));
    }

    public function testAsn(): void
    {
        $adapter = new MaxMindRecordAdapter();
        self::assertSame(13335, $adapter->extractAsn(['autonomous_system_number' => 13335]));
        self::assertSame(13335, $adapter->extractAsn(['autonomous_system_number' => '13335']));
        self::assertNull($adapter->extractAsn([]));
        self::assertNull($adapter->extractAsn(['autonomous_system_number' => 'AS13335']));
    }

    public function testAsOrg(): void
    {
        $adapter = new MaxMindRecordAdapter();
        self::assertSame(
            'Cloudflare, Inc.',
            $adapter->extractAsOrg(['autonomous_system_organization' => 'Cloudflare, Inc.']),
        );
        self::assertNull($adapter->extractAsOrg([]));
        self::assertNull($adapter->extractAsOrg(['autonomous_system_organization' => '']));
    }
}
