<?php

declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| HYRO ONE Complete Country List
|--------------------------------------------------------------------------
| Loads ISO country codes and names from the Linux system.
| The first available timezone for each country is selected automatically.
|--------------------------------------------------------------------------
*/

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

$countries = [];

$timezonesByCountry = [];

foreach (
    DateTimeZone::listIdentifiers(
        DateTimeZone::PER_COUNTRY
    ) as $timezone
) {
    /*
     * PER_COUNTRY cannot be used without a country code on some PHP
     * versions, so the complete mapping is created below when possible.
     */
}

/*
|--------------------------------------------------------------------------
| Build timezone map
|--------------------------------------------------------------------------
*/

foreach (DateTimeZone::listIdentifiers() as $timezone) {
    $parts = explode('/', $timezone);

    if (count($parts) < 2) {
        continue;
    }

    /*
     * Country-specific timezone resolution is completed after the ISO
     * country code is loaded. UTC remains the safe fallback.
     */
}

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]);

        $timezoneList = DateTimeZone::listIdentifiers(
            DateTimeZone::PER_COUNTRY,
            $countryCode
        );

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

/*
|--------------------------------------------------------------------------
| Fallback if the server ISO file is unavailable
|--------------------------------------------------------------------------
*/

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',
        ],
    ];
}

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

return $countries;