<?php

namespace Database\Seeders;

use App\Models\Role;
use App\Models\Shift;
use App\Models\User;
use Illuminate\Database\Seeder;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $roles = Role::pluck('id', 'name');
        $guruShift = Shift::where('name', 'Shift Guru & Staf TU')->first();
        $siswaShift = Shift::where('name', 'Shift Siswa Reguler')->first();
        $password = 'password123';

        $staff = [
            [
                'email' => 'superadmin@smkn1gunungagung.sch.id',
                'role' => 'super_admin',
                'shift_id' => null,
                'name' => 'Super Administrator',
                'nip_nis' => '198001012005011001',
                'phone_number' => '081234567890',
                'force_password_change' => false,
            ],
            [
                'email' => 'admin@smkn1gunungagung.sch.id',
                'role' => 'admin_tu',
                'shift_id' => $guruShift?->id,
                'name' => 'Admin Tata Usaha',
                'nip_nis' => '198501012010011001',
                'phone_number' => '081234567891',
                'force_password_change' => true,
            ],
            [
                'email' => 'kepsek@smkn1gunungagung.sch.id',
                'role' => 'kepala_sekolah',
                'shift_id' => $guruShift?->id,
                'name' => 'Drs. H. Bambang Irawan, M.Pd.',
                'nip_nis' => '196805121994031004',
                'phone_number' => '081234567892',
                'force_password_change' => false,
            ],
        ];

        foreach ($staff as $row) {
            User::updateOrCreate(
                ['email' => $row['email']],
                [
                    'role_id' => $roles[$row['role']],
                    'shift_id' => $row['shift_id'],
                    'name' => $row['name'],
                    'nip_nis' => $row['nip_nis'],
                    'phone_number' => $row['phone_number'],
                    'password' => $password,
                    'force_password_change' => $row['force_password_change'],
                    'is_active' => true,
                    'student_status' => 'aktif',
                ]
            );
        }

        $teachers = [
            ['name' => 'Siti Aminah, S.Pd.', 'nip_nis' => '198203152008012001', 'email' => 'siti.aminah@smkn1gunungagung.sch.id', 'phone_number' => '081345678901'],
            ['name' => 'Budi Santoso, S.Kom.', 'nip_nis' => '198704202011011002', 'email' => 'budi.santoso@smkn1gunungagung.sch.id', 'phone_number' => '081345678902'],
            ['name' => 'Rina Marlina, S.Pd.', 'nip_nis' => '199001102015012003', 'email' => 'rina.marlina@smkn1gunungagung.sch.id', 'phone_number' => '081345678903'],
            ['name' => 'Ahmad Fauzi, S.T.', 'nip_nis' => '198611252009011004', 'email' => 'ahmad.fauzi@smkn1gunungagung.sch.id', 'phone_number' => '081345678904'],
            ['name' => 'Dewi Lestari, S.Pd.', 'nip_nis' => '199205182016012005', 'email' => 'dewi.lestari@smkn1gunungagung.sch.id', 'phone_number' => '081345678905'],
            ['name' => 'Hendra Wijaya, S.Kom.', 'nip_nis' => '198909012012011006', 'email' => 'hendra.wijaya@smkn1gunungagung.sch.id', 'phone_number' => '081345678906'],
            ['name' => 'Nurul Hidayah, S.Pd.', 'nip_nis' => '199308222018012007', 'email' => 'nurul.hidayah@smkn1gunungagung.sch.id', 'phone_number' => '081345678907'],
            ['name' => 'Agus Priyanto, S.Pd.', 'nip_nis' => '198512101010011008', 'email' => 'agus.priyanto@smkn1gunungagung.sch.id', 'phone_number' => '081345678908'],
        ];

        foreach ($teachers as $teacher) {
            User::updateOrCreate(
                ['email' => $teacher['email']],
                [
                    'role_id' => $roles['guru'],
                    'shift_id' => $guruShift?->id,
                    'name' => $teacher['name'],
                    'nip_nis' => $teacher['nip_nis'],
                    'phone_number' => $teacher['phone_number'],
                    'password' => $password,
                    'force_password_change' => false,
                    'is_active' => true,
                    'student_status' => 'aktif',
                ]
            );
        }

        $classes = [
            'X RPL 1' => [
                ['Andi Pratama', '0081234561'],
                ['Salsa Bilqis', '0081234562'],
                ['Rizky Maulana', '0081234563'],
                ['Putri Ayu', '0081234564'],
                ['Dimas Saputra', '0081234565'],
            ],
            'X TKJ 1' => [
                ['Fajar Nugroho', '0081234571'],
                ['Maya Sari', '0081234572'],
                ['Ilham Ramadhan', '0081234573'],
                ['Nadia Putri', '0081234574'],
            ],
            'XI RPL 1' => [
                ['Bayu Firmansyah', '0071234561'],
                ['Citra Dewi', '0071234562'],
                ['Eko Prasetyo', '0071234563'],
                ['Fitri Handayani', '0071234564'],
                ['Gilang Ramadhan', '0071234565'],
            ],
            'XI TKJ 1' => [
                ['Hana Safira', '0071234571'],
                ['Irfan Hakim', '0071234572'],
                ['Joko Widodo', '0071234573'],
            ],
            'XII RPL 1' => [
                ['Kartika Sari', '0061234561'],
                ['Lukman Hakim', '0061234562'],
                ['Mega Wulandari', '0061234563'],
                ['Naufal Azmi', '0061234564'],
                ['Olivia Rahma', '0061234565'],
            ],
            'XII AKL 1' => [
                ['Pandu Wijaya', '0061234581'],
                ['Qori Amalia', '0061234582'],
                ['Rendi Kurniawan', '0061234583'],
            ],
        ];

        $phoneBase = 8123000000;
        $index = 0;

        foreach ($classes as $className => $students) {
            foreach ($students as [$name, $nis]) {
                $index++;
                $slug = strtolower(str_replace(' ', '.', $name));
                User::updateOrCreate(
                    ['email' => "{$slug}@siswa.smkn1gunungagung.sch.id"],
                    [
                        'role_id' => $roles['siswa'],
                        'shift_id' => $siswaShift?->id,
                        'name' => $name,
                        'nip_nis' => $nis,
                        'class_name' => $className,
                        'phone_number' => '08' . ($phoneBase + $index),
                        'password' => $password,
                        'force_password_change' => false,
                        'is_active' => true,
                        'student_status' => 'aktif',
                    ]
                );
            }
        }

        // Satu alumni untuk demo status lulus
        User::updateOrCreate(
            ['email' => 'alumni.demo@siswa.smkn1gunungagung.sch.id'],
            [
                'role_id' => $roles['siswa'],
                'shift_id' => null,
                'name' => 'Alumni Demo',
                'nip_nis' => '0051234599',
                'class_name' => 'XII RPL 1',
                'phone_number' => '081299999999',
                'password' => $password,
                'force_password_change' => false,
                'is_active' => false,
                'student_status' => 'lulus',
            ]
        );
    }
}
