<?php

namespace Database\Seeders;

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

/**
 * GoogleMeetPermissionSeeder
 * 
 * Creates permissions for the Google Meet configuration module.
 * All permissions are assigned exclusively to the Super Manager role.
 * 
 * Permission naming follows the SSHIA system convention: {action} {resource}
 * 
 * @see SECTION_4_ROLE_PERMISSION_SYSTEM.md for permission system documentation
 */
class GoogleMeetPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Define permission types for Google Meet configuration
        $permissions = [
            'view google meet configuration',
            'create google meet configuration',
            'edit google meet configuration',
            'delete google meet configuration',
            'test google meet connection',
            'view google meet logs',
            'manage google meet settings',
            'create google meet meetings',
        ];

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

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

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

        if (!$superManager) {
            $this->command->warn('Super Manager role not found. Creating it...');
            $superManager = Role::create([
                'name' => 'Super Manager',
                'guard_name' => 'web',
            ]);
        }

        // Assign all Google Meet permissions to Super Manager
        $superManager->givePermissionTo($permissions);

        $this->command->info('Google Meet permissions assigned to Super Manager role.');

        // Display assigned permissions
        $this->command->table(
            ['Permission', 'Assigned To'],
            collect($permissions)->map(fn($perm) => [$perm, 'Super Manager'])->toArray()
        );

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

    /**
     * Get all Google Meet permission names.
     * Useful for checking permissions in controllers or views.
     *
     * @return array
     */
    public static function getPermissionNames(): array
    {
        return [
            'view google meet configuration',
            'create google meet configuration',
            'edit google meet configuration',
            'delete google meet configuration',
            'test google meet connection',
            'view google meet logs',
            'manage google meet settings',
            'create google meet meetings',
        ];
    }

    /**
     * Get permissions grouped by category.
     *
     * @return array
     */
    public static function getGroupedPermissions(): array
    {
        return [
            'Configuration Management' => [
                'view google meet configuration',
                'create google meet configuration',
                'edit google meet configuration',
                'delete google meet configuration',
            ],
            'Testing & Debugging' => [
                'test google meet connection',
                'view google meet logs',
            ],
            'Operations' => [
                'manage google meet settings',
                'create google meet meetings',
            ],
        ];
    }
}
