<?php

namespace Database\Seeders;

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

/**
 * TrainingPermissionSeeder
 *
 * Seeds all permissions for the Training Module and assigns them to appropriate roles.
 *
 * Run: php artisan db:seed --class=TrainingPermissionSeeder
 *
 * @package Database\Seeders
 */
class TrainingPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
        
        // Define all training module permissions
        $permissions = $this->getPermissions();
        
        // Create permissions
        foreach ($permissions as $permission) {
            Permission::firstOrCreate(
                ['name' => $permission['name']],
                ['guard_name' => 'web']
            );
        }
        
        // Assign permissions to roles
        $this->assignPermissionsToRoles();
        
        $this->command->info('Training module permissions seeded successfully!');

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

    /**
     * Get all permissions for the training module.
     *
     * @return array
     */
    protected function getPermissions(): array
    {
        return [
            // Training Packages
            [
                'name' => 'view training packages',
                'description' => 'View training packages list and details',
            ],
            [
                'name' => 'create training packages',
                'description' => 'Create new training packages',
            ],
            [
                'name' => 'edit training packages',
                'description' => 'Edit existing training packages',
            ],
            [
                'name' => 'delete training packages',
                'description' => 'Delete training packages',
            ],
            [
                'name' => 'publish training packages',
                'description' => 'Publish/unpublish training packages',
            ],
            
            // Training Sessions
            [
                'name' => 'view training sessions',
                'description' => 'View training sessions list and calendar',
            ],
            [
                'name' => 'create training sessions',
                'description' => 'Create new training sessions',
            ],
            [
                'name' => 'edit training sessions',
                'description' => 'Edit existing training sessions',
            ],
            [
                'name' => 'delete training sessions',
                'description' => 'Delete training sessions',
            ],
            [
                'name' => 'schedule training sessions',
                'description' => 'Schedule and reschedule training sessions',
            ],
            [
                'name' => 'cancel training sessions',
                'description' => 'Cancel training sessions',
            ],
            
            // Trainer Management
            [
                'name' => 'view trainers',
                'description' => 'View trainers list',
            ],
            [
                'name' => 'assign trainers',
                'description' => 'Assign trainers to sessions',
            ],
            [
                'name' => 'remove trainers',
                'description' => 'Remove trainers from sessions',
            ],
            
            // Registrations
            [
                'name' => 'view training registrations',
                'description' => 'View training registrations list',
            ],
            [
                'name' => 'register for training',
                'description' => 'Register for training programs',
            ],
            [
                'name' => 'approve training registrations',
                'description' => 'Approve or reject training registrations',
            ],
            [
                'name' => 'edit training registrations',
                'description' => 'Edit training registration details',
            ],
            [
                'name' => 'cancel training registrations',
                'description' => 'Cancel training registrations',
            ],
            [
                'name' => 'complete training registrations',
                'description' => 'Mark training registrations as completed',
            ],
            
            // Attendance
            [
                'name' => 'view training attendance',
                'description' => 'View training attendance records',
            ],
            [
                'name' => 'manage training attendance',
                'description' => 'Check-in/check-out participants and manage attendance',
            ],
            [
                'name' => 'export training attendance',
                'description' => 'Export attendance records',
            ],
            
            // Certificates
            [
                'name' => 'view training certificates',
                'description' => 'View training certificates',
            ],
            [
                'name' => 'download training certificates',
                'description' => 'Download training certificates',
            ],
            [
                'name' => 'generate training certificates',
                'description' => 'Generate training certificates',
            ],
            [
                'name' => 'revoke training certificates',
                'description' => 'Revoke or restore training certificates',
            ],
            
            // Payments
            [
                'name' => 'view training payments',
                'description' => 'View training payment records',
            ],
            [
                'name' => 'process training payments',
                'description' => 'Process training payments',
            ],
            [
                'name' => 'refund training payments',
                'description' => 'Process refunds for training payments',
            ],
            
            // Reports
            [
                'name' => 'view training reports',
                'description' => 'View training reports and analytics',
            ],
            [
                'name' => 'export training reports',
                'description' => 'Export training reports',
            ],
            
            // Settings
            [
                'name' => 'manage training settings',
                'description' => 'Manage training module settings',
            ],
            [
                'name' => 'manage training categories',
                'description' => 'Manage training categories',
            ],
        ];
    }

    /**
     * Assign permissions to roles.
     *
     * @return void
     */
    protected function assignPermissionsToRoles(): void
    {
        // Super Manager - Full Access
        $superManager = Role::findByName('Super Manager');
        if ($superManager) {
            $superManager->givePermissionTo(Permission::whereIn('name', [
                'view training packages',
                'create training packages',
                'edit training packages',
                'delete training packages',
                'publish training packages',
                'view training sessions',
                'create training sessions',
                'edit training sessions',
                'delete training sessions',
                'schedule training sessions',
                'cancel training sessions',
                'view trainers',
                'assign trainers',
                'remove trainers',
                'view training registrations',
                'register for training',
                'approve training registrations',
                'edit training registrations',
                'cancel training registrations',
                'complete training registrations',
                'view training attendance',
                'manage training attendance',
                'export training attendance',
                'view training certificates',
                'download training certificates',
                'generate training certificates',
                'revoke training certificates',
                'view training payments',
                'process training payments',
                'refund training payments',
                'view training reports',
                'export training reports',
                'manage training settings',
                'manage training categories',
            ])->get());
        }
        
        // Manager - Administrative Access
        $manager = Role::findByName('Manager');
        if ($manager) {
            $manager->givePermissionTo([
                'view training packages',
                'create training packages',
                'edit training packages',
                'delete training packages',
                'publish training packages',
                'view training sessions',
                'create training sessions',
                'edit training sessions',
                'delete training sessions',
                'schedule training sessions',
                'cancel training sessions',
                'view trainers',
                'assign trainers',
                'remove trainers',
                'view training registrations',
                'register for training',
                'approve training registrations',
                'edit training registrations',
                'cancel training registrations',
                'complete training registrations',
                'view training attendance',
                'manage training attendance',
                'export training attendance',
                'view training certificates',
                'download training certificates',
                'generate training certificates',
                'view training payments',
                'view training reports',
                'export training reports',
            ]);
        }
        
        // Trainers - Trainer Access
        $trainers = Role::findByName('Trainers');
        if ($trainers) {
            $trainers->givePermissionTo([
                'view training packages',
                'view training sessions',
                'view trainers',
                'view training registrations',
                'view training attendance',
                'manage training attendance',
                'view training certificates',
                'download training certificates',
            ]);
        }
        
        // Trainees - Limited Access
        $trainees = Role::findByName('Trainees');
        if ($trainees) {
            $trainees->givePermissionTo([
                'view training packages',
                'view training sessions',
                'register for training',
                'view training attendance',
                'view training certificates',
                'download training certificates',
            ]);
        }
        
        // Members - Basic Access
        $members = Role::findByName('Members');
        if ($members) {
            $members->givePermissionTo([
                'view training packages',
                'view training sessions',
                'register for training',
                'view training certificates',
                'download training certificates',
            ]);
        }
        
        // Mentor - Basic Training Access
        $mentor = Role::findByName('Mentor');
        if ($mentor) {
            $mentor->givePermissionTo([
                'view training packages',
                'register for training',
                'view training sessions',
                'view training certificates',
                'download training certificates',
            ]);
        }
        
        // Mentee - Basic Access
        $mentee = Role::findByName('Mentee');
        if ($mentee) {
            $mentee->givePermissionTo([
                'view training packages',
                'register for training',
                'view training certificates',
                'download training certificates',
            ]);
        }
        
        // Partners - Partner Access
        $partners = Role::findByName('Partners');
        if ($partners) {
            $partners->givePermissionTo([
                'view training packages',
                'register for training',
                'view training certificates',
                'download training certificates',
            ]);
        }
        
        // Contributor - Content Access
        $contributor = Role::findByName('Contributor');
        if ($contributor) {
            $contributor->givePermissionTo([
                'view training packages',
                'view training sessions',
                'register for training',
                'view training certificates',
                'download training certificates',
            ]);
        }
        
        // Health Marketor - Marketing Access
        $healthMarketor = Role::findByName('Health Marketor');
        if ($healthMarketor) {
            $healthMarketor->givePermissionTo([
                'view training packages',
                'view training sessions',
                'view training registrations',
                'view training reports',
                'export training reports',
            ]);
        }
    }
}
