<?php

namespace Database\Seeders;

use App\Modules\Platform\Geography\Models\GeoCity;
use App\Modules\Platform\Geography\Models\GeoCountry;
use App\Modules\Platform\Geography\Models\GeoRegion;
use Illuminate\Database\Seeder;

class GeographySeeder extends Seeder
{
    public function run(): void
    {
        $countries = require database_path('data/geography.php');

        foreach ($countries as $countrySort => $countryData) {
            $country = GeoCountry::query()->firstOrCreate(
                ['code' => $countryData['code']],
                [
                    'name' => $countryData['name'],
                    'is_active' => true,
                    'sort_order' => $countrySort + 1,
                ],
            );

            foreach ($countryData['regions'] as $regionSort => $regionData) {
                $region = GeoRegion::query()->firstOrCreate(
                    [
                        'country_id' => $country->id,
                        'tenant_id' => null,
                        'name->en' => $regionData['name']['en'],
                    ],
                    [
                        'name' => $regionData['name'],
                        'is_active' => true,
                        'sort_order' => $regionSort + 1,
                    ],
                );

                foreach ($regionData['cities'] as $citySort => $cityName) {
                    GeoCity::query()->firstOrCreate(
                        [
                            'region_id' => $region->id,
                            'tenant_id' => null,
                            'name->en' => $cityName['en'],
                        ],
                        [
                            'name' => $cityName,
                            'is_active' => true,
                            'sort_order' => $citySort + 1,
                        ],
                    );
                }
            }
        }
    }
}
