<?php

use App\Enums\PermissionAction;
use App\Enums\PermissionModule;
use App\Enums\PermissionSlug;

it('uses PermissionSlug as the single permission catalog', function () {
    expect(PermissionSlug::values())->toBe([
        PermissionSlug::UsersView->value,
        PermissionSlug::UsersCreate->value,
        PermissionSlug::UsersUpdate->value,
        PermissionSlug::UsersDelete->value,
        PermissionSlug::CustomersView->value,
        PermissionSlug::CustomersCreate->value,
        PermissionSlug::CustomersUpdate->value,
        PermissionSlug::CustomersDelete->value,
        PermissionSlug::AgentsView->value,
        PermissionSlug::AgentsCreate->value,
        PermissionSlug::AgentsUpdate->value,
        PermissionSlug::AgentsDelete->value,
        PermissionSlug::UnitsView->value,
        PermissionSlug::UnitsCreate->value,
        PermissionSlug::UnitsUpdate->value,
        PermissionSlug::UnitsDelete->value,
        PermissionSlug::BookingsView->value,
        PermissionSlug::BookingsCreate->value,
        PermissionSlug::BookingsUpdate->value,
        PermissionSlug::BookingsCancel->value,
        PermissionSlug::ReportsView->value,
    ]);
});

it('derives module and action from each permission slug', function () {
    foreach (PermissionSlug::cases() as $permission) {
        expect($permission->module())->toBeInstanceOf(PermissionModule::class)
            ->and($permission->action())->toBeInstanceOf(PermissionAction::class)
            ->and($permission->value)->toBe(
                $permission->module()->value.'.'.$permission->action()->value,
            );
    }
});

it('groups permissions by module without omitting cases', function () {
    expect(array_keys(PermissionSlug::grouped()))->toBe(
        array_column(PermissionModule::cases(), 'value'),
    )->and(array_merge(...array_values(PermissionSlug::grouped())))->toBe(
        PermissionSlug::cases(),
    );
});
