<?php

namespace Database\Seeders;

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

class MentorshipPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * This seeder creates all permissions for the Mentorship Management Module
     * and assigns them to the appropriate roles.
     */
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Define all mentorship permissions
        $permissions = $this->getPermissions();

        // Create permissions
        foreach ($permissions as $permission) {
            Permission::firstOrCreate(
                ['name' => $permission],
                ['guard_name' => 'web']
            );
        }

        $this->command->info('Mentorship permissions created successfully.');

        // Assign permissions to roles
        $this->assignPermissionsToRoles();

        $this->command->info('Mentorship permissions assigned to roles successfully.');

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

    /**
     * Get all mentorship permissions organized by module.
     */
    protected function getPermissions(): array
    {
        return [
            // Categories
            'view mentorship categories',
            'create mentorship categories',
            'edit mentorship categories',
            'delete mentorship categories',

            // Programmes
            'view mentorship programmes',
            'create mentorship programmes',
            'edit mentorship programmes',
            'delete mentorship programmes',

            // Applications
            'view mentorship applications',
            'view own mentorship applications',
            'review mentorship applications',
            'approve mentorship applications',
            'reject mentorship applications',

            // Assignments
            'view mentorship assignments',
            'create mentorship assignments',
            'edit mentorship assignments',
            'complete mentorship assignments',
            'cancel mentorship assignments',
            'reassign mentorship assignments',

            // Sessions
            'view mentorship sessions',
            'create mentorship sessions',
            'edit mentorship sessions',
            'delete mentorship sessions',
            'cancel mentorship sessions',
            'complete mentorship sessions',

            // Certificates
            'view mentorship certificates',
            'download mentorship certificates',
            'generate mentorship certificates',
            'revoke mentorship certificates',

            // Payments
            'view mentorship payments',
            'view own mentorship payments',

            // Statistics
            'view mentorship statistics',
        ];
    }

    /**
     * Assign permissions to roles.
     */
    protected function assignPermissionsToRoles(): void
    {
        // Super Manager - All permissions
        $superManager = Role::where('name', 'Super Manager')->first();
        if ($superManager) {
            $superManager->givePermissionTo(Permission::all());
            $this->command->info('All mentorship permissions assigned to Super Manager.');
        }

        // Manager - Most permissions except delete and revoke
        $manager = Role::where('name', 'Manager')->first();
        if ($manager) {
            $managerPermissions = [
                // Categories
                'view mentorship categories',
                'create mentorship categories',
                'edit mentorship categories',

                // Programmes
                'view mentorship programmes',
                'create mentorship programmes',
                'edit mentorship programmes',

                // Applications
                'view mentorship applications',
                'review mentorship applications',
                'approve mentorship applications',
                'reject mentorship applications',

                // Assignments
                'view mentorship assignments',
                'create mentorship assignments',
                'edit mentorship assignments',
                'complete mentorship assignments',
                'cancel mentorship assignments',
                'reassign mentorship assignments',

                // Sessions
                'view mentorship sessions',
                'create mentorship sessions',
                'edit mentorship sessions',
                'cancel mentorship sessions',
                'complete mentorship sessions',

                // Certificates
                'view mentorship certificates',
                'download mentorship certificates',
                'generate mentorship certificates',

                // Payments
                'view mentorship payments',

                // Statistics
                'view mentorship statistics',
            ];
            $manager->givePermissionTo($managerPermissions);
            $this->command->info('Mentorship permissions assigned to Manager.');
        }

        // Mentor - Limited permissions for their own assignments
        $mentor = Role::where('name', 'Mentor')->first();
        if ($mentor) {
            $mentorPermissions = [
                'view mentorship programmes',
                'view mentorship assignments',
                'edit mentorship assignments',
                'view mentorship sessions',
                'create mentorship sessions',
                'edit mentorship sessions',
                'complete mentorship sessions',
                'cancel mentorship sessions',
                'view mentorship certificates',
                'download mentorship certificates',
                'view mentorship statistics',
                'view own mentorship applications',
                'view own mentorship payments',
            ];
            $mentor->givePermissionTo($mentorPermissions);
            $this->command->info('Mentorship permissions assigned to Mentor.');
        }

        // Mentee - Very limited permissions
        $mentee = Role::where('name', 'Mentee')->first();
        if ($mentee) {
            $menteePermissions = [
                'view mentorship programmes',
                'view mentorship assignments',
                'view mentorship sessions',
                'view mentorship certificates',
                'download mentorship certificates',
                'view own mentorship applications',
            ];
            $mentee->givePermissionTo($menteePermissions);
            $this->command->info('Mentorship permissions assigned to Mentee.');
        }

        // Trainers - Can view programmes and sessions
        $trainers = Role::where('name', 'Trainers')->first();
        if ($trainers) {
            $trainersPermissions = [
                'view mentorship programmes',
                'view mentorship categories',
                'view mentorship sessions',
                'view mentorship statistics',
            ];
            $trainers->givePermissionTo($trainersPermissions);
            $this->command->info('Mentorship permissions assigned to Trainers.');
        }

        // Trainees - Can view programmes
        $trainees = Role::where('name', 'Trainees')->first();
        if ($trainees) {
            $traineesPermissions = [
                'view mentorship programmes',
                'view mentorship categories',
            ];
            $trainees->givePermissionTo($traineesPermissions);
            $this->command->info('Mentorship permissions assigned to Trainees.');
        }

        // Members - Basic viewing permissions
        $members = Role::where('name', 'Members')->first();
        if ($members) {
            $membersPermissions = [
                'view mentorship programmes',
                'view mentorship categories',
            ];
            $members->givePermissionTo($membersPermissions);
            $this->command->info('Mentorship permissions assigned to Members.');
        }

        // Partners - Can view programmes
        $partners = Role::where('name', 'Partners')->first();
        if ($partners) {
            $partnersPermissions = [
                'view mentorship programmes',
                'view mentorship categories',
                'view mentorship sessions',
            ];
            $partners->givePermissionTo($partnersPermissions);
            $this->command->info('Mentorship permissions assigned to Partners.');
        }

        // Contributor - Can view programmes
        $contributor = Role::where('name', 'Contributor')->first();
        if ($contributor) {
            $contributorPermissions = [
                'view mentorship programmes',
                'view mentorship categories',
            ];
            $contributor->givePermissionTo($contributorPermissions);
            $this->command->info('Mentorship permissions assigned to Contributor.');
        }

        // Health Marketor - Can view programmes and statistics
        $healthMarketor = Role::where('name', 'Health Marketor')->first();
        if ($healthMarketor) {
            $healthMarketorPermissions = [
                'view mentorship programmes',
                'view mentorship categories',
                'view mentorship statistics',
            ];
            $healthMarketor->givePermissionTo($healthMarketorPermissions);
            $this->command->info('Mentorship permissions assigned to Health Marketor.');
        }
    }

    /**
     * Get permissions grouped by category for documentation.
     */
    public static function getPermissionsGrouped(): array
    {
        return [
            'Categories' => [
                'view mentorship categories',
                'create mentorship categories',
                'edit mentorship categories',
                'delete mentorship categories',
            ],
            'Programmes' => [
                'view mentorship programmes',
                'create mentorship programmes',
                'edit mentorship programmes',
                'delete mentorship programmes',
            ],
            'Applications' => [
                'view mentorship applications',
                'view own mentorship applications',
                'review mentorship applications',
                'approve mentorship applications',
                'reject mentorship applications',
            ],
            'Assignments' => [
                'view mentorship assignments',
                'create mentorship assignments',
                'edit mentorship assignments',
                'complete mentorship assignments',
                'cancel mentorship assignments',
                'reassign mentorship assignments',
            ],
            'Sessions' => [
                'view mentorship sessions',
                'create mentorship sessions',
                'edit mentorship sessions',
                'delete mentorship sessions',
                'cancel mentorship sessions',
                'complete mentorship sessions',
            ],
            'Certificates' => [
                'view mentorship certificates',
                'download mentorship certificates',
                'generate mentorship certificates',
                'revoke mentorship certificates',
            ],
            'Payments' => [
                'view mentorship payments',
                'view own mentorship payments',
            ],
            'Statistics' => [
                'view mentorship statistics',
            ],
        ];
    }

    /**
     * Get role permission matrix for documentation.
     */
    public static function getRolePermissionMatrix(): array
    {
        return [
            'Super Manager' => [
                'description' => 'Full access to all mentorship features',
                'permissions' => 'All permissions',
            ],
            'Manager' => [
                'description' => 'Administrative access with limited destructive actions',
                'permissions' => [
                    'view mentorship categories', 'create mentorship categories', 'edit mentorship categories',
                    'view mentorship programmes', 'create mentorship programmes', 'edit mentorship programmes',
                    'view mentorship applications', 'review mentorship applications', 'approve mentorship applications', 'reject mentorship applications',
                    'view mentorship assignments', 'create mentorship assignments', 'edit mentorship assignments', 'complete mentorship assignments', 'cancel mentorship assignments', 'reassign mentorship assignments',
                    'view mentorship sessions', 'create mentorship sessions', 'edit mentorship sessions', 'cancel mentorship sessions', 'complete mentorship sessions',
                    'view mentorship certificates', 'download mentorship certificates', 'generate mentorship certificates',
                    'view mentorship payments', 'view mentorship statistics',
                ],
            ],
            'Mentor' => [
                'description' => 'Can manage own sessions, view assigned assignments, and see own applications/payments',
                'permissions' => [
                    'view mentorship programmes', 'view mentorship assignments', 'edit mentorship assignments',
                    'view mentorship sessions', 'create mentorship sessions', 'edit mentorship sessions', 'complete mentorship sessions', 'cancel mentorship sessions',
                    'view mentorship certificates', 'download mentorship certificates',
                    'view mentorship statistics',
                    'view own mentorship applications',
                    'view own mentorship payments',
                ],
            ],
            'Mentee' => [
                'description' => 'Can view own assignments, download certificates, and see own applications',
                'permissions' => [
                    'view mentorship programmes', 'view mentorship assignments',
                    'view mentorship sessions',
                    'view mentorship certificates', 'download mentorship certificates',
                    'view own mentorship applications',
                ],
            ],
            'Trainers' => [
                'description' => 'Can view programmes and sessions',
                'permissions' => [
                    'view mentorship programmes', 'view mentorship categories',
                    'view mentorship sessions', 'view mentorship statistics',
                ],
            ],
            'Trainees' => [
                'description' => 'Can view programmes',
                'permissions' => [
                    'view mentorship programmes', 'view mentorship categories',
                ],
            ],
            'Members' => [
                'description' => 'Can view programmes',
                'permissions' => [
                    'view mentorship programmes', 'view mentorship categories',
                ],
            ],
            'Partners' => [
                'description' => 'Can view programmes and sessions',
                'permissions' => [
                    'view mentorship programmes', 'view mentorship categories',
                    'view mentorship sessions',
                ],
            ],
            'Contributor' => [
                'description' => 'Can view programmes',
                'permissions' => [
                    'view mentorship programmes', 'view mentorship categories',
                ],
            ],
            'Health Marketor' => [
                'description' => 'Can view programmes and statistics',
                'permissions' => [
                    'view mentorship programmes', 'view mentorship categories',
                    'view mentorship statistics',
                ],
            ],
        ];
    }
}
