<?php

use App\Filament\Resources\FindingResource;
use App\Models\Finding;
use App\Models\InventoryItem;
use App\Models\Policy;
use App\Models\PolicyVersion;

test('finding detail shows a scope tags diff without Graph calls', function () {
    bindFailHardGraphClient();

    [$user, $tenant] = createUserWithTenant(role: 'manager');

    $baseline = createInventorySyncOperationRun($tenant, [
        'selection_hash' => hash('sha256', 'scope-scope-tags-diff'),
        'status' => 'success',
        'finished_at' => now()->subDays(2),
    ]);

    $current = createInventorySyncOperationRun($tenant, [
        'selection_hash' => $baseline->selection_hash,
        'status' => 'success',
        'finished_at' => now()->subDay(),
    ]);

    $policy = Policy::factory()->for($tenant)->create([
        'external_id' => 'policy-scope-tags-1',
        'policy_type' => 'deviceConfiguration',
        'platform' => 'windows10',
    ]);

    $baselineVersion = PolicyVersion::factory()->for($tenant)->create([
        'policy_id' => $policy->getKey(),
        'version_number' => 17,
        'policy_type' => $policy->policy_type,
        'platform' => $policy->platform,
        'captured_at' => $baseline->finished_at->copy()->subHour(),
        'snapshot' => ['customSettingFoo' => 'Same value'],
        'assignments' => [],
        'scope_tags' => [
            'ids' => ['0'],
            'names' => ['Default'],
        ],
    ]);

    $currentVersion = PolicyVersion::factory()->for($tenant)->create([
        'policy_id' => $policy->getKey(),
        'version_number' => 18,
        'policy_type' => $policy->policy_type,
        'platform' => $policy->platform,
        'captured_at' => $current->finished_at->copy()->subHour(),
        'snapshot' => ['customSettingFoo' => 'Same value'],
        'assignments' => [],
        'scope_tags' => [
            'ids' => ['0', 'a1b2c3'],
            'names' => ['Default', 'Verbund-1'],
        ],
    ]);

    $finding = Finding::factory()->for($tenant)->create([
        'finding_type' => Finding::FINDING_TYPE_DRIFT,
        'scope_key' => (string) $current->selection_hash,
        'baseline_operation_run_id' => $baseline->getKey(),
        'current_operation_run_id' => $current->getKey(),
        'subject_type' => 'scope_tag',
        'subject_external_id' => $policy->external_id,
        'evidence_jsonb' => [
            'change_type' => 'modified',
            'summary' => [
                'kind' => 'policy_scope_tags',
                'changed_fields' => ['scope_tags_hash'],
            ],
            'baseline' => [
                'policy_id' => $policy->external_id,
                'policy_version_id' => $baselineVersion->getKey(),
                'scope_tags_hash' => 'baseline-scope-tags-hash',
            ],
            'current' => [
                'policy_id' => $policy->external_id,
                'policy_version_id' => $currentVersion->getKey(),
                'scope_tags_hash' => 'current-scope-tags-hash',
            ],
        ],
    ]);

    InventoryItem::factory()->for($tenant)->create([
        'external_id' => $finding->subject_external_id,
        'display_name' => 'My Policy Scope Tags',
    ]);

    $this->actingAs($user)
        ->get(FindingResource::getUrl('view', ['record' => $finding], tenant: $tenant))
        ->assertOk()
        ->assertSee('Scope tags diff')
        ->assertSee('1 added')
        ->assertSee('0 removed')
        ->assertSee('Verbund-1')
        ->assertSee('Default');
});
