<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $countries = [
        ['iso_code' => 'EGY', 'name_ar' => 'مصر',                    'name_en' => 'Egypt',                 'nationality_ar' => 'مصري',          'nationality_en' => 'Egyptian',          'phone_code' => '+20'],
        ['iso_code' => 'SAU', 'name_ar' => 'المملكة العربية السعودية', 'name_en' => 'Saudi Arabia',          'nationality_ar' => 'سعودي',         'nationality_en' => 'Saudi',             'phone_code' => '+966'],
        ['iso_code' => 'ARE', 'name_ar' => 'الإمارات العربية المتحدة', 'name_en' => 'United Arab Emirates',  'nationality_ar' => 'إماراتي',       'nationality_en' => 'Emirati',           'phone_code' => '+971'],
        ['iso_code' => 'KWT', 'name_ar' => 'الكويت',                  'name_en' => 'Kuwait',                'nationality_ar' => 'كويتي',         'nationality_en' => 'Kuwaiti',           'phone_code' => '+965'],
        ['iso_code' => 'QAT', 'name_ar' => 'قطر',                     'name_en' => 'Qatar',                 'nationality_ar' => 'قطري',          'nationality_en' => 'Qatari',            'phone_code' => '+974'],
        ['iso_code' => 'BHR', 'name_ar' => 'البحرين',                 'name_en' => 'Bahrain',               'nationality_ar' => 'بحريني',        'nationality_en' => 'Bahraini',          'phone_code' => '+973'],
        ['iso_code' => 'OMN', 'name_ar' => 'عمان',                    'name_en' => 'Oman',                  'nationality_ar' => 'عماني',         'nationality_en' => 'Omani',             'phone_code' => '+968'],
        ['iso_code' => 'JOR', 'name_ar' => 'الأردن',                  'name_en' => 'Jordan',                'nationality_ar' => 'أردني',         'nationality_en' => 'Jordanian',         'phone_code' => '+962'],
        ['iso_code' => 'IRQ', 'name_ar' => 'العراق',                  'name_en' => 'Iraq',                  'nationality_ar' => 'عراقي',         'nationality_en' => 'Iraqi',             'phone_code' => '+964'],
        ['iso_code' => 'SYR', 'name_ar' => 'سوريا',                   'name_en' => 'Syria',                 'nationality_ar' => 'سوري',          'nationality_en' => 'Syrian',            'phone_code' => '+963'],
        ['iso_code' => 'LBN', 'name_ar' => 'لبنان',                   'name_en' => 'Lebanon',               'nationality_ar' => 'لبناني',        'nationality_en' => 'Lebanese',          'phone_code' => '+961'],
        ['iso_code' => 'PSE', 'name_ar' => 'فلسطين',                  'name_en' => 'Palestine',             'nationality_ar' => 'فلسطيني',       'nationality_en' => 'Palestinian',       'phone_code' => '+970'],
        ['iso_code' => 'YEM', 'name_ar' => 'اليمن',                   'name_en' => 'Yemen',                 'nationality_ar' => 'يمني',          'nationality_en' => 'Yemeni',            'phone_code' => '+967'],
        ['iso_code' => 'LBY', 'name_ar' => 'ليبيا',                   'name_en' => 'Libya',                 'nationality_ar' => 'ليبي',          'nationality_en' => 'Libyan',            'phone_code' => '+218'],
        ['iso_code' => 'TUN', 'name_ar' => 'تونس',                    'name_en' => 'Tunisia',               'nationality_ar' => 'تونسي',         'nationality_en' => 'Tunisian',          'phone_code' => '+216'],
        ['iso_code' => 'DZA', 'name_ar' => 'الجزائر',                 'name_en' => 'Algeria',               'nationality_ar' => 'جزائري',        'nationality_en' => 'Algerian',          'phone_code' => '+213'],
        ['iso_code' => 'MAR', 'name_ar' => 'المغرب',                  'name_en' => 'Morocco',               'nationality_ar' => 'مغربي',         'nationality_en' => 'Moroccan',          'phone_code' => '+212'],
        ['iso_code' => 'SDN', 'name_ar' => 'السودان',                 'name_en' => 'Sudan',                 'nationality_ar' => 'سوداني',        'nationality_en' => 'Sudanese',          'phone_code' => '+249'],
        ['iso_code' => 'SOM', 'name_ar' => 'الصومال',                 'name_en' => 'Somalia',               'nationality_ar' => 'صومالي',        'nationality_en' => 'Somali',            'phone_code' => '+252'],
        ['iso_code' => 'MRT', 'name_ar' => 'موريتانيا',               'name_en' => 'Mauritania',            'nationality_ar' => 'موريتاني',      'nationality_en' => 'Mauritanian',       'phone_code' => '+222'],
        ['iso_code' => 'COM', 'name_ar' => 'جزر القمر',               'name_en' => 'Comoros',               'nationality_ar' => 'قمري',          'nationality_en' => 'Comorian',          'phone_code' => '+269'],
        ['iso_code' => 'DJI', 'name_ar' => 'جيبوتي',                  'name_en' => 'Djibouti',              'nationality_ar' => 'جيبوتي',        'nationality_en' => 'Djiboutian',        'phone_code' => '+253'],
        ['iso_code' => 'USA', 'name_ar' => 'الولايات المتحدة',        'name_en' => 'United States',         'nationality_ar' => 'أمريكي',        'nationality_en' => 'American',          'phone_code' => '+1'],
        ['iso_code' => 'GBR', 'name_ar' => 'المملكة المتحدة',         'name_en' => 'United Kingdom',        'nationality_ar' => 'بريطاني',       'nationality_en' => 'British',           'phone_code' => '+44'],
        ['iso_code' => 'FRA', 'name_ar' => 'فرنسا',                   'name_en' => 'France',                'nationality_ar' => 'فرنسي',         'nationality_en' => 'French',            'phone_code' => '+33'],
        ['iso_code' => 'DEU', 'name_ar' => 'ألمانيا',                 'name_en' => 'Germany',               'nationality_ar' => 'ألماني',        'nationality_en' => 'German',            'phone_code' => '+49'],
        ['iso_code' => 'ITA', 'name_ar' => 'إيطاليا',                 'name_en' => 'Italy',                 'nationality_ar' => 'إيطالي',        'nationality_en' => 'Italian',           'phone_code' => '+39'],
        ['iso_code' => 'ESP', 'name_ar' => 'إسبانيا',                 'name_en' => 'Spain',                 'nationality_ar' => 'إسباني',        'nationality_en' => 'Spanish',           'phone_code' => '+34'],
        ['iso_code' => 'TUR', 'name_ar' => 'تركيا',                   'name_en' => 'Turkey',                'nationality_ar' => 'تركي',          'nationality_en' => 'Turkish',           'phone_code' => '+90'],
        ['iso_code' => 'IRN', 'name_ar' => 'إيران',                   'name_en' => 'Iran',                  'nationality_ar' => 'إيراني',        'nationality_en' => 'Iranian',           'phone_code' => '+98'],
        ['iso_code' => 'CHN', 'name_ar' => 'الصين',                   'name_en' => 'China',                 'nationality_ar' => 'صيني',          'nationality_en' => 'Chinese',           'phone_code' => '+86'],
        ['iso_code' => 'JPN', 'name_ar' => 'اليابان',                 'name_en' => 'Japan',                 'nationality_ar' => 'ياباني',        'nationality_en' => 'Japanese',          'phone_code' => '+81'],
        ['iso_code' => 'IND', 'name_ar' => 'الهند',                   'name_en' => 'India',                 'nationality_ar' => 'هندي',          'nationality_en' => 'Indian',            'phone_code' => '+91'],
        ['iso_code' => 'RUS', 'name_ar' => 'روسيا',                   'name_en' => 'Russia',                'nationality_ar' => 'روسي',          'nationality_en' => 'Russian',           'phone_code' => '+7'],
        ['iso_code' => 'BRA', 'name_ar' => 'البرازيل',                'name_en' => 'Brazil',                'nationality_ar' => 'برازيلي',       'nationality_en' => 'Brazilian',         'phone_code' => '+55'],
        ['iso_code' => 'CAN', 'name_ar' => 'كندا',                    'name_en' => 'Canada',                'nationality_ar' => 'كندي',          'nationality_en' => 'Canadian',          'phone_code' => '+1'],
        ['iso_code' => 'AUS', 'name_ar' => 'أستراليا',                'name_en' => 'Australia',             'nationality_ar' => 'أسترالي',       'nationality_en' => 'Australian',        'phone_code' => '+61'],
        ['iso_code' => 'ZAF', 'name_ar' => 'جنوب أفريقيا',            'name_en' => 'South Africa',          'nationality_ar' => 'جنوب أفريقي',   'nationality_en' => 'South African',     'phone_code' => '+27'],
        ['iso_code' => 'NGA', 'name_ar' => 'نيجيريا',                 'name_en' => 'Nigeria',               'nationality_ar' => 'نيجيري',        'nationality_en' => 'Nigerian',          'phone_code' => '+234'],
        ['iso_code' => 'PAK', 'name_ar' => 'باكستان',                 'name_en' => 'Pakistan',              'nationality_ar' => 'باكستاني',      'nationality_en' => 'Pakistani',         'phone_code' => '+92'],
        ['iso_code' => 'KOR', 'name_ar' => 'كوريا الجنوبية',          'name_en' => 'South Korea',           'nationality_ar' => 'كوري جنوبي',    'nationality_en' => 'South Korean',      'phone_code' => '+82'],
        ['iso_code' => 'MEX', 'name_ar' => 'المكسيك',                 'name_en' => 'Mexico',                'nationality_ar' => 'مكسيكي',        'nationality_en' => 'Mexican',           'phone_code' => '+52'],
        ['iso_code' => 'NLD', 'name_ar' => 'هولندا',                  'name_en' => 'Netherlands',           'nationality_ar' => 'هولندي',        'nationality_en' => 'Dutch',             'phone_code' => '+31'],
        ['iso_code' => 'CHE', 'name_ar' => 'سويسرا',                  'name_en' => 'Switzerland',           'nationality_ar' => 'سويسري',        'nationality_en' => 'Swiss',             'phone_code' => '+41'],
        ['iso_code' => 'SWE', 'name_ar' => 'السويد',                  'name_en' => 'Sweden',                'nationality_ar' => 'سويدي',         'nationality_en' => 'Swedish',           'phone_code' => '+46'],
        ['iso_code' => 'GRC', 'name_ar' => 'اليونان',                 'name_en' => 'Greece',                'nationality_ar' => 'يوناني',        'nationality_en' => 'Greek',             'phone_code' => '+30'],
        ['iso_code' => 'ETH', 'name_ar' => 'إثيوبيا',                 'name_en' => 'Ethiopia',              'nationality_ar' => 'إثيوبي',        'nationality_en' => 'Ethiopian',         'phone_code' => '+251'],
        ['iso_code' => 'KEN', 'name_ar' => 'كينيا',                   'name_en' => 'Kenya',                 'nationality_ar' => 'كيني',          'nationality_en' => 'Kenyan',            'phone_code' => '+254'],
        ['iso_code' => 'PHL', 'name_ar' => 'الفلبين',                 'name_en' => 'Philippines',           'nationality_ar' => 'فلبيني',        'nationality_en' => 'Filipino',          'phone_code' => '+63'],
        ['iso_code' => 'IDN', 'name_ar' => 'إندونيسيا',               'name_en' => 'Indonesia',             'nationality_ar' => 'إندونيسي',      'nationality_en' => 'Indonesian',        'phone_code' => '+62'],
    ];

    foreach ($countries as $c) {
        $existing = $db->selectOne("SELECT id FROM countries WHERE iso_code = ?", [$c['iso_code']]);
        if ($existing) {
            continue;
        }
        $db->insert('countries', [
            'iso_code'       => $c['iso_code'],
            'name_ar'        => $c['name_ar'],
            'name_en'        => $c['name_en'],
            'nationality_ar' => $c['nationality_ar'],
            'nationality_en' => $c['nationality_en'],
            'phone_code'     => $c['phone_code'],
            'is_active'      => 1,
        ]);
    }
};