<?php

declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| HYRO ONE Complete Country List
|--------------------------------------------------------------------------
| Loads ISO country codes and country names from the Linux server.
| Selects the first available timezone for each country.
|--------------------------------------------------------------------------
*/

$isoFile = '/usr/share/zoneinfo/iso3166.tab';

$countries = [];

if (is_readable($isoFile)) {
    $lines = file(
        $isoFile,
        FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
    );

    foreach ($lines ?: [] as $line) {
        $line = trim($line);

        if ($line === '' || str_starts_with($line, '#')) {
            continue;
        }

        $parts = preg_split('/\s+/', $line, 2);

        if (!is_array($parts) || count($parts) !== 2) {
            continue;
        }

        $countryCode = strtoupper(trim($parts[0]));
        $countryName = trim($parts[1]);

        try {
            $timezoneList = DateTimeZone::listIdentifiers(
                DateTimeZone::PER_COUNTRY,
                $countryCode
            );
        } catch (Throwable $exception) {
            $timezoneList = [];
        }

        $countries[$countryCode] = [
            'name' => $countryName,
            'phone_code' => '',
            'timezone' => $timezoneList[0] ?? 'UTC',
        ];
    }
}

/*
|--------------------------------------------------------------------------
| Safe fallback
|--------------------------------------------------------------------------
*/

if ($countries === []) {
    $countries = [
        'AE' => [
            'name' => 'United Arab Emirates',
            'phone_code' => '+971',
            'timezone' => 'Asia/Dubai',
        ],

        'IN' => [
            'name' => 'India',
            'phone_code' => '+91',
            'timezone' => 'Asia/Kolkata',
        ],

        'SA' => [
            'name' => 'Saudi Arabia',
            'phone_code' => '+966',
            'timezone' => 'Asia/Riyadh',
        ],

        'QA' => [
            'name' => 'Qatar',
            'phone_code' => '+974',
            'timezone' => 'Asia/Qatar',
        ],

        'OM' => [
            'name' => 'Oman',
            'phone_code' => '+968',
            'timezone' => 'Asia/Muscat',
        ],

        'KW' => [
            'name' => 'Kuwait',
            'phone_code' => '+965',
            'timezone' => 'Asia/Kuwait',
        ],

        'BH' => [
            'name' => 'Bahrain',
            'phone_code' => '+973',
            'timezone' => 'Asia/Bahrain',
        ],

        'US' => [
            'name' => 'United States',
            'phone_code' => '+1',
            'timezone' => 'America/New_York',
        ],

        'GB' => [
            'name' => 'United Kingdom',
            'phone_code' => '+44',
            'timezone' => 'Europe/London',
        ],

        'DE' => [
            'name' => 'Germany',
            'phone_code' => '+49',
            'timezone' => 'Europe/Berlin',
        ],
    ];
}

uasort(
    $countries,
    static function (array $first, array $second): int {
        return strcasecmp(
            (string) $first['name'],
            (string) $second['name']
        );
    }
);

return $countries;