<?php

namespace Database\Seeders;

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

class PermissionSeeder extends Seeder
{
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Create permission
        Permission::firstOrCreate(['name' => 'access-admin', 'guard_name' => 'web']);

        // Create admin role with permission
        $adminRole = Role::firstOrCreate(['name' => 'admin', 'guard_name' => 'web']);
        $adminRole->givePermissionTo('access-admin');

        $this->command->info('✅ Permissions & roles seeded!');

        // If there are users with discord_id = '000000000000000001', assign admin role
        $adminUser = User::where('discord_id', '000000000000000001')->first();
        if ($adminUser) {
            $adminUser->assignRole('admin');
            $this->command->info("   Admin role assigned to: {$adminUser->username}");
        }
    }
}
