<?php

declare(strict_types=1);

use App\Support\Governance\PlatformVocabularyGlossary;
use App\Support\OperationCatalog;
use App\Support\OperationRunType;
use App\Services\Providers\ProviderOperationRegistry;

it('keeps touched registry ownership metadata inside the allowed three-way boundary classification', function (): void {
    $classifications = collect(app(PlatformVocabularyGlossary::class)->registries())
        ->map(static fn ($descriptor): string => $descriptor->boundaryClassification)
        ->unique()
        ->values()
        ->all();

    expect($classifications)->toEqualCanonicalizing([
        PlatformVocabularyGlossary::BOUNDARY_CROSS_DOMAIN_GOVERNANCE,
        PlatformVocabularyGlossary::BOUNDARY_PLATFORM_CORE,
        PlatformVocabularyGlossary::BOUNDARY_INTUNE_SPECIFIC,
    ]);
});

it('guards the false-universal policy_type alias behind explicit context-aware vocabulary helpers', function (): void {
    $glossary = app(PlatformVocabularyGlossary::class);

    expect($glossary->term('policy_type')?->boundaryClassification)->toBe(PlatformVocabularyGlossary::BOUNDARY_INTUNE_SPECIFIC)
        ->and($glossary->resolveAlias('policy_type', 'compare')?->termKey)->toBe('governed_subject')
        ->and(OperationCatalog::canonicalCode('baseline_capture'))->toBe('baseline.capture');
});

it('guards canonical operation type writers against raw alias reintroduction', function (): void {
    $legacyAliases = [
        'baseline_capture',
        'baseline_compare',
        'inventory_sync',
        'entra_group_sync',
        'backup_schedule_run',
        'backup_schedule_retention',
        'backup_schedule_purge',
        'directory_role_definitions.sync',
    ];

    $registry = app(ProviderOperationRegistry::class);
    $registryTypes = array_merge(
        array_keys($registry->definitions()),
        collect($registry->definitions())->pluck('operation_type')->all(),
        array_keys($registry->providerBindings()),
        collect($registry->providerBindings())
            ->flatMap(static fn (array $bindings): array => collect($bindings)->pluck('operation_type')->all())
            ->all(),
        OperationRunType::values(),
        array_keys(config('tenantpilot.operations.lifecycle.covered_types', [])),
    );

    expect(array_values(array_intersect($legacyAliases, $registryTypes)))->toBe([]);
});
