<?php

namespace Database\Seeders;

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

class RolesAndPermissionsSeeder extends Seeder
{
    public function run(): void
    {
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        $roles = ['super_admin', 'ketua', 'bendahara', 'pengurus', 'anggota', 'auditor'];

        foreach ($roles as $role) {
            Role::firstOrCreate(['name' => $role, 'guard_name' => 'web']);
        }

        // Generate Shield permissions untuk semua resource
        \Artisan::call('shield:generate', ['--all' => true, '--panel' => 'admin']);

        // super_admin mendapat semua permission via Shield config
        $superAdmin = User::firstOrCreate(
            ['email' => 'admin@persegi.id'],
            [
                'name'     => 'Super Admin',
                'password' => bcrypt('password'),
                'phone'    => '08123456789',
                'status'   => 'aktif',
            ]
        );

        $superAdmin->assignRole('super_admin');
    }
}
