<?php

namespace Database\Seeders;

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

/**
 * SelfPacedPermissionSeeder
 *
 * Seeds all permissions for the Self-Paced Learning Portal and assigns them
 * to the appropriate Spatie roles.
 *
 * Run: php artisan db:seed --class=SelfPacedPermissionSeeder
 */
class SelfPacedPermissionSeeder extends Seeder
{
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

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

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

        $this->command->info('Self-Paced Learning Portal permissions seeded successfully!');

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

    protected function getPermissions(): array
    {
        return [
            // Course management
            ['name' => 'view self-paced courses'],
            ['name' => 'create self-paced courses'],
            ['name' => 'edit self-paced courses'],
            ['name' => 'delete self-paced courses'],

            // Topics, sub-topics, media, downloads
            ['name' => 'manage self-paced topics'],
            ['name' => 'manage self-paced media'],

            // Exam management
            ['name' => 'manage self-paced exams'],

            // Enrollment management
            ['name' => 'view self-paced enrollments'],
            ['name' => 'manage self-paced enrollments'],

            // Results
            ['name' => 'view self-paced results'],

            // Certificates
            ['name' => 'manage self-paced certificates'],

            // Learner portal access (Trainees)
            ['name' => 'access self-paced learner portal'],

            // Payment & Fee management (New)
            ['name' => 'manage self-paced paystack'],
            ['name' => 'manage self-paced exam fees'],
            ['name' => 'verify self-paced payments'],

            // Book management (New)
            ['name' => 'manage self-paced book'],

            // System / fee settings (Super Manager + Manager only)
            ['name' => 'manage self-paced settings'],
        ];
    }

    protected function assignPermissionsToRoles(): void
    {
        // ------------------------------------------------------------------
        // Super Manager — full access to everything
        // ------------------------------------------------------------------
        $superManager = Role::findByName('Super Manager');
        if ($superManager) {
            $superManager->givePermissionTo(Permission::whereIn('name', [
                'view self-paced courses',
                'create self-paced courses',
                'edit self-paced courses',
                'delete self-paced courses',
                'manage self-paced topics',
                'manage self-paced media',
                'manage self-paced exams',
                'view self-paced enrollments',
                'manage self-paced enrollments',
                'view self-paced results',
                'manage self-paced certificates',
                'access self-paced learner portal',
                'manage self-paced settings',
                'manage self-paced paystack',
                'manage self-paced exam fees',
                'manage self-paced book',
                'verify self-paced payments',
            ])->get());
        }

        // ------------------------------------------------------------------
        // Manager — full access except system settings
        // ------------------------------------------------------------------
        $manager = Role::findByName('Manager');
        if ($manager) {
            $manager->givePermissionTo([
                'view self-paced courses',
                'create self-paced courses',
                'edit self-paced courses',
                'delete self-paced courses',
                'manage self-paced topics',
                'manage self-paced media',
                'manage self-paced exams',
                'view self-paced enrollments',
                'manage self-paced enrollments',
                'view self-paced results',
                'manage self-paced certificates',
                'access self-paced learner portal',
                'manage self-paced exam fees',
                'manage self-paced book',
                'verify self-paced payments',
            ]);
        }

        // ------------------------------------------------------------------
        // Trainers — can manage courses, topics, media, and exams;
        //            cannot manage fees/settings or enrollments/certificates
        // ------------------------------------------------------------------
        $trainers = Role::findByName('Trainers');
        if ($trainers) {
            $trainers->givePermissionTo([
                'view self-paced courses',
                'create self-paced courses',
                'edit self-paced courses',
                'manage self-paced topics',
                'manage self-paced media',
                'manage self-paced exams',
                'view self-paced enrollments',
                'view self-paced results',
            ]);
        }

        // ------------------------------------------------------------------
        // Trainees — learner portal access only
        // ------------------------------------------------------------------
        $trainees = Role::findByName('Trainees');
        if ($trainees) {
            $trainees->givePermissionTo([
                'access self-paced learner portal',
            ]);
        }
    }
}
