<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use App\Models\User;

class RoleSeeder extends Seeder
{
    public function run(): void
    {
        // 1. Buat 3 Role Utama
        // Kita pakai firstOrCreate agar tidak error jika dijalankan berulang
        $roleSuperAdmin = Role::firstOrCreate(['name' => 'super-admin']);
        $roleAdminMapel = Role::firstOrCreate(['name' => 'admin-mapel']);
        $roleGuru       = Role::firstOrCreate(['name' => 'guru-mapel']);

        // 2. Buat User Super Admin Default
        $superAdmin = User::firstOrCreate([
            'email' => 'admin@sekolah.com' 
        ], [
            'name' => 'Super Administrator',
            'password' => bcrypt('password'), // Password default
        ]);

        // 3. Assign Role Super Admin ke user tersebut
        $superAdmin->assignRole($roleSuperAdmin);

        // 4. (Opsional) Update user "Guru Budi" yang tadi dibuat agar punya role Guru
        $guru = User::where('email', 'guru@sekolah.com')->first();
        if($guru) {
            $guru->assignRole($roleGuru);
        }
    }
}