<?php

namespace Database\Seeders;

use App\Models\Role;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $adminRole = Role::where('nama', 'admin')->first();
        $bendaharaRole = Role::where('nama', 'bendahara')->first();
        $kepsekRole = Role::where('nama', 'kepala_sekolah')->first();
        $yayasanRole = Role::where('nama', 'yayasan')->first();
        $waliKelasRole = Role::where('nama', 'wali_kelas')->first();
        
        $kelas = \App\Models\Kelas::first();

        // Admin default
        User::updateOrCreate(
            ['username' => 'admin'],
            [
                'name' => 'Administrator',
                'email' => 'admin@sppapp.test',
                'password' => Hash::make('admin123'),
                'role_id' => $adminRole->id,
                'is_active' => true,
            ]
        );

        // Bendahara
        User::updateOrCreate(
            ['username' => 'bendahara'],
            [
                'name' => 'Siti Nurhasanah',
                'email' => 'bendahara@sppapp.test',
                'password' => Hash::make('bendahara123'),
                'role_id' => $bendaharaRole->id,
                'is_active' => true,
            ]
        );

        // Kepala Sekolah
        User::updateOrCreate(
            ['username' => 'kepsek'],
            [
                'name' => 'Dr. Ahmad Fauzi, M.Pd',
                'email' => 'kepsek@sppapp.test',
                'password' => Hash::make('kepsek123'),
                'role_id' => $kepsekRole->id,
                'jenjang' => 'SMP', // Default testing jenjang
                'is_active' => true,
            ]
        );

        // Pimpinan Yayasan
        User::updateOrCreate(
            ['username' => 'yayasan'],
            [
                'name' => 'H. Muhammad Ridwan',
                'email' => 'yayasan@sppapp.test',
                'password' => Hash::make('yayasan123'),
                'role_id' => $yayasanRole->id,
                'is_active' => true,
            ]
        );

        // Wali Kelas (assign to first available class if exists)
        User::updateOrCreate(
            ['username' => 'walikelas'],
            [
                'name' => 'Budi Santoso, S.Pd',
                'email' => 'walikelas@sppapp.test',
                'password' => Hash::make('walikelas123'),
                'role_id' => $waliKelasRole->id,
                'kelas_id' => $kelas ? $kelas->id : null,
                'is_active' => true,
            ]
        );

    }
}
