<?php

declare(strict_types=1);

use App\Support\Baselines\ResolutionPath;
use App\Support\Baselines\SubjectClass;
use App\Support\Inventory\InventoryPolicyTypeMeta;

it('derives baseline subject resolution defaults for supported policy types', function (): void {
    $contract = InventoryPolicyTypeMeta::baselineSupportContract('deviceConfiguration');

    expect($contract['config_supported'])->toBeTrue()
        ->and($contract['runtime_valid'])->toBeTrue()
        ->and($contract['subject_class'])->toBe(SubjectClass::PolicyBacked->value)
        ->and($contract['resolution_path'])->toBe(ResolutionPath::Policy->value)
        ->and($contract['compare_capability'])->toBe('supported')
        ->and($contract['capture_capability'])->toBe('supported')
        ->and($contract['source_model_expected'])->toBe('policy');
});

it('derives limited inventory-backed foundation support from canonical metadata', function (): void {
    $contract = InventoryPolicyTypeMeta::baselineSupportContract('roleScopeTag');

    expect($contract['config_supported'])->toBeTrue()
        ->and($contract['runtime_valid'])->toBeTrue()
        ->and($contract['subject_class'])->toBe(SubjectClass::FoundationBacked->value)
        ->and($contract['resolution_path'])->toBe(ResolutionPath::FoundationInventory->value)
        ->and($contract['compare_capability'])->toBe('limited')
        ->and($contract['capture_capability'])->toBe('limited')
        ->and($contract['source_model_expected'])->toBe('inventory');
});

it('derives supported foundation policy resolution for intune role definitions', function (): void {
    $contract = InventoryPolicyTypeMeta::baselineSupportContract('intuneRoleDefinition');

    expect($contract['config_supported'])->toBeTrue()
        ->and($contract['runtime_valid'])->toBeTrue()
        ->and($contract['subject_class'])->toBe(SubjectClass::FoundationBacked->value)
        ->and($contract['resolution_path'])->toBe(ResolutionPath::FoundationPolicy->value)
        ->and($contract['compare_capability'])->toBe('supported')
        ->and($contract['capture_capability'])->toBe('supported')
        ->and($contract['source_model_expected'])->toBe('policy');
});

it('marks unsupported and malformed contracts deterministically', function (): void {
    config()->set('tenantpilot.foundation_types', [
        [
            'type' => 'intuneRoleAssignment',
            'label' => 'Intune RBAC Role Assignment',
            'baseline_compare' => [
                'supported' => false,
                'identity_strategy' => 'external_id',
            ],
        ],
        [
            'type' => 'brokenFoundation',
            'label' => 'Broken Foundation',
            'baseline_compare' => [
                'supported' => true,
                'resolution' => [
                    'resolution_path' => 'broken',
                ],
            ],
        ],
    ]);

    $unsupported = InventoryPolicyTypeMeta::baselineSupportContract('intuneRoleAssignment');
    $invalid = InventoryPolicyTypeMeta::baselineSupportContract('brokenFoundation');

    expect($unsupported['config_supported'])->toBeFalse()
        ->and($unsupported['compare_capability'])->toBe('unsupported')
        ->and($unsupported['capture_capability'])->toBe('unsupported');

    expect($invalid['config_supported'])->toBeTrue()
        ->and($invalid['runtime_valid'])->toBeFalse()
        ->and($invalid['compare_capability'])->toBe('unsupported')
        ->and($invalid['capture_capability'])->toBe('unsupported');
});
