<?php

$defaultCountries = [
    ['name' => 'India', 'phone_code' => '+91'],
    ['name' => 'United States', 'phone_code' => '+1'],
];

$defaultStates = [
    'India' => [
        'Andhra Pradesh',
        'Tamil Nadu',
        'Karnataka',
        'Kerala',
        'Maharashtra',
        'Delhi',
    ],
    'United States' => [
        'California',
        'Texas',
        'New York',
        'Florida',
        'Illinois',
    ],
];

$defaultIndustries = [
    'Agency or Sales House',
    'Agriculture',
    'Art and Design',
    'Automotive',
    'Construction',
    'Consulting',
    'Education',
    'Finance',
    'Healthcare',
    'Manufacturing',
    'Retail',
    'Software / IT',
];

$defaultCurrencies = ['INR - Indian Rupee', 'USD - US Dollar', 'EUR - Euro'];
$defaultLanguages = ['English', 'Hindi', 'Tamil'];
$defaultTimezones = ['(GMT 5:30) India Standard Time (Asia/Kolkata)'];

$countriesRows = fetch_all('SELECT name, phone_code FROM countries ORDER BY sort_order, name');
$countries = [];
foreach ($countriesRows as $row) {
    $countries[] = [
        'name' => $row['name'],
        'phone_code' => $row['phone_code'],
    ];
}
if (!$countries) {
    $countries = $defaultCountries;
}

$stateRows = fetch_all(
    'SELECT c.name AS country_name, s.name AS state_name
     FROM states s
     INNER JOIN countries c ON c.id = s.country_id
     ORDER BY c.sort_order, s.sort_order, s.name'
);
$states = [];
foreach ($stateRows as $row) {
    $states[$row['country_name']][] = $row['state_name'];
}
if (!$states) {
    $states = $defaultStates;
}

$industriesDb = fetch_all('SELECT name FROM industries ORDER BY sort_order, name');
$industries = array_map(
    static fn(array $row): string => $row['name'],
    $industriesDb
);
if (!$industries) {
    $industries = $defaultIndustries;
}

$currenciesDb = fetch_all('SELECT name FROM currencies ORDER BY sort_order, name');
$currencies = array_map(
    static fn(array $row): string => $row['name'],
    $currenciesDb
);
if (!$currencies) {
    $currencies = $defaultCurrencies;
}

$languagesDb = fetch_all('SELECT name FROM languages ORDER BY sort_order, name');
$languages = array_map(
    static fn(array $row): string => $row['name'],
    $languagesDb
);
if (!$languages) {
    $languages = $defaultLanguages;
}

$timezonesDb = fetch_all('SELECT name FROM timezones ORDER BY sort_order, name');
$timezones = array_map(
    static fn(array $row): string => $row['name'],
    $timezonesDb
);
if (!$timezones) {
    $timezones = $defaultTimezones;
}
