<?php

namespace Database\Seeders;

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

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $allSiswa = DummyDataService::getAllSiswa();

        foreach ($allSiswa as $data) {
            $nomorInduk = $data['nisn'] ?? $data['nip'] ?? null;
            
            if (!$nomorInduk) continue;

            User::updateOrCreate(
                ['nomor_induk' => $nomorInduk],
                [
                    'id' => $data['id'],
                    'name' => $data['nama_lengkap'],
                    'nama_lengkap' => $data['nama_lengkap'],
                    'email' => $data['email'],
                    'password' => Hash::make($data['password']),
                    'phone' => $data['nomor_hp'] ?? null,
                    'role' => $data['role'],
                    'is_banned' => $data['is_banned'] ?? false,
                    'kelas' => $data['kelas'] ?? null,
                    'golongan' => $data['golongan'] ?? null,
                ]
            );
        }

        // Default admin
        User::updateOrCreate(
            ['nomor_induk' => 'admin'],
            [
                'name' => 'Admin Perpustakaan',
                'nama_lengkap' => 'Admin Perpustakaan',
                'email' => 'admin@smkn1perpus.sch.id',
                'password' => Hash::make('password'),
                'role' => 'penjaga perpus',
            ]
        );
    }
}
