<?php

declare(strict_types=1);

use App\Filament\Pages\Monitoring\EvidenceOverview;
use App\Filament\Resources\EvidenceSnapshotResource;
use App\Models\EvidenceSnapshot;
use App\Models\ManagedEnvironment;
use App\Support\Evidence\EvidenceCompletenessState;
use App\Support\Evidence\EvidenceSnapshotStatus;
use App\Support\Workspaces\WorkspaceContext;
use Livewire\Livewire;

it('Spec314 evidence overview clean workspace entry is workspace wide', function (): void {
    $environmentA = ManagedEnvironment::factory()->active()->create(['name' => 'Evidence Environment A']);
    [$user, $environmentA] = createUserWithTenant(tenant: $environmentA, role: 'owner');

    $environmentB = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $environmentA->workspace_id,
        'name' => 'Evidence Environment B',
    ]);
    createUserWithTenant(tenant: $environmentB, user: $user, role: 'owner');

    $snapshotA = spec314EvidenceSnapshot($environmentA, EvidenceCompletenessState::Complete->value);
    $snapshotB = spec314EvidenceSnapshot($environmentB, EvidenceCompletenessState::Partial->value);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $environmentA->workspace_id])
        ->get(route('admin.evidence.overview'))
        ->assertOk()
        ->assertSee(EvidenceSnapshotResource::getUrl('view', ['record' => $snapshotA], tenant: $environmentA, panel: 'admin'), false)
        ->assertSee(EvidenceSnapshotResource::getUrl('view', ['record' => $snapshotB], tenant: $environmentB, panel: 'admin'), false);
});

it('Spec314 evidence overview ignores stale persisted environment filters on clean entry', function (): void {
    $environmentA = ManagedEnvironment::factory()->active()->create();
    [$user, $environmentA] = createUserWithTenant(tenant: $environmentA, role: 'owner');

    $environmentB = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $environmentA->workspace_id,
    ]);
    createUserWithTenant(tenant: $environmentB, user: $user, role: 'owner');

    $snapshotA = spec314EvidenceSnapshot($environmentA, EvidenceCompletenessState::Complete->value);
    $snapshotB = spec314EvidenceSnapshot($environmentB, EvidenceCompletenessState::Partial->value);

    $this->actingAs($user);
    setAdminPanelContext();
    session()->put(WorkspaceContext::SESSION_KEY, (int) $environmentA->workspace_id);

    $component = Livewire::actingAs($user)->test(EvidenceOverview::class);
    $filtersSessionKey = $component->instance()->getTableFiltersSessionKey();

    session()->put($filtersSessionKey, [
        'managed_environment_id' => ['value' => (string) $environmentA->getKey()],
    ]);

    $this->get(route('admin.evidence.overview'))
        ->assertOk()
        ->assertSee(EvidenceSnapshotResource::getUrl('view', ['record' => $snapshotA], tenant: $environmentA, panel: 'admin'), false)
        ->assertSee(EvidenceSnapshotResource::getUrl('view', ['record' => $snapshotB], tenant: $environmentB, panel: 'admin'), false);

    expect(data_get(session()->get($filtersSessionKey, []), 'managed_environment_id.value'))->toBeNull();
});

function spec314EvidenceSnapshot(ManagedEnvironment $environment, string $completenessState): EvidenceSnapshot
{
    return EvidenceSnapshot::query()->create([
        'managed_environment_id' => (int) $environment->getKey(),
        'workspace_id' => (int) $environment->workspace_id,
        'status' => EvidenceSnapshotStatus::Active->value,
        'completeness_state' => $completenessState,
        'summary' => ['missing_dimensions' => 0, 'stale_dimensions' => 0],
        'generated_at' => now(),
    ]);
}
