<?php

namespace Tests\Unit;

use App\LocalisationService;
use Illuminate\Database\Eloquent\Model;
use Mtc\MercuryDataModels\Country;
use Tests\DatabaseTestCase;

class LocalisationServiceTest extends DatabaseTestCase
{
    #[\Override]
    protected function setUp(): void
    {
        $this->needsMasterDatabase = true;
        $this->needsTenantDatabase = false;
        parent::setUp();
        Country::unguard();
    }

    #[\Override]
    protected function tearDown(): void
    {
        // unguard() is global to all models; reguard so the relaxed state does not
        // leak into other tests sharing the same (paratest) process.
        Model::reguard();
        parent::tearDown();
    }

    public function testPhonePrefixReturnsDialCodeWithPlus(): void
    {
        Country::query()->create([
            'name' => 'United Kingdom',
            'code' => 'GB',
            'dial_code' => '44',
            'has_postcodes' => true,
        ]);

        $result = (new LocalisationService())->phonePrefix('GB');

        $this->assertEquals('+44', $result);
    }

    public function testPhonePrefixReturnsEmptyStringForNullCountryCode(): void
    {
        $result = (new LocalisationService())->phonePrefix(null);

        $this->assertEquals('', $result);
    }

    public function testPhonePrefixReturnsEmptyStringForEmptyCountryCode(): void
    {
        $result = (new LocalisationService())->phonePrefix('');

        $this->assertEquals('', $result);
    }

    public function testPhonePrefixReturnsEmptyPrefixForNonExistentCountry(): void
    {
        $result = (new LocalisationService())->phonePrefix('XX');

        $this->assertEquals('+', $result);
    }

    public function testPhonePrefixReturnsCorrectDialCodeForDifferentCountries(): void
    {
        Country::query()->create([
            'name' => 'United States',
            'code' => 'US',
            'dial_code' => '1',
            'has_postcodes' => true,
        ]);

        Country::query()->create([
            'name' => 'Germany',
            'code' => 'DE',
            'dial_code' => '49',
            'has_postcodes' => true,
        ]);

        $this->assertEquals('+1', (new LocalisationService())->phonePrefix('US'));
        $this->assertEquals('+49', (new LocalisationService())->phonePrefix('DE'));
    }
}
