<?php

use App\Services\Auth\RoleCapabilityMap;
use App\Services\Auth\WorkspaceRoleCapabilityMap;
use App\Support\Auth\Capabilities;
use App\Support\Auth\WorkspaceRole;
use App\Support\TenantRole;

it('uses only canonical capabilities in workspace role grants', function (): void {
    foreach (WorkspaceRole::cases() as $role) {
        $capabilities = WorkspaceRoleCapabilityMap::getCapabilities($role);

        expect($capabilities)
            ->toBe(array_values(array_unique($capabilities)));

        foreach ($capabilities as $capability) {
            expect(Capabilities::isKnown($capability))->toBeTrue();
        }
    }
});

it('keeps workspace and tenant membership management owner only', function (): void {
    expect(WorkspaceRoleCapabilityMap::rolesWithCapability(Capabilities::WORKSPACE_MEMBERSHIP_MANAGE))
        ->toBe([WorkspaceRole::Owner->value]);

    expect(WorkspaceRoleCapabilityMap::hasCapability(WorkspaceRole::Owner, Capabilities::WORKSPACE_MEMBERSHIP_MANAGE))->toBeTrue();
    expect(WorkspaceRoleCapabilityMap::hasCapability(WorkspaceRole::Owner, Capabilities::TENANT_MEMBERSHIP_MANAGE))->toBeTrue();

    foreach ([WorkspaceRole::Manager, WorkspaceRole::Operator, WorkspaceRole::Readonly] as $role) {
        expect(WorkspaceRoleCapabilityMap::hasCapability($role, Capabilities::WORKSPACE_MEMBERSHIP_MANAGE))->toBeFalse();
        expect(WorkspaceRoleCapabilityMap::hasCapability($role, Capabilities::TENANT_MEMBERSHIP_MANAGE))->toBeFalse();
    }

    expect(RoleCapabilityMap::hasCapability(TenantRole::Manager, Capabilities::TENANT_MEMBERSHIP_MANAGE))->toBeFalse();
});
