<?php

namespace Database\Seeders;

use App\Models\Address;
use App\Models\City;
use Illuminate\Database\Seeder;
use App\Models\Region;
use Illuminate\Support\Facades\File;

class RegionsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Delete all regions and cities
        Address::truncate();
        City::truncate();
        Region::truncate();

        $contents = File::get(base_path('public/regions.json'));
        $json = json_decode(json: $contents, associative: true);
        $regions = $json['regions'];
        $cities = $json['districts'];

        foreach ($regions as $region) {
            Region::create([
                'id' => $region['id'],
                'name' => [
                    'uz' => $region['name'],
                    'ru' => $region['name']
                ]
            ]);
        }

        foreach ($cities as $city) {
            City::create([
                'region_id' => $city['region_id'],
                'name' => [
                    'uz' => $city['name'],
                    'ru' => $city['name']
                ],
            ]);
        }
    }
}
