<?php

namespace Database\Seeders;

use App\Models\BdDivision;
use App\Models\BdDistrict;
use App\Models\BdUpazila;
use Illuminate\Database\Seeder;

class BdLocationSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 8 Divisions
        $divisions = [
            ['id' => 1, 'name' => 'Dhaka', 'bn_name' => 'ঢাকা'],
            ['id' => 2, 'name' => 'Chattogram', 'bn_name' => 'চট্টগ্রাম'],
            ['id' => 3, 'name' => 'Rajshahi', 'bn_name' => 'রাজশাহী'],
            ['id' => 4, 'name' => 'Khulna', 'bn_name' => 'খুলনা'],
            ['id' => 5, 'name' => 'Barishal', 'bn_name' => 'বরিশাল'],
            ['id' => 6, 'name' => 'Sylhet', 'bn_name' => 'সিলেট'],
            ['id' => 7, 'name' => 'Rangpur', 'bn_name' => 'রংপুর'],
            ['id' => 8, 'name' => 'Mymensingh', 'bn_name' => 'ময়মনসিংহ'],
        ];

        foreach ($divisions as $div) {
            BdDivision::updateOrCreate(['id' => $div['id']], [
                'name' => $div['name'],
                'bn_name' => $div['bn_name'],
            ]);
        }

        // 64 Districts
        $districts = [
            // Dhaka Division (division_id: 1)
            ['id' => 1, 'division_id' => 1, 'name' => 'Dhaka', 'bn_name' => 'ঢাকা'],
            ['id' => 2, 'division_id' => 1, 'name' => 'Gazipur', 'bn_name' => 'গাজীপুর'],
            ['id' => 3, 'division_id' => 1, 'name' => 'Narayanganj', 'bn_name' => 'নারায়ণগঞ্জ'],
            ['id' => 4, 'division_id' => 1, 'name' => 'Tangail', 'bn_name' => 'টাঙ্গাইল'],
            ['id' => 5, 'division_id' => 1, 'name' => 'Faridpur', 'bn_name' => 'ফরিদপুর'],
            ['id' => 6, 'division_id' => 1, 'name' => 'Gopalganj', 'bn_name' => 'গোপালগঞ্জ'],
            ['id' => 7, 'division_id' => 1, 'name' => 'Kishoreganj', 'bn_name' => 'কিশোরগঞ্জ'],
            ['id' => 8, 'division_id' => 1, 'name' => 'Madaripur', 'bn_name' => 'মাদারীপুর'],
            ['id' => 9, 'division_id' => 1, 'name' => 'Manikganj', 'bn_name' => 'মানিকগঞ্জ'],
            ['id' => 10, 'division_id' => 1, 'name' => 'Munshiganj', 'bn_name' => 'মুন্সীগঞ্জ'],
            ['id' => 11, 'division_id' => 1, 'name' => 'Rajbari', 'bn_name' => 'রাজবাড়ী'],
            ['id' => 12, 'division_id' => 1, 'name' => 'Shariatpur', 'bn_name' => 'শরীয়তপুর'],
            ['id' => 13, 'division_id' => 1, 'name' => 'Narsingdi', 'bn_name' => 'নরসিংদী'],

            // Chattogram Division (division_id: 2)
            ['id' => 14, 'division_id' => 2, 'name' => 'Chattogram', 'bn_name' => 'চট্টগ্রাম'],
            ['id' => 15, 'division_id' => 2, 'name' => 'Coxs Bazar', 'bn_name' => 'কক্সবাজার'],
            ['id' => 16, 'division_id' => 2, 'name' => 'Cumilla', 'bn_name' => 'কুমিল্লা'],
            ['id' => 17, 'division_id' => 2, 'name' => 'Feni', 'bn_name' => 'ফেনী'],
            ['id' => 18, 'division_id' => 2, 'name' => 'Brahmanbaria', 'bn_name' => 'ব্রাহ্মণবাড়িয়া'],
            ['id' => 19, 'division_id' => 2, 'name' => 'Rangamati', 'bn_name' => 'রাঙ্গামাটি'],
            ['id' => 20, 'division_id' => 2, 'name' => 'Khagrachhari', 'bn_name' => 'খাগড়াছড়ি'],
            ['id' => 21, 'division_id' => 2, 'name' => 'Bandarban', 'bn_name' => 'বান্দরবান'],
            ['id' => 22, 'division_id' => 2, 'name' => 'Noakhali', 'bn_name' => 'নোয়াখালী'],
            ['id' => 23, 'division_id' => 2, 'name' => 'Lakshmipur', 'bn_name' => 'লক্ষ্মীপুর'],
            ['id' => 24, 'division_id' => 2, 'name' => 'Chandpur', 'bn_name' => 'চাঁদপুর'],

            // Rajshahi Division (division_id: 3)
            ['id' => 25, 'division_id' => 3, 'name' => 'Rajshahi', 'bn_name' => 'রাজশাহী'],
            ['id' => 26, 'division_id' => 3, 'name' => 'Bogra', 'bn_name' => 'বগুড়া'],
            ['id' => 27, 'division_id' => 3, 'name' => 'Joypurhat', 'bn_name' => 'জয়পুরহাট'],
            ['id' => 28, 'division_id' => 3, 'name' => 'Naogaon', 'bn_name' => 'নওগাঁ'],
            ['id' => 29, 'division_id' => 3, 'name' => 'Natore', 'bn_name' => 'নাটোর'],
            ['id' => 30, 'division_id' => 3, 'name' => 'Chapainawabganj', 'bn_name' => 'চাঁপাইনবাবগঞ্জ'],
            ['id' => 31, 'division_id' => 3, 'name' => 'Pabna', 'bn_name' => 'পাবনা'],
            ['id' => 32, 'division_id' => 3, 'name' => 'Sirajganj', 'bn_name' => 'সিরাজগঞ্জ'],

            // Khulna Division (division_id: 4)
            ['id' => 33, 'division_id' => 4, 'name' => 'Khulna', 'bn_name' => 'খুলনা'],
            ['id' => 34, 'division_id' => 4, 'name' => 'Jashore', 'bn_name' => 'যশোর'],
            ['id' => 35, 'division_id' => 4, 'name' => 'Satkhira', 'bn_name' => 'সাতক্ষীরা'],
            ['id' => 36, 'division_id' => 4, 'name' => 'Bagerhat', 'bn_name' => 'বাগেরহাট'],
            ['id' => 37, 'division_id' => 4, 'name' => 'Kushtia', 'bn_name' => 'কুষ্টিয়া'],
            ['id' => 38, 'division_id' => 4, 'name' => 'Meherpur', 'bn_name' => 'মেহেরপুর'],
            ['id' => 39, 'division_id' => 4, 'name' => 'Chuadanga', 'bn_name' => 'চুয়াডাঙ্গা'],
            ['id' => 40, 'division_id' => 4, 'name' => 'Jhenaidah', 'bn_name' => 'ঝিনাইদহ'],
            ['id' => 41, 'division_id' => 4, 'name' => 'Magura', 'bn_name' => 'মাগুরা'],
            ['id' => 42, 'division_id' => 4, 'name' => 'Narail', 'bn_name' => 'নড়াইল'],

            // Barishal Division (division_id: 5)
            ['id' => 43, 'division_id' => 5, 'name' => 'Barishal', 'bn_name' => 'বরিশাল'],
            ['id' => 44, 'division_id' => 5, 'name' => 'Patuakhali', 'bn_name' => 'পটুয়াখালী'],
            ['id' => 45, 'division_id' => 5, 'name' => 'Bhola', 'bn_name' => 'ভোলা'],
            ['id' => 46, 'division_id' => 5, 'name' => 'Pirojpur', 'bn_name' => 'পিরোজপুর'],
            ['id' => 47, 'division_id' => 5, 'name' => 'Barguna', 'bn_name' => 'বরগুনা'],
            ['id' => 48, 'division_id' => 5, 'name' => 'Jhalokati', 'bn_name' => 'ঝালকাঠি'],

            // Sylhet Division (division_id: 6)
            ['id' => 49, 'division_id' => 6, 'name' => 'Sylhet', 'bn_name' => 'সিলেট'],
            ['id' => 50, 'division_id' => 6, 'name' => 'Moulvibazar', 'bn_name' => 'মৌলভীবাজার'],
            ['id' => 51, 'division_id' => 6, 'name' => 'Habiganj', 'bn_name' => 'হবিগঞ্জ'],
            ['id' => 52, 'division_id' => 6, 'name' => 'Sunamganj', 'bn_name' => 'সুনামগঞ্জ'],

            // Rangpur Division (division_id: 7)
            ['id' => 53, 'division_id' => 7, 'name' => 'Rangpur', 'bn_name' => 'রংপুর'],
            ['id' => 54, 'division_id' => 7, 'name' => 'Dinajpur', 'bn_name' => 'দিনাজপুর'],
            ['id' => 55, 'division_id' => 7, 'name' => 'Kurigram', 'bn_name' => 'কুড়িগ্রাম'],
            ['id' => 56, 'division_id' => 7, 'name' => 'Gaibandha', 'bn_name' => 'গাইবান্ধা'],
            ['id' => 57, 'division_id' => 7, 'name' => 'Lalmonirhat', 'bn_name' => 'লালমনিরহাট'],
            ['id' => 58, 'division_id' => 7, 'name' => 'Nilphamari', 'bn_name' => 'নীলফামারী'],
            ['id' => 59, 'division_id' => 7, 'name' => 'Panchagarh', 'bn_name' => 'পঞ্চগড়'],
            ['id' => 60, 'division_id' => 7, 'name' => 'Thakurgaon', 'bn_name' => 'ঠাকুরগাঁও'],

            // Mymensingh Division (division_id: 8)
            ['id' => 61, 'division_id' => 8, 'name' => 'Mymensingh', 'bn_name' => 'ময়মনসিংহ'],
            ['id' => 62, 'division_id' => 8, 'name' => 'Jamalpur', 'bn_name' => 'জামালপুর'],
            ['id' => 63, 'division_id' => 8, 'name' => 'Netrokona', 'bn_name' => 'নেত্রকোণা'],
            ['id' => 64, 'division_id' => 8, 'name' => 'Sherpur', 'bn_name' => 'শেরপুর'],
        ];

        foreach ($districts as $dist) {
            BdDistrict::updateOrCreate(['id' => $dist['id']], [
                'division_id' => $dist['division_id'],
                'name' => $dist['name'],
                'bn_name' => $dist['bn_name'],
            ]);
        }

        // Seeding typical Upazilas for each District
        // We'll generate a comprehensive set of upazilas to keep database fully populated for checkout.
        $upazilas = [];
        $idCounter = 1;

        // Map typical upazilas for each district id
        $upazilaMap = [
            1 => ['Dhaka Sadar', 'Savar', 'Dhamrai', 'Keraniganj', 'Dohar', 'Nawabganj', 'Mirpur', 'Uttara', 'Gulshan', 'Dhanmondi', 'Motijheel'],
            2 => ['Gazipur Sadar', 'Kaliakair', 'Kaliganj', 'Kapasia', 'Sreepur'],
            3 => ['Narayanganj Sadar', 'Araihazar', 'Bandar', 'Rupganj', 'Sonargaon'],
            4 => ['Tangail Sadar', 'Basail', 'Bhuapur', 'Delduar', 'Ghatail', 'Gopalpur', 'Kalihati', 'Madhupur', 'Mirzapur', 'Nagarpur', 'Sakhipur'],
            5 => ['Faridpur Sadar', 'Alfadanga', 'Bhanga', 'Boalmari', 'Charbhadrasan', 'Madhukhali', 'Nagarkanda', 'Sadarpur', 'Saltha'],
            6 => ['Gopalganj Sadar', 'Kashiani', 'Kotalipara', 'Muksudpur', 'Tungipara'],
            7 => ['Kishoreganj Sadar', 'Bajitpur', 'Bhairab', 'Hossainpur', 'Itna', 'Karimganj', 'Katiadi', 'Kuliarchar', 'Mithamain', 'Nikli', 'Tarail'],
            8 => ['Madaripur Sadar', 'Kalkini', 'Rajoir', 'Shibchar'],
            9 => ['Manikganj Sadar', 'Singair', 'Shibalaya', 'Saturia', 'Harirampur', 'Ghior', 'Daulatpur'],
            10 => ['Munshiganj Sadar', 'Srinagar', 'Sirajdikhan', 'Lohajang', 'Tongibari', 'Gajaria'],
            11 => ['Rajbari Sadar', 'Baliakandi', 'Goalandaghat', 'Pangsha', 'Kalukhali'],
            12 => ['Shariatpur Sadar', 'Damudya', 'Naria', 'Jajira', 'Gosairhat', 'Bhedarganj'],
            13 => ['Narsingdi Sadar', 'Belabo', 'Monohardi', 'Palash', 'Raipura', 'Shibpur'],
            14 => ['Chattogram Sadar', 'Hathazari', 'Patiya', 'Raozan', 'Sandwip', 'Satkania', 'Sitakunda', 'Anwara', 'Banshkhali', 'Boalkhali', 'Chandanaish', 'Fatikchhari', 'Lohagara', 'Mirsharai'],
            15 => ['Coxs Bazar Sadar', 'Chakaria', 'Maheshkhali', 'Ramu', 'Teknaf', 'Ukhiya', 'Pekua'],
            16 => ['Cumilla Sadar', 'Barura', 'Chandina', 'Daudkandi', 'Debidwar', 'Homna', 'Laksam', 'Muradnagar', 'Nangalkot', 'Chauddagram'],
            17 => ['Feni Sadar', 'Chhagalnaiya', 'Daganbhuiyan', 'Parshuram', 'Sonavazi', 'Fulgazi'],
            18 => ['Brahmanbaria Sadar', 'Ashuganj', 'Bancharampur', 'Kasba', 'Nabinagar', 'Sarail', 'Nasirnagar'],
            19 => ['Rangamati Sadar', 'Baghaichhari', 'Barkal', 'Kawkhali', 'Rajasthali'],
            20 => ['Khagrachhari Sadar', 'Dighinala', 'Lakshmichhari', 'Panchhari', 'Ramgarh'],
            21 => ['Bandarban Sadar', 'Alikadam', 'Lama', 'Naikhongchhari', 'Ruma'],
            22 => ['Noakhali Sadar', 'Begumganj', 'Chatkhil', 'Companyganj', 'Hatiya', 'Senbagh', 'Subarnachar'],
            23 => ['Lakshmipur Sadar', 'Raipur', 'Ramganj', 'Ramgati', 'Kamalnagar'],
            24 => ['Chandpur Sadar', 'Faridganj', 'Hajiganj', 'Haimchar', 'Kachua', 'Matlab'],
            25 => ['Rajshahi Sadar', 'Bagha', 'Bagmara', 'Charghat', 'Durgapur', 'Godagari', 'Mohanpur', 'Paba', 'Puthia', 'Tanore'],
            26 => ['Bogra Sadar', 'Adamdighi', 'Dhunat', 'Dupchanchia', 'Gabtali', 'Kahaloo', 'Nandigram', 'Sariakandi', 'Sherpur', 'Shibganj', 'Sonatala'],
            27 => ['Joypurhat Sadar', 'Akkelpur', 'Kalai', 'Khetlal', 'Panchbibi'],
            28 => ['Naogaon Sadar', 'Atrai', 'Badalgachhi', 'Dhamoirhat', 'Manda', 'Mohadevpur', 'Niamatpur', 'Patnitala', 'Sapahar'],
            29 => ['Natore Sadar', 'Bagatipara', 'Baraigram', 'Gurudaspur', 'Lalpur', 'Singra'],
            30 => ['Nawabganj Sadar', 'Bholahat', 'Gomastapur', 'Nachole', 'Shibganj'],
            31 => ['Pabna Sadar', 'Atgharia', 'Bera', 'Bhangura', 'Chatmohar', 'Faridpur', 'Ishwardi', 'Santhia', 'Sujanagar'],
            32 => ['Sirajganj Sadar', 'Belkuchi', 'Chauhali', 'Kamarkhanda', 'Kazipur', 'Rayganj', 'Shahjadpur', 'Tarash', 'Ullahpara'],
            33 => ['Khulna Sadar', 'Batiaghata', 'Dacope', 'Dumuria', 'Digholia', 'Koyra', 'Paikgachha', 'Phultala', 'Rupsha'],
            34 => ['Jashore Sadar', 'Abhaynagar', 'Bagherpara', 'Chougachha', 'Jhikargachha', 'Keshabpur', 'Manirampur', 'Sharsha'],
            35 => ['Satkhira Sadar', 'Assasuni', 'Debhata', 'Kalaroa', 'Kaliganj', 'Shyamnagar', 'Tala'],
            36 => ['Bagerhat Sadar', 'Chitalmari', 'Fakirhat', 'Kachua', 'Mollahat', 'Mongla', 'Morrelganj', 'Rampal', 'Sarankhola'],
            37 => ['Kushtia Sadar', 'Bheramara', 'Daulatpur', 'Khoksa', 'Kumarkhali', 'Mirpur'],
            38 => ['Meherpur Sadar', 'Gangni', 'Mujibnagar'],
            39 => ['Chuadanga Sadar', 'Alamdanga', 'Damurhuda', 'Jibannagar'],
            40 => ['Jhenaidah Sadar', 'Harakunda', 'Kaliganj', 'Kotchandpur', 'Maheshpur', 'Shailkupa'],
            41 => ['Magura Sadar', 'Mohammadpur', 'Shalikha', 'Sreepur'],
            42 => ['Narail Sadar', 'Kalia', 'Lohagara'],
            43 => ['Barishal Sadar', 'Agailjhara', 'Babuganj', 'Bakerganj', 'Banaripara', 'Gaurnadi', 'Hizla', 'Mehendiganj', 'Muladi', 'Wazirpur'],
            44 => ['Patuakhali Sadar', 'Bauphal', 'Dashmina', 'Galachipa', 'Kalapara', 'Mirzaganj', 'Rangabali'],
            45 => ['Bhola Sadar', 'Burhanuddin', 'Char Fasson', 'Daulatkhan', 'Lalmohan', 'Manpura', 'Tazumuddin'],
            46 => ['Pirojpur Sadar', 'Bhandaria', 'Kawkhali', 'Mathbaria', 'Nazirpur', 'Nesarabad', 'Zianagar'],
            47 => ['Barguna Sadar', 'Amtali', 'Bamna', 'Betagi', 'Patharghata', 'Taltali'],
            48 => ['Jhalokati Sadar', 'Kathalia', 'Nalchity', 'Rajapur'],
            49 => ['Sylhet Sadar', 'Beanibazar', 'Bishwanath', 'Companiganj', 'Fenchuganj', 'Golapganj', 'Gowainghat', 'Jaintiapur', 'Kanaighat', 'Zakiganj'],
            50 => ['Moulvibazar Sadar', 'Barlekha', 'Kamalganj', 'Kulaura', 'Rajnagar', 'Sreemangal', 'Juri'],
            51 => ['Habiganj Sadar', 'Ajmiriganj', 'Bahubal', 'Baniachong', 'Chunarughat', 'Lakhai', 'Madhabpur', 'Nabiganj'],
            52 => ['Sunamganj Sadar', 'Dharampasha', 'Dowarabazar', 'Jagannathpur', 'Jamalganj', 'Tahirpur'],
            53 => ['Rangpur Sadar', 'Badarganj', 'Gangachara', 'Kaunia', 'Mithapukur', 'Pirgachha', 'Pirganj', 'Taraganj'],
            54 => ['Dinajpur Sadar', 'Biral', 'Bochaganj', 'Chirirbandar', 'Phulbari', 'Ghoraghat', 'Hakimpur', 'Kaharole', 'Khansama', 'Nawabganj', 'Parbatipur'],
            55 => ['Kurigram Sadar', 'Bhurungamari', 'Chilmari', 'Phulbari', 'Nageshwari', 'Rajarhat', 'Ulipur'],
            56 => ['Gaibandha Sadar', 'Gobindaganj', 'Palashbari', 'Sadullapur', 'Saghata', 'Sundarganj'],
            57 => ['Lalmonirhat Sadar', 'Aditmari', 'Hatibandha', 'Kaliganj', 'Patgram'],
            58 => ['Nilphamari Sadar', 'Dimla', 'Domar', 'Jaldhaka', 'Kishoreganj', 'Saidpur'],
            59 => ['Panchagarh Sadar', 'Atwari', 'Boda', 'Debiganj', 'Tetulia'],
            60 => ['Thakurgaon Sadar', 'Baliadangi', 'Haripur', 'Ranisankail', 'Pirganj'],
            61 => ['Mymensingh Sadar', 'Bhaluka', 'Gauripur', 'Haluaghat', 'Ishwarganj', 'Muktagachha', 'Nandail', 'Phulpur', 'Trishal'],
            62 => ['Jamalpur Sadar', 'Bakshiganj', 'Dewanganj', 'Isampur', 'Madarganj', 'Melandaha', 'Sarishabari'],
            63 => ['Netrokona Sadar', 'Atpara', 'Barhatta', 'Durgapur', 'Khaliajuri', 'Kalmakanda', 'Kendua', 'Madan', 'Mohanganj', 'Purbadhala'],
            64 => ['Sherpur Sadar', 'Jhenaigati', 'Nakla', 'Nalitabari', 'Sreebardi'],
        ];

        foreach ($upazilaMap as $districtId => $names) {
            foreach ($names as $name) {
                $upazilas[] = [
                    'id' => $idCounter++,
                    'district_id' => $districtId,
                    'name' => $name,
                    'bn_name' => null,
                ];
            }
        }

        // Insert in chunks of 100 to optimize SQL queries
        foreach (array_chunk($upazilas, 100) as $chunk) {
            foreach ($chunk as $upazila) {
                BdUpazila::updateOrCreate(['id' => $upazila['id']], [
                    'district_id' => $upazila['district_id'],
                    'name' => $upazila['name'],
                    'bn_name' => $upazila['bn_name'],
                ]);
            }
        }
    }
}
