<?php

declare(strict_types=1);

use App\Filament\Resources\BaselineSnapshotResource;
use App\Models\BaselineProfile;
use App\Models\BaselineSnapshot;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('shows snapshot fidelity counts and evidence gap totals on list and view pages', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'readonly');

    $profile = BaselineProfile::factory()->active()->create([
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    $complete = BaselineSnapshot::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'baseline_profile_id' => (int) $profile->getKey(),
        'captured_at' => now()->subHour(),
        'summary_jsonb' => [
            'total_items' => 5,
            'fidelity_counts' => ['content' => 5, 'meta' => 0],
            'gaps' => ['count' => 0, 'by_reason' => []],
        ],
    ]);

    $withGaps = BaselineSnapshot::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'baseline_profile_id' => (int) $profile->getKey(),
        'captured_at' => now()->subMinutes(10),
        'summary_jsonb' => [
            'total_items' => 5,
            'fidelity_counts' => ['content' => 3, 'meta' => 2],
            'gaps' => ['count' => 2, 'by_reason' => ['missing_evidence' => 2]],
        ],
    ]);

    $this->actingAs($user)
        ->get(BaselineSnapshotResource::getUrl(panel: 'admin'))
        ->assertOk()
        ->assertSee('Detailed evidence 5, Metadata only 0')
        ->assertSee('Detailed evidence 3, Metadata only 2');

    $this->actingAs($user)
        ->get(BaselineSnapshotResource::getUrl('view', ['record' => $withGaps], panel: 'admin'))
        ->assertOk()
        ->assertSee('Coverage summary')
        ->assertSee('Detailed evidence 3, Metadata only 2')
        ->assertSee('Evidence gaps')
        ->assertSee('2');

    $this->actingAs($user)
        ->get(BaselineSnapshotResource::getUrl('view', ['record' => $complete], panel: 'admin'))
        ->assertOk()
        ->assertSee('Coverage summary')
        ->assertSee('Detailed evidence 5, Metadata only 0')
        ->assertSee('Evidence gaps')
        ->assertSee('0');
});
