<?php

namespace Database\Seeders;

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

/**
 * SmsPermissionSeeder
 * 
 * Creates and assigns permissions for the SMS Configuration module.
 * This seeder creates the "manage sms configuration" permission and
 * assigns it to the Super Manager role.
 * 
 * Usage:
 * php artisan db:seed --class=SmsPermissionSeeder
 * 
 * Or include in DatabaseSeeder.php:
 * $this->call(SmsPermissionSeeder::class);
 * 
 * @package Database\Seeders
 */
class SmsPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Define SMS permissions
        $smsPermissions = [
            [
                'name' => 'manage sms configuration',
                'description' => 'Can manage SMS API credentials and settings',
            ],
            [
                'name' => 'view sms logs',
                'description' => 'Can view SMS log history',
            ],
            [
                'name' => 'send sms',
                'description' => 'Can send SMS messages using the SMS service',
            ],
            [
                'name' => 'test sms connection',
                'description' => 'Can test SMS connectivity',
            ],
        ];

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

        $this->command->info('SMS permissions created successfully!');

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

    /**
     * Assign permissions to appropriate roles.
     *
     * @return void
     */
    protected function assignPermissionsToRoles(): void
    {
        // Get Super Manager role
        $superManager = Role::where('name', 'Super Manager')->first();

        if ($superManager) {
            // Super Manager gets all SMS permissions
            $superManager->givePermissionTo([
                'manage sms configuration',
                'view sms logs',
                'send sms',
                'test sms connection',
            ]);

            $this->command->info('SMS permissions assigned to Super Manager role.');
        } else {
            $this->command->warn('Super Manager role not found. Please run the main RolePermissionSeeder first.');
        }

        // Get Manager role
        $manager = Role::where('name', 'Manager')->first();

        if ($manager) {
            // Manager can view logs and send SMS, but not configure
            $manager->givePermissionTo([
                'view sms logs',
                'send sms',
            ]);

            $this->command->info('SMS permissions assigned to Manager role.');
        }

        // Note: Other roles typically don't need SMS permissions
        // SMS is usually handled by administrators only

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

    /**
     * Get the list of permissions created by this seeder.
     * Useful for documentation and verification.
     *
     * @return array
     */
    public static function getPermissionsList(): array
    {
        return [
            'manage sms configuration' => [
                'description' => 'Full control over SMS configuration including creating, editing, and deleting credentials',
                'roles' => ['Super Manager'],
            ],
            'view sms logs' => [
                'description' => 'Can view the SMS log history and statistics',
                'roles' => ['Super Manager', 'Manager'],
            ],
            'send sms' => [
                'description' => 'Can use the SMSService to send SMS messages',
                'roles' => ['Super Manager', 'Manager'],
            ],
            'test sms connection' => [
                'description' => 'Can send test SMS messages to verify configuration',
                'roles' => ['Super Manager'],
            ],
        ];
    }
}
