<?php
declare(strict_types=1);

function thailand_province_options(): array
{
    return array(
        'Bangkok',
        'Amnat Charoen',
        'Ang Thong',
        'Bueng Kan',
        'Buriram',
        'Chachoengsao',
        'Chai Nat',
        'Chaiyaphum',
        'Chanthaburi',
        'Chiang Mai',
        'Chiang Rai',
        'Chonburi',
        'Chumphon',
        'Kalasin',
        'Kamphaeng Phet',
        'Kanchanaburi',
        'Khon Kaen',
        'Krabi',
        'Lampang',
        'Lamphun',
        'Loei',
        'Lopburi',
        'Mae Hong Son',
        'Maha Sarakham',
        'Mukdahan',
        'Nakhon Nayok',
        'Nakhon Pathom',
        'Nakhon Phanom',
        'Nakhon Ratchasima',
        'Nakhon Sawan',
        'Nakhon Si Thammarat',
        'Nan',
        'Narathiwat',
        'Nong Bua Lamphu',
        'Nong Khai',
        'Nonthaburi',
        'Pathum Thani',
        'Pattani',
        'Phang Nga',
        'Phatthalung',
        'Phayao',
        'Phetchabun',
        'Phetchaburi',
        'Phichit',
        'Phitsanulok',
        'Phra Nakhon Si Ayutthaya',
        'Phrae',
        'Phuket',
        'Prachinburi',
        'Prachuap Khiri Khan',
        'Ranong',
        'Ratchaburi',
        'Rayong',
        'Roi Et',
        'Sa Kaeo',
        'Sakon Nakhon',
        'Samut Prakan',
        'Samut Sakhon',
        'Samut Songkhram',
        'Saraburi',
        'Satun',
        'Sing Buri',
        'Sisaket',
        'Songkhla',
        'Sukhothai',
        'Suphan Buri',
        'Surat Thani',
        'Surin',
        'Tak',
        'Trang',
        'Trat',
        'Ubon Ratchathani',
        'Udon Thani',
        'Uthai Thani',
        'Uttaradit',
        'Yala',
        'Yasothon',
    );
}

function vehicle_province_options(string $selectedProvince = ''): array
{
    $options = thailand_province_options();
    $selectedProvince = trim($selectedProvince);

    if ($selectedProvince !== '' && !in_array($selectedProvince, $options, true)) {
        $options[] = $selectedProvince;
    }

    return $options;
}

function vehicle_type_options(): array
{
    return array('Sedan', 'SUV', 'Pickup', 'Van', 'Supercar', 'EV', 'Motorcycle');
}

function default_vehicle_brands(): array
{
    return array(
        'Toyota',
        'Honda',
        'Nissan',
        'Mazda',
        'Mitsubishi',
        'Isuzu',
        'Ford',
        'Chevrolet',
        'Mercedes-Benz',
        'BMW',
        'Audi',
        'Porsche',
        'Lexus',
        'Tesla',
        'BYD',
        'MG',
        'Volvo',
        'Volkswagen',
        'Mini',
        'Suzuki',
    );
}

function vehicle_brand_options(mysqli $conn, array $extraBrands = array()): array
{
    $brands = default_vehicle_brands();
    $result = $conn->query("
        SELECT DISTINCT brand
        FROM cars
        WHERE brand IS NOT NULL AND brand <> ''
        ORDER BY brand ASC
    ");

    if ($result instanceof mysqli_result) {
        while ($row = $result->fetch_assoc()) {
            $brand = trim((string) ($row['brand'] ?? ''));

            if ($brand !== '' && !in_array($brand, $brands, true)) {
                $brands[] = $brand;
            }
        }
    }

    foreach ($extraBrands as $brand) {
        $brand = trim((string) $brand);

        if ($brand !== '' && !in_array($brand, $brands, true)) {
            $brands[] = $brand;
        }
    }

    return $brands;
}
