<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class ImportDataGuru extends Command
{
    protected $signature   = 'import:guru {--fresh : Hapus semua data guru sebelum import}';
    protected $description = 'Import data guru SMKN 1 Cirebon dari daftar bawaan';

    private array $data = [
        ['nama_guru' => 'IMAM MUZAENI, S.Pd.T',           'nip' => '197804130201011013'],
        ['nama_guru' => 'DIAN SRIANDINI, M.Pd',            'nip' => '197307162006021005'],
        ['nama_guru' => 'DIAN YUDIANTONO, S.Pd',           'nip' => '198110712011011008'],
        ['nama_guru' => 'Dr. NANA JULIAWNA',                'nip' => '196602141990031006'],
        ['nama_guru' => 'H. NASRUDIN, S.Pd',                'nip' => '197106152005011006'],
        ['nama_guru' => 'FUAD, S.Pd',                       'nip' => '198105152022211038'],
        ['nama_guru' => 'JAMALUDIN ZUHRI, S.Pd',           'nip' => ''],
        ['nama_guru' => 'SYAHRUL RONNY, M.Pd',             'nip' => '198704022006041005'],
        ['nama_guru' => 'HELENA KURNASIH, M.Pd',           'nip' => '197805052003122003'],
        ['nama_guru' => 'PRIYONO, S.Pd',                    'nip' => ''],
        ['nama_guru' => 'ALFIANDIE SHOFIA WARDAI, S.S.',   'nip' => ''],
        ['nama_guru' => 'SRIKADIS, T',                      'nip' => '197810132022211005'],
        ['nama_guru' => 'HERAWATI, S.Pd. M.Pd',            'nip' => ''],
        ['nama_guru' => 'SUKMANAH, S.Pd. MM',              'nip' => '197208043200112001'],
        ['nama_guru' => 'ELI LIMAENAH, S.Ag',              'nip' => '198009221995122001'],
        ['nama_guru' => 'SELLY APRILIANI, S.Pd',           'nip' => '199704221232212005'],
        ['nama_guru' => 'ANITA ZULFA1, S.Pd',              'nip' => '197002152008042006'],
        ['nama_guru' => 'ADE CAHYANI WIDKUM, S.Pd',        'nip' => ''],
        ['nama_guru' => 'TRIUTAMI, S.H',                    'nip' => ''],
        ['nama_guru' => 'MAMAN SUHERMAN, M.Pd',            'nip' => '156502192005011056'],
        ['nama_guru' => 'INSULINDE VULYANTI, M.Pd',        'nip' => '197807022022512005'],
        ['nama_guru' => 'TRESNA SUSLIAWATI, S.Pd',         'nip' => '198605224201201216'],
    ];

    public function handle(): int
    {
        $this->info('╔══════════════════════════════════════╗');
        $this->info('║   Import Data Guru — SMKN 1 Cirebon  ║');
        $this->info('╚══════════════════════════════════════╝');

        if ($this->option('fresh')) {
            if ($this->confirm('⚠️  Opsi --fresh akan menghapus SEMUA data guru. Lanjutkan?', false)) {
                DB::statement('SET FOREIGN_KEY_CHECKS=0');
                DB::table('guru')->truncate();
                DB::statement('SET FOREIGN_KEY_CHECKS=1');
                $this->warn('🗑  Tabel guru dikosongkan.');
            } else {
                $this->info('Dibatalkan.');
                return self::SUCCESS;
            }
        }

        $bar      = $this->output->createProgressBar(count($this->data));
        $inserted = 0;
        $updated  = 0;

        $bar->start();

        foreach ($this->data as $guru) {
            $exists = DB::table('guru')->where('nama_guru', $guru['nama_guru'])->exists();

            DB::table('guru')->updateOrInsert(
                ['nama_guru' => $guru['nama_guru']],
                [
                    'nip'      => $guru['nip'],
                    'id_profil' => 1,
                    'mapel'   => $guru['mapel']   ?? '',
                    'jabatan' => $guru['jabatan'] ?? '',
                    'foto'    => $guru['foto']    ?? '',
                ]
            );

            $exists ? $updated++ : $inserted++;
            $bar->advance();
        }

        $bar->finish();
        $this->newLine(2);

        $this->table(
            ['Status', 'Jumlah'],
            [
                ['✅ Ditambahkan', $inserted],
                ['🔄 Diperbarui',  $updated],
                ['📊 Total',        $inserted + $updated],
            ]
        );

        $this->info('Import selesai!');
        return self::SUCCESS;
    }
}
