<?php

namespace Database\Seeders;

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

class PostalCodeCityUpdaterSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $postalCodes = PostalCode::all();
        $this->command->getOutput()->progressStart(count($postalCodes));

        $postalCodes->each(function ($postalCode) {
            $city_name = explode(" - ", $postalCode->name)[1];
            $city = City::where('name', $city_name)->first();

            if ($city !== null) {
                $postalCode->city_id = $city->id;
                $postalCode->save();

                $this->command->getOutput()->progressAdvance();
            }
        });

        $this->command->getOutput()->progressFinish();
    }
}
