<?php

namespace Database\Seeders;

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

/**
 * EmailConfigurationPermissionSeeder
 * 
 * Seeds permissions for the Email Configuration module.
 * Creates and assigns permissions to appropriate roles.
 * 
 * Permission Types:
 * - manage email configuration: Full access to all email configuration features
 * 
 * Role Assignments:
 * - Super Manager: All permissions
 * - Manager: View only (if needed, customize as required)
 * 
 * @package Database\Seeders
 */
class EmailConfigurationPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Define permissions for email configuration
        $permissions = [
            'manage email configuration' => 'Full access to manage email configuration settings',
            'view email configuration' => 'View email configuration status',
            'create email configuration' => 'Create new email configuration',
            'edit email configuration' => 'Edit existing email configuration',
            'delete email configuration' => 'Delete email configuration',
            'test email configuration' => 'Test SMTP/IMAP connections',
            'view email logs' => 'View email operation logs',
            'export email logs' => 'Export email logs',
        ];

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

        // Assign permissions to roles

        // Super Manager gets all permissions
        $superManager = Role::where('name', 'Super Manager')->first();
        if ($superManager) {
            $superManager->givePermissionTo(array_keys($permissions));
        }

        // Manager gets view permissions only (customize as needed)
        $manager = Role::where('name', 'Manager')->first();
        if ($manager) {
            $manager->givePermissionTo([
                'view email configuration',
                'view email logs',
            ]);
        }

        // Log seeding results
        $this->command->info('Email Configuration permissions seeded successfully!');
        $this->command->info('Created permissions:');
        foreach ($permissions as $name => $description) {
            $this->command->line("  - {$name}: {$description}");
        }

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

    /**
     * Get the list of permissions for this module.
     * Useful for permission management UI.
     *
     * @return array
     */
    public static function getPermissionList(): array
    {
        return [
            'manage email configuration' => [
                'description' => 'Full access to manage email configuration settings',
                'group' => 'email',
                'critical' => true,
            ],
            'view email configuration' => [
                'description' => 'View email configuration status',
                'group' => 'email',
                'critical' => false,
            ],
            'create email configuration' => [
                'description' => 'Create new email configuration',
                'group' => 'email',
                'critical' => false,
            ],
            'edit email configuration' => [
                'description' => 'Edit existing email configuration',
                'group' => 'email',
                'critical' => false,
            ],
            'delete email configuration' => [
                'description' => 'Delete email configuration',
                'group' => 'email',
                'critical' => true,
            ],
            'test email configuration' => [
                'description' => 'Test SMTP/IMAP connections',
                'group' => 'email',
                'critical' => false,
            ],
            'view email logs' => [
                'description' => 'View email operation logs',
                'group' => 'email',
                'critical' => false,
            ],
            'export email logs' => [
                'description' => 'Export email logs',
                'group' => 'email',
                'critical' => false,
            ],
        ];
    }

    /**
     * Get the default role assignments for this module.
     *
     * @return array
     */
    public static function getDefaultRoleAssignments(): array
    {
        return [
            'Super Manager' => [
                'manage email configuration',
                'view email configuration',
                'create email configuration',
                'edit email configuration',
                'delete email configuration',
                'test email configuration',
                'view email logs',
                'export email logs',
            ],
            'Manager' => [
                'view email configuration',
                'view email logs',
            ],
        ];
    }
}
