<?php

declare(strict_types=1);

use App\Models\InventoryItem;

it('derives inventory type split and shared artifact source descriptor', function (): void {
    [$user, $tenant] = createUserWithTenant(ensureDefaultMicrosoftProviderConnection: true);
    $connection = $tenant->providerConnections()->where('provider', 'microsoft')->where('is_default', true)->firstOrFail();

    $item = InventoryItem::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'policy_type' => 'deviceCompliancePolicy',
        'external_id' => 'compliance-policy-1',
        'display_name' => 'Compliance Policy One',
        'meta_jsonb' => [
            'provider_key' => 'microsoft',
            'provider_connection_id' => (int) $connection->getKey(),
        ],
    ]);

    expect($item->inventoryTypeDescriptor())->toMatchArray([
        'canonical_type' => 'endpoint_compliance_policy',
        'provider_object_type' => 'deviceCompliancePolicy',
        'provider_display_type' => 'Device Compliance',
        'legacy_policy_type' => 'deviceCompliancePolicy',
    ])
        ->and($item->artifactSourceDescriptor()->toArray())->toMatchArray([
            'workspace_id' => (int) $tenant->workspace_id,
            'tenant_id' => (int) $tenant->getKey(),
            'managed_environment_id' => (int) $tenant->getKey(),
            'source_family' => 'inventory',
            'source_kind' => 'inventory_projection',
            'provider_key' => 'microsoft',
            'provider_connection_id' => (int) $connection->getKey(),
            'source_target_kind' => 'governed_subject',
            'source_target_identifier' => 'compliance-policy-1',
            'detector_key' => 'inventory.deviceCompliancePolicy',
            'control_key' => 'endpoint_hardening_compliance',
            'package_run_id' => null,
        ]);

    $this->actingAs($user);
});
