<?php

namespace Database\Seeders;

use App\Models\Geo\City;
use App\Models\Geo\District;
use App\Models\Geo\Province;
use Illuminate\Database\Seeder;

/**
 * Seeds Sri Lanka provinces / districts / cities.
 *
 * Falls back to a hard-coded built-in dataset if no internet — this seeder is
 * idempotent and safe to re-run.
 */
class GeographySeeder extends Seeder
{
    public function run(): void
    {
        foreach ($this->dataset() as $provinceName => $districts) {
            $province = Province::firstOrCreate(['name' => $provinceName]);

            foreach ($districts as $districtName => $cities) {
                $district = District::firstOrCreate(
                    ['province_id' => $province->id, 'name' => $districtName],
                );

                foreach ($cities as $cityName) {
                    City::firstOrCreate([
                        'district_id' => $district->id,
                        'name' => $cityName,
                    ]);
                }
            }
        }
    }

    /**
     * Built-in dataset (9 provinces, 25 districts, ~150 cities).
     */
    private function dataset(): array
    {
        return [
            'Western' => [
                'Colombo' => ['Colombo', 'Dehiwala', 'Moratuwa', 'Kotte', 'Maharagama', 'Kesbewa', 'Kolonnawa', 'Homagama', 'Avissawella'],
                'Gampaha' => ['Gampaha', 'Negombo', 'Wattala', 'Ja-Ela', 'Kelaniya', 'Minuwangoda', 'Kadawatha', 'Ragama', 'Kiribathgoda'],
                'Kalutara' => ['Kalutara', 'Panadura', 'Horana', 'Beruwala', 'Aluthgama', 'Matugama', 'Wadduwa'],
            ],
            'Central' => [
                'Kandy' => ['Kandy', 'Peradeniya', 'Gampola', 'Nawalapitiya', 'Katugastota', 'Kundasale'],
                'Matale' => ['Matale', 'Dambulla', 'Sigiriya', 'Galewela', 'Rattota'],
                'Nuwara Eliya' => ['Nuwara Eliya', 'Hatton', 'Talawakelle', 'Maskeliya', 'Ginigathena'],
            ],
            'Southern' => [
                'Galle' => ['Galle', 'Hikkaduwa', 'Ambalangoda', 'Bentota', 'Karapitiya', 'Unawatuna'],
                'Matara' => ['Matara', 'Weligama', 'Mirissa', 'Akuressa', 'Kamburupitiya'],
                'Hambantota' => ['Hambantota', 'Tangalle', 'Tissamaharama', 'Beliatta', 'Ambalantota'],
            ],
            'Northern' => [
                'Jaffna' => ['Jaffna', 'Chavakachcheri', 'Point Pedro', 'Nallur', 'Velanai'],
                'Kilinochchi' => ['Kilinochchi', 'Pallai'],
                'Mannar' => ['Mannar', 'Madhu', 'Pesalai'],
                'Vavuniya' => ['Vavuniya', 'Cheddikulam', 'Nedunkeni'],
                'Mullaitivu' => ['Mullaitivu', 'Puthukkudiyiruppu', 'Oddusuddan'],
            ],
            'Eastern' => [
                'Trincomalee' => ['Trincomalee', 'Kinniya', 'Muttur', 'Nilaveli'],
                'Batticaloa' => ['Batticaloa', 'Eravur', 'Kattankudy', 'Valaichchenai'],
                'Ampara' => ['Ampara', 'Kalmunai', 'Sammanthurai', 'Akkaraipattu'],
            ],
            'North Western' => [
                'Kurunegala' => ['Kurunegala', 'Kuliyapitiya', 'Pannala', 'Polgahawela', 'Maho'],
                'Puttalam' => ['Puttalam', 'Chilaw', 'Anamaduwa', 'Wennappuwa'],
            ],
            'North Central' => [
                'Anuradhapura' => ['Anuradhapura', 'Kekirawa', 'Mihintale', 'Talawa'],
                'Polonnaruwa' => ['Polonnaruwa', 'Hingurakgoda', 'Medirigiriya', 'Kaduruwela'],
            ],
            'Uva' => [
                'Badulla' => ['Badulla', 'Bandarawela', 'Ella', 'Welimada', 'Mahiyanganaya', 'Haputale', 'Passara'],
                'Monaragala' => ['Monaragala', 'Wellawaya', 'Bibile', 'Buttala'],
            ],
            'Sabaragamuwa' => [
                'Ratnapura' => ['Ratnapura', 'Embilipitiya', 'Balangoda', 'Eheliyagoda', 'Pelmadulla'],
                'Kegalle' => ['Kegalle', 'Mawanella', 'Rambukkana', 'Warakapola'],
            ],
        ];
    }
}
