<?php

declare(strict_types=1);

use App\Services\Providers\ProviderOperationRegistry;
use App\Support\OperationCatalog;

it('declares provider operation definitions with canonical operation types only', function (): void {
    $registry = app(ProviderOperationRegistry::class);

    foreach ($registry->definitions() as $key => $definition) {
        $operationType = (string) ($definition['operation_type'] ?? '');
        $resolution = OperationCatalog::resolve($operationType);

        expect($key)->toBe($operationType)
            ->and($resolution->aliasStatus)->toBe('canonical')
            ->and($resolution->canonical->canonicalCode)->toBe($operationType);
    }
});

it('declares provider bindings with canonical operation types only', function (): void {
    $registry = app(ProviderOperationRegistry::class);

    foreach ($registry->providerBindings() as $key => $bindings) {
        expect(OperationCatalog::resolve($key)->aliasStatus)->toBe('canonical');

        foreach ($bindings as $binding) {
            $operationType = (string) ($binding['operation_type'] ?? '');
            $resolution = OperationCatalog::resolve($operationType);

            expect($operationType)->toBe($key)
                ->and($resolution->aliasStatus)->toBe('canonical')
                ->and($resolution->canonical->canonicalCode)->toBe($operationType);
        }
    }
});

it('rejects legacy aliases as provider registry keys', function (): void {
    $registry = app(ProviderOperationRegistry::class);

    expect($registry->isAllowed('inventory_sync'))->toBeFalse()
        ->and($registry->isAllowed('entra_group_sync'))->toBeFalse()
        ->and($registry->isAllowed('directory_role_definitions.sync'))->toBeFalse()
        ->and($registry->isAllowed('inventory.sync'))->toBeTrue()
        ->and($registry->isAllowed('directory.groups.sync'))->toBeTrue()
        ->and($registry->isAllowed('directory.role_definitions.sync'))->toBeTrue();
});
