<?php

namespace Database\Seeders;

use App\Models\City;
use App\Models\Corridor;
use Illuminate\Database\Seeder;

class CitiesSeeder extends Seeder
{
    public function run(): void
    {
        $cities = [
            ['name_sw' => 'Dar es Salaam', 'name_en' => 'Dar es Salaam', 'region' => 'Dar es Salaam', 'lat' => -6.7924, 'lng' => 39.2083],
            ['name_sw' => 'Dodoma',        'name_en' => 'Dodoma',        'region' => 'Dodoma',        'lat' => -6.1722, 'lng' => 35.7395],
            ['name_sw' => 'Mwanza',        'name_en' => 'Mwanza',        'region' => 'Mwanza',        'lat' => -2.5163, 'lng' => 32.9175],
            ['name_sw' => 'Arusha',        'name_en' => 'Arusha',        'region' => 'Arusha',        'lat' => -3.3869, 'lng' => 36.6830],
            ['name_sw' => 'Moshi',         'name_en' => 'Moshi',         'region' => 'Kilimanjaro',   'lat' => -3.3500, 'lng' => 37.3400],
            ['name_sw' => 'Morogoro',      'name_en' => 'Morogoro',      'region' => 'Morogoro',      'lat' => -6.8230, 'lng' => 37.6590],
            ['name_sw' => 'Mbeya',         'name_en' => 'Mbeya',         'region' => 'Mbeya',         'lat' => -8.9094, 'lng' => 33.4608],
            ['name_sw' => 'Iringa',        'name_en' => 'Iringa',        'region' => 'Iringa',        'lat' => -7.7700, 'lng' => 35.7000],
            ['name_sw' => 'Tanga',         'name_en' => 'Tanga',         'region' => 'Tanga',         'lat' => -5.0692, 'lng' => 39.1031],
            ['name_sw' => 'Tabora',        'name_en' => 'Tabora',        'region' => 'Tabora',        'lat' => -5.0167, 'lng' => 32.8000],
        ];

        foreach ($cities as $city) {
            City::firstOrCreate(['name_en' => $city['name_en']], $city);
        }

        $dsm     = City::where('name_en', 'Dar es Salaam')->first();
        $dodoma  = City::where('name_en', 'Dodoma')->first();
        $mwanza  = City::where('name_en', 'Mwanza')->first();
        $arusha  = City::where('name_en', 'Arusha')->first();
        $moshi   = City::where('name_en', 'Moshi')->first();
        $morogoro = City::where('name_en', 'Morogoro')->first();

        $corridors = [
            // Phase 1 — PRIMARY
            [$dsm->id,    $dodoma->id,  12000, 450],
            [$dodoma->id, $dsm->id,     12000, 450],
            [$dsm->id,    $mwanza->id,  25000, 1100],
            [$mwanza->id, $dsm->id,     25000, 1100],
            [$arusha->id, $moshi->id,   3000,  90],
            [$moshi->id,  $arusha->id,  3000,  90],
            [$dsm->id,    $arusha->id,  16000, 650],
            [$arusha->id, $dsm->id,     16000, 650],
            [$dodoma->id, $mwanza->id,  16000, 690],
            [$mwanza->id, $dodoma->id,  16000, 690],
            // Phase 2 additions
            [$dsm->id,    $morogoro->id, 5000, 195],
            [$morogoro->id, $dsm->id,    5000, 195],
        ];

        foreach ($corridors as [$from, $to, $min, $dist]) {
            Corridor::firstOrCreate(
                ['from_city_id' => $from, 'to_city_id' => $to],
                ['min_price_tzs' => $min, 'distance_km' => $dist, 'is_active' => true]
            );
        }
    }
}
