<?php

namespace Database\Seeders;

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

/**
 * PaymentConfigurationPermissionSeeder
 * 
 * Creates permissions for the Payment Configuration module and assigns them to appropriate roles.
 * This seeder follows the SSHIA Laravel System permission patterns.
 * 
 * Permission Naming Convention: {action} {resource}
 * - view payment configuration
 * - create payment configuration
 * - edit payment configuration
 * - delete payment configuration
 * - test payment connection
 * - view payment logs
 * 
 * @package PaymentConfigurationModule
 * @author SSHIA Development Team
 */
class PaymentConfigurationPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Define permissions for Payment Configuration module
        $permissions = [
            [
                'name' => 'view payment configuration',
                'description' => 'View payment configuration dashboard and status',
            ],
            [
                'name' => 'create payment configuration',
                'description' => 'Create new payment configuration',
            ],
            [
                'name' => 'edit payment configuration',
                'description' => 'Edit existing payment configuration',
            ],
            [
                'name' => 'delete payment configuration',
                'description' => 'Delete payment configuration',
            ],
            [
                'name' => 'test payment connection',
                'description' => 'Test API connections for Collection and Disbursement',
            ],
            [
                'name' => 'view payment logs',
                'description' => 'View API interaction logs',
            ],
        ];

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

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

        // Assign permissions to Super Manager role
        $superManager = Role::where('name', 'Super Manager')->first();

        if ($superManager) {
            $superManager->givePermissionTo(collect($permissions)->pluck('name'));
            $this->command->info('Payment Configuration permissions assigned to Super Manager role.');
        } else {
            $this->command->warn('Super Manager role not found. Please run RolePermissionSeeder first.');
        }

        // Optional: Assign limited permissions to Manager role (if needed)
        // By design, only Super Manager has access to payment configuration
        // Uncomment the following if you want Manager to have read-only access:
        
        // $manager = Role::where('name', 'Manager')->first();
        // if ($manager) {
        //     $manager->givePermissionTo([
        //         'view payment configuration',
        //         'view payment logs',
        //     ]);
        // }

        // Summary output
        $this->command->info('=== Payment Configuration Permission Summary ===');
        $this->command->info('Permissions created:');
        foreach ($permissions as $permission) {
            $this->command->info("  - {$permission['name']}: {$permission['description']}");
        }
        $this->command->info('');
        $this->command->info('Role assignments:');
        $this->command->info('  - Super Manager: All permissions');
        $this->command->info('  - Other roles: No access (by design)');

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