<?php

declare(strict_types=1);

use App\Models\StoredReport;

it('derives stored report descriptor and keeps report_type as provider detail', function (): void {
    [$user, $tenant] = createUserWithTenant(ensureDefaultMicrosoftProviderConnection: true);
    $connection = $tenant->providerConnections()->where('provider', 'microsoft')->where('is_default', true)->firstOrFail();

    $report = StoredReport::factory()->entraAdminRoles([
        'provider_key' => 'microsoft',
        'provider_connection_id' => (int) $connection->getKey(),
    ])->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    $descriptor = $report->artifactSourceDescriptor()->toArray();

    expect($descriptor)->toMatchArray([
        'workspace_id' => (int) $tenant->workspace_id,
        'tenant_id' => (int) $tenant->getKey(),
        'managed_environment_id' => (int) $tenant->getKey(),
        'source_family' => 'stored_report',
        'source_kind' => 'stored_report',
        'provider_key' => 'microsoft',
        'provider_connection_id' => (int) $connection->getKey(),
        'source_target_kind' => 'managed_environment',
        'detector_key' => 'entra_admin_roles.privileged_role_assignment',
        'control_key' => 'privileged_access_governance',
        'package_run_id' => null,
    ])
        ->and($descriptor)->not->toHaveKey('report_type')
        ->and($report->artifactProviderDetail()->toArray())->toMatchArray([
            'legacy_report_type' => StoredReport::REPORT_TYPE_ENTRA_ADMIN_ROLES,
            'provider_object_type' => StoredReport::REPORT_TYPE_ENTRA_ADMIN_ROLES,
        ]);

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