<?php

namespace Database\Seeders;

use App\Models\Municipality;
use App\Models\Province;
use Illuminate\Database\Seeder;

class MunicipalitySeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $municipalities = [
            ['province_code' => 'BGO', 'name' => 'Caxito', 'code' => 'CAXITO'],
            ['province_code' => 'BGU', 'name' => 'Benguela', 'code' => 'BENGUELA'],
            ['province_code' => 'BIE', 'name' => 'Kuito', 'code' => 'KUITO'],
            ['province_code' => 'CAB', 'name' => 'Cabinda', 'code' => 'CABINDA'],
            ['province_code' => 'CCU', 'name' => 'Menongue', 'code' => 'MENONGUE'],
            ['province_code' => 'CNO', 'name' => 'Ndalatando', 'code' => 'NDALATANDO'],
            ['province_code' => 'CUS', 'name' => 'Sumbe', 'code' => 'SUMBE'],
            ['province_code' => 'CNN', 'name' => 'Ondjiva', 'code' => 'ONDJIVA'],
            ['province_code' => 'HUA', 'name' => 'Huambo', 'code' => 'HUAMBO'],
            ['province_code' => 'HUI', 'name' => 'Lubango', 'code' => 'LUBANGO'],
            ['province_code' => 'LUA', 'name' => 'Luanda', 'code' => 'LUANDA'],
            ['province_code' => 'LNO', 'name' => 'Dundo', 'code' => 'DUNDO'],
            ['province_code' => 'LSU', 'name' => 'Saurimo', 'code' => 'SAURIMO'],
            ['province_code' => 'MAL', 'name' => 'Malanje', 'code' => 'MALANJE'],
            ['province_code' => 'MOX', 'name' => 'Luena', 'code' => 'LUENA'],
            ['province_code' => 'NAM', 'name' => 'Namibe', 'code' => 'NAMIBE'],
            ['province_code' => 'UIG', 'name' => 'Uíge', 'code' => 'UIGE'],
            ['province_code' => 'ZAI', 'name' => 'Mbanza Kongo', 'code' => 'MBANZA_KONGO'],
        ];

        foreach ($municipalities as $municipality) {
            $province = Province::where('code', $municipality['province_code'])->first();

            if (! $province) {
                continue;
            }

            Municipality::firstOrCreate(
                ['code' => $municipality['code']],
                [
                    'province_id' => $province->id,
                    'name' => $municipality['name'],
                    'code' => $municipality['code'],
                ]
            );
        }
    }
}
