<?php

declare(strict_types=1);

use App\Support\Baselines\BaselineCompareReasonCode;
use App\Support\Governance\PlatformVocabularyGlossary;
use App\Support\Operations\ExecutionDenialReasonCode;
use App\Support\Providers\ProviderReasonCodes;
use App\Support\RbacReason;
use App\Support\ReasonTranslation\ReasonTranslator;
use App\Support\Tenants\TenantOperabilityReasonCode;

it('keeps adopted operator labels free from raw internal reason codes', function (): void {
    $translator = app(ReasonTranslator::class);
    $reasonCodes = [
        ExecutionDenialReasonCode::MissingCapability->value,
        ProviderReasonCodes::ProviderConsentMissing,
        TenantOperabilityReasonCode::RememberedContextStale->value,
        RbacReason::ManualAssignmentRequired->value,
    ];

    foreach ($reasonCodes as $reasonCode) {
        $envelope = $translator->translate($reasonCode);

        expect($envelope)->not->toBeNull()
            ->and($envelope?->operatorLabel)->not->toBe($reasonCode)
            ->and($envelope?->operatorLabel)->not->toContain('_');
    }
});

it('uses the canonical operator vocabulary for adopted reason families', function (): void {
    expect(ExecutionDenialReasonCode::MissingCapability->toReasonResolutionEnvelope()->operatorLabel)->toBe('Permission required')
        ->and(app(ReasonTranslator::class)->translate(ProviderReasonCodes::ProviderPermissionDenied)?->operatorLabel)->toBe('Permission denied')
        ->and(TenantOperabilityReasonCode::TenantAlreadyArchived->toReasonResolutionEnvelope()->guidanceText())->toBe('No action needed.')
        ->and(RbacReason::ManualAssignmentRequired->toReasonResolutionEnvelope()->operatorLabel)->toBe('Manual role assignment required');
});

it('keeps primary-surface reason ownership inside the allowed three-way boundary classification', function (): void {
    $translator = app(ReasonTranslator::class);
    $classifications = collect([
        $translator->boundaryClassification(ExecutionDenialReasonCode::MissingCapability->value, ReasonTranslator::EXECUTION_DENIAL_ARTIFACT),
        $translator->boundaryClassification(ProviderReasonCodes::ProviderConsentMissing),
        $translator->boundaryClassification(TenantOperabilityReasonCode::RememberedContextStale->value, ReasonTranslator::TENANT_OPERABILITY_ARTIFACT),
        $translator->boundaryClassification(RbacReason::ManualAssignmentRequired->value, ReasonTranslator::RBAC_ARTIFACT),
        $translator->boundaryClassification(BaselineCompareReasonCode::CoverageUnproven->value, ReasonTranslator::GOVERNANCE_ARTIFACT_TRUTH_ARTIFACT),
    ])
        ->filter()
        ->unique()
        ->values()
        ->all();

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