<?php

namespace Tests\Unit;

use App\Support\PhoneNumber;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class PhoneNumberTest extends TestCase
{
    #[DataProvider('validNumbers')]
    public function test_it_normalizes_supported_local_phone_formats(string $input, string $expected): void
    {
        $this->assertSame($expected, PhoneNumber::normalize($input, '255'));
    }

    public static function validNumbers(): array
    {
        return [
            'ten digits starting 07' => ['0712345678', '255712345678'],
            'ten digits starting 06' => ['0612345678', '255612345678'],
            'nine digits starting 7' => ['712345678', '255712345678'],
            'nine digits starting 6' => ['612345678', '255612345678'],
            'already canonical' => ['255712345678', '255712345678'],
            'formatted canonical' => ['+255 712 345 678', '255712345678'],
        ];
    }

    #[DataProvider('invalidNumbers')]
    public function test_it_rejects_invalid_phone_formats(string $input): void
    {
        $this->assertNull(PhoneNumber::normalize($input, '255'));
    }

    public static function invalidNumbers(): array
    {
        return [
            'ten digits without zero' => ['7123456789'],
            'nine digits starting 5' => ['512345678'],
            'ten digits starting 05' => ['0512345678'],
            'too short' => ['71234567'],
            'too long' => ['07123456789'],
        ];
    }

    public function test_it_uses_a_different_configured_country_code_without_changing_the_local_number(): void
    {
        $this->assertSame('254712345678', PhoneNumber::normalize('0712345678', '254'));
    }
}
