<?php

namespace Database\Seeders;

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

class RolePermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
        
        // Define permission types
        $permissionTypes = ['view', 'create', 'edit', 'delete', 'approve', 'deactivate', 'manage'];
        
        // Define resources
        $resources = ['users', 'resources', 'reports', 'settings'];
        
        // Create permissions
        foreach ($resources as $resource) {
            foreach ($permissionTypes as $type) {
                Permission::firstOrCreate(['name' => "$type $resource"]);
            }
        }
        
        // Define roles with themes — slugs must match ThemeSeeder slugs exactly
        $roles = [
            ['name' => 'Super Manager',   'theme' => 'midnight-dark'],
            ['name' => 'Manager',         'theme' => 'ocean-blue'],
            ['name' => 'Members',         'theme' => 'arctic-white'],
            ['name' => 'Trainers',        'theme' => 'desert-sand'],
            ['name' => 'Trainees',        'theme' => 'forest-green'],
            ['name' => 'Mentor',          'theme' => 'royal-purple'],
            ['name' => 'Mentee',          'theme' => 'sunset-orange'],
            ['name' => 'Partners',        'theme' => 'corporate-gray'],
            ['name' => 'Contributor',     'theme' => 'cherry-blossom'],
            ['name' => 'Health Marketor', 'theme' => 'crimson-red'],
        ];
        
        foreach ($roles as $roleData) {
            $role = Role::firstOrCreate(['name' => $roleData['name']]);
            $role->update(['theme' => $roleData['theme']]);
        }
        
        // Assign all permissions to Super Manager
        $superManager = Role::findByName('Super Manager');
        $superManager->givePermissionTo(Permission::all());
        
        // Assign specific permissions to other roles
        $manager = Role::findByName('Manager');
        $manager->givePermissionTo([
            'view users', 'create users', 'edit users', 'manage users',
            'view resources', 'view reports',
            'create resources', 'edit resources',
            'approve resources'
        ]);
        
        $members = Role::findByName('Members');
        $members->givePermissionTo([
            'view resources', 'create resources', 'edit resources'
        ]);
        
        // Create a default Super Manager user (if not exists)
        $user = User::firstOrCreate(
            ['email' => 'admin@sshia.com'],
            [
                'name'     => 'Super Admin',
                'username' => 'superadmin',
                'password' => Hash::make('password'),
                'is_active' => true,
            ]
        );
        
        if (!$user->hasRole('Super Manager')) {
            $user->assignRole('Super Manager');
        }
        
        $this->command->info('Roles and permissions seeded successfully!');
        $this->command->info('Default Super Manager: admin@sshia.com / password');

        // Clear permission cache after seeding
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
    }
}
