<?php

namespace Tests\Feature;

use App\Models\Role;
use App\Services\Permission\PermissionRegistry;
use Database\Seeders\RoleAndPermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PermissionRegistryTest extends TestCase
{
    use RefreshDatabase;
    public function test_seeder_syncs_module_permissions(): void
    {
        $this->seed(RoleAndPermissionSeeder::class);

        $registry = app(PermissionRegistry::class);
        $expected = array_keys($registry->flattenConfiguredPermissions());

        $this->assertDatabaseCount('permissions', count($expected));

        $superAdmin = Role::findByName(config('permissions.super_admin_role'), 'web');
        $this->assertTrue($superAdmin->hasAllPermissions($expected));
    }

    public function test_matrix_groups_permissions_by_module(): void
    {
        $this->seed(RoleAndPermissionSeeder::class);

        $role = Role::findByName('Super Admin', 'web');
        $matrix = app(PermissionRegistry::class)->matrixForRole($role);

        $this->assertArrayHasKey('Platform', $matrix);
        $this->assertNotEmpty($matrix['Platform']);
        $this->assertTrue($matrix['Platform'][0]['granted']['all']);
    }
}
