<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class IndoRegionVillageSeeder extends Seeder
{
    /**
     * CATATAN: Seeder ini membutuhkan package azishapidin/laravel-indonesia
     *
     * Jika package belum terinstall, jalankan:
     * composer require azishapidin/laravel-indonesia
     *
     * Atau gunakan package alternatif: laravolt/indonesia
     * composer require laravolt/indonesia
     */
    public function run()
    {
        // Cek apakah package terinstall
        if (! class_exists('\AzisHapidin\IndoRegion\RawDataGetter')) {
            $this->command->warn('⚠️  Package azishapidin/laravel-indonesia tidak terinstall!');
            $this->command->warn('    Jalankan: composer require azishapidin/laravel-indonesia');
            $this->command->warn('    Atau gunakan: composer require laravolt/indonesia');
            $this->command->info('');
            $this->command->info('💡 Skip import desa/kelurahan (villages) - tidak diperlukan untuk checkout ongkir');

            return;
        }

        $this->command->info('📥 Import desa/kelurahan (harap tunggu, ini akan lama)...');

        try {
            $villages = \AzisHapidin\IndoRegion\RawDataGetter::getVillages();

            $total = count($villages);
            $this->command->info("Total: $total desa/kelurahan");

            foreach (array_chunk($villages, 500) as $index => $chunk) {
                DB::table('villages')->insert($chunk);
                $progress = ($index + 1) * 500;
                $this->command->info("Progress: $progress / $total desa/kelurahan...");
            }

            $this->command->info('✅ Berhasil import desa/kelurahan');

        } catch (\Exception $e) {
            $this->command->error('Error: '.$e->getMessage());
            $this->command->info('💡 Skip import desa/kelurahan - tidak diperlukan untuk checkout ongkir');
        }
    }
}
