<?php

declare(strict_types=1);

use App\Support\Inventory\InventoryPolicyTypeMeta;

it('keeps display-name foundation support truthful as limited inventory-backed capability', function (): void {
    $contract = InventoryPolicyTypeMeta::baselineSupportContract('roleScopeTag');

    expect($contract['config_supported'])->toBeTrue()
        ->and($contract['runtime_valid'])->toBeTrue()
        ->and($contract['subject_class'])->toBe('foundation_backed')
        ->and($contract['resolution_path'])->toBe('foundation_inventory')
        ->and($contract['compare_capability'])->toBe('limited')
        ->and($contract['capture_capability'])->toBe('limited')
        ->and($contract['source_model_expected'])->toBe('inventory');
});

it('treats unknown baseline types as derived and excluded from support promises', function (): void {
    $contract = InventoryPolicyTypeMeta::baselineSupportContract('unknownFoundation');

    expect($contract['config_supported'])->toBeFalse()
        ->and($contract['runtime_valid'])->toBeTrue()
        ->and($contract['subject_class'])->toBe('derived')
        ->and($contract['resolution_path'])->toBe('derived')
        ->and($contract['compare_capability'])->toBe('unsupported')
        ->and($contract['capture_capability'])->toBe('unsupported')
        ->and($contract['source_model_expected'])->toBe('derived');
});

it('downgrades malformed baseline support declarations before they can overpromise runtime capability', function (): void {
    $foundationTypes = is_array(config('tenantpilot.foundation_types')) ? config('tenantpilot.foundation_types') : [];
    $foundationTypes[] = [
        'type' => 'brokenFoundation',
        'label' => 'Broken Foundation',
        'baseline_compare' => [
            'supported' => true,
            'identity_strategy' => 'external_id',
            'resolution' => [
                'subject_class' => 'foundation_backed',
                'resolution_path' => 'foundation_policy',
                'compare_capability' => 'supported',
                'capture_capability' => 'supported',
                'source_model_expected' => 'inventory',
            ],
        ],
    ];

    config()->set('tenantpilot.foundation_types', $foundationTypes);

    $contract = InventoryPolicyTypeMeta::baselineSupportContract('brokenFoundation');

    expect($contract['config_supported'])->toBeTrue()
        ->and($contract['runtime_valid'])->toBeFalse()
        ->and($contract['subject_class'])->toBe('foundation_backed')
        ->and($contract['resolution_path'])->toBe('foundation_policy')
        ->and($contract['compare_capability'])->toBe('unsupported')
        ->and($contract['capture_capability'])->toBe('unsupported')
        ->and($contract['source_model_expected'])->toBe('inventory');
});
