<?php

namespace Database\Seeders;

use App\Models\Reference\Currency;
use App\Models\Reference\Locale;
use Illuminate\Database\Seeder;

/**
 * Platform-wide currency and locale reference data — global, like
 * {@see GeographySeeder}, never tenant-scoped. Backs the Master Control
 * "Locale" tab's Selects (App\Filament\Central\Resources\TenantResource\Pages\EditTenant),
 * so a landlord picks a currency/locale from a real list instead of typing a
 * code and a symbol by hand.
 *
 * IANA timezones are deliberately NOT seeded here: PHP already ships the
 * full, currently-correct tzdata (see App\Support\Locale\TimezoneOptions,
 * built from DateTimeZone::listIdentifiers()) — hand-maintaining a second,
 * static copy of it would only go stale.
 *
 * Idempotent — {@see Currency::findByCode()} et al. all key on `code`, and
 * this uses firstOrCreate throughout, so re-running produces no duplicates.
 */
class ReferenceDataSeeder extends Seeder
{
    public function run(): void
    {
        $this->seedCurrencies();
        $this->seedLocales();
    }

    /**
     * Major world currencies. `decimal_digits` defaults to 2; the handful of
     * currencies with no minor unit in everyday use (JPY, KRW, VND, IDR) are
     * called out explicitly rather than left to round incorrectly.
     */
    private function seedCurrencies(): void
    {
        $zeroDecimal = ['JPY', 'KRW', 'VND', 'IDR'];

        $rows = [
            ['AED', 'UAE Dirham', 'د.إ'],
            ['AUD', 'Australian Dollar', 'A$'],
            ['BDT', 'Bangladeshi Taka', '৳'],
            ['BRL', 'Brazilian Real', 'R$'],
            ['CAD', 'Canadian Dollar', 'C$'],
            ['CHF', 'Swiss Franc', 'CHF'],
            ['CNY', 'Chinese Yuan', '¥'],
            ['DKK', 'Danish Krone', 'kr'],
            ['EUR', 'Euro', '€'],
            ['GBP', 'British Pound', '£'],
            ['HKD', 'Hong Kong Dollar', 'HK$'],
            ['IDR', 'Indonesian Rupiah', 'Rp'],
            ['INR', 'Indian Rupee', '₹'],
            ['JPY', 'Japanese Yen', '¥'],
            ['KRW', 'South Korean Won', '₩'],
            ['LKR', 'Sri Lankan Rupee', 'Rs.'],
            ['MYR', 'Malaysian Ringgit', 'RM'],
            ['MXN', 'Mexican Peso', 'MX$'],
            ['NOK', 'Norwegian Krone', 'kr'],
            ['NZD', 'New Zealand Dollar', 'NZ$'],
            ['PHP', 'Philippine Peso', '₱'],
            ['PKR', 'Pakistani Rupee', '₨'],
            ['QAR', 'Qatari Riyal', 'ر.ق'],
            ['SAR', 'Saudi Riyal', 'ر.س'],
            ['SEK', 'Swedish Krona', 'kr'],
            ['SGD', 'Singapore Dollar', 'S$'],
            ['THB', 'Thai Baht', '฿'],
            ['TRY', 'Turkish Lira', '₺'],
            ['TWD', 'New Taiwan Dollar', 'NT$'],
            ['USD', 'US Dollar', '$'],
            ['VND', 'Vietnamese Dong', '₫'],
            ['ZAR', 'South African Rand', 'R'],
        ];

        foreach ($rows as $sort => [$code, $name, $symbol]) {
            Currency::query()->firstOrCreate(
                ['code' => $code],
                [
                    'name' => $name,
                    'symbol' => $symbol,
                    'decimal_digits' => in_array($code, $zeroDecimal, true) ? 0 : 2,
                    'sort_order' => ($sort + 1) * 10,
                ],
            );
        }
    }

    /**
     * BCP-47 tags for the languages/regions the tenant base actually trades
     * in — Sri Lanka's three official/national languages first, then the
     * locales matching {@see seedCurrencies()}'s countries. Not the full ~700
     * tag ICU ships (that list is not curated for business use the way this
     * one is), but real, unambiguous tags — never free text.
     */
    private function seedLocales(): void
    {
        $rows = [
            ['en-LK', 'English (Sri Lanka)'],
            ['si-LK', 'Sinhala (Sri Lanka)'],
            ['ta-LK', 'Tamil (Sri Lanka)'],
            ['en-US', 'English (United States)'],
            ['en-GB', 'English (United Kingdom)'],
            ['en-AU', 'English (Australia)'],
            ['en-CA', 'English (Canada)'],
            ['en-NZ', 'English (New Zealand)'],
            ['en-IN', 'English (India)'],
            ['en-ZA', 'English (South Africa)'],
            ['en-SG', 'English (Singapore)'],
            ['en-HK', 'English (Hong Kong)'],
            ['en-AE', 'English (United Arab Emirates)'],
            ['ar-AE', 'Arabic (United Arab Emirates)'],
            ['ar-SA', 'Arabic (Saudi Arabia)'],
            ['ar-QA', 'Arabic (Qatar)'],
            ['hi-IN', 'Hindi (India)'],
            ['bn-BD', 'Bengali (Bangladesh)'],
            ['ur-PK', 'Urdu (Pakistan)'],
            ['zh-CN', 'Chinese, Simplified (China)'],
            ['zh-TW', 'Chinese, Traditional (Taiwan)'],
            ['zh-HK', 'Chinese, Traditional (Hong Kong)'],
            ['ja-JP', 'Japanese (Japan)'],
            ['ko-KR', 'Korean (South Korea)'],
            ['id-ID', 'Indonesian (Indonesia)'],
            ['ms-MY', 'Malay (Malaysia)'],
            ['th-TH', 'Thai (Thailand)'],
            ['vi-VN', 'Vietnamese (Vietnam)'],
            ['tl-PH', 'Filipino (Philippines)'],
            ['tr-TR', 'Turkish (Turkey)'],
            ['pt-BR', 'Portuguese (Brazil)'],
            ['es-MX', 'Spanish (Mexico)'],
            ['fr-FR', 'French (France)'],
            ['fr-CA', 'French (Canada)'],
            ['de-DE', 'German (Germany)'],
            ['de-CH', 'German (Switzerland)'],
            ['nl-NL', 'Dutch (Netherlands)'],
            ['da-DK', 'Danish (Denmark)'],
            ['nb-NO', 'Norwegian (Norway)'],
            ['sv-SE', 'Swedish (Sweden)'],
        ];

        foreach ($rows as $sort => [$code, $name]) {
            Locale::query()->firstOrCreate(
                ['code' => $code],
                ['name' => $name, 'sort_order' => ($sort + 1) * 10],
            );
        }
    }
}
