<?php

declare(strict_types=1);

use App\Models\Country;
use App\Models\User;
use App\Services\Countries\CountryCatalogService;
use App\Services\Countries\CountryRemoteProviderInterface;
use App\Services\Countries\CountrySyncService;
use Illuminate\Support\Facades\DB;

it('syncs countries updates cache and backfills users from legacy country codes', function (): void {
    config()->set('skinbase-countries.deactivate_missing', true);

    Country::query()->where('iso2', 'SI')->update([
        'iso' => 'SI',
        'iso3' => 'SVN',
        'name' => 'Old Slovenia',
        'name_common' => 'Old Slovenia',
        'active' => true,
    ]);

    $user = User::factory()->create();

    DB::table('user_profiles')->insert([
        'user_id' => $user->id,
        'country_code' => 'SI',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $catalog = app(CountryCatalogService::class);
    expect(collect($catalog->profileSelectOptions())->firstWhere('iso2', 'SI')['name'])->toBe('Old Slovenia');

    app()->instance(CountryRemoteProviderInterface::class, new class implements CountryRemoteProviderInterface {
        public function fetchAll(): array
        {
            return [
                [
                    'iso2' => 'SI',
                    'iso3' => 'SVN',
                    'numeric_code' => '705',
                    'name_common' => 'Slovenia',
                    'name_official' => 'Republic of Slovenia',
                    'region' => 'Europe',
                    'subregion' => 'Central Europe',
                    'flag_svg_url' => 'https://flags.test/si.svg',
                    'flag_png_url' => 'https://flags.test/si.png',
                    'flag_emoji' => '🇸🇮',
                ],
                [
                    'iso2' => '',
                    'name_common' => 'Invalid',
                ],
                [
                    'iso2' => 'SI',
                    'name_common' => 'Duplicate Slovenia',
                ],
                [
                    'iso2' => 'ZZ',
                    'iso3' => 'ZZZ',
                    'numeric_code' => '999',
                    'name_common' => 'Zedland',
                    'name_official' => 'Republic of Zedland',
                    'region' => 'Europe',
                    'subregion' => 'Nowhere',
                    'flag_svg_url' => 'https://flags.test/zz.svg',
                    'flag_png_url' => 'https://flags.test/zz.png',
                    'flag_emoji' => '🏳️',
                ],
            ];
        }

        public function normalizePayload(array $payload): array
        {
            return $payload;
        }
    });

    $summary = app(CountrySyncService::class)->sync(allowFallback: false, deactivateMissing: true);

    expect($summary['updated'])->toBe(1)
        ->and($summary['inserted'])->toBe(1)
        ->and($summary['invalid'])->toBe(1)
        ->and($summary['skipped'])->toBe(1)
        ->and($summary['backfilled_users'])->toBe(1);

    expect(collect($catalog->profileSelectOptions())->firstWhere('iso2', 'SI')['name'])->toBe('Slovenia');
    expect($user->fresh()->country_id)->toBe(Country::query()->where('iso2', 'SI')->value('id'));
});
