<?php

declare(strict_types=1);

use App\Filament\Pages\Governance\GovernanceInbox;
use App\Filament\Pages\Monitoring\FindingExceptionsQueue;
use App\Models\Finding;
use App\Models\FindingException;
use App\Models\ManagedEnvironment;
use App\Support\Navigation\CanonicalNavigationContext;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Livewire\Livewire;

it('keeps the finding exceptions queue secondary when opened from the governance inbox', function (): void {
    $tenant = ManagedEnvironment::factory()->create([
        'status' => 'active',
        'name' => 'Alpha ManagedEnvironment',
        'external_id' => 'alpha-tenant',
    ]);
    [$user, $tenant] = createUserWithTenant($tenant, role: 'owner', workspaceRole: 'manager');

    $finding = Finding::factory()
        ->for($tenant)
        ->riskAccepted()
        ->create([
            'workspace_id' => (int) $tenant->workspace_id,
            'subject_external_id' => 'exception-secondary-finding',
        ]);

    $exception = FindingException::query()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'finding_id' => (int) $finding->getKey(),
        'requested_by_user_id' => (int) $user->getKey(),
        'owner_user_id' => (int) $user->getKey(),
        'status' => FindingException::STATUS_PENDING,
        'current_validity_state' => FindingException::VALIDITY_MISSING_SUPPORT,
        'request_reason' => 'Exception queue return context',
        'requested_at' => now()->subDay(),
        'review_due_at' => now()->addDay(),
        'evidence_summary' => ['reference_count' => 0],
    ]);

    $this->actingAs($user);
    Filament::setCurrentPanel('admin');
    Filament::setTenant(null, true);
    Filament::bootCurrentPanel();
    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);

    $context = CanonicalNavigationContext::forGovernanceInbox(
        canonicalRouteName: GovernanceInbox::getRouteName(Filament::getPanel('admin')),
        tenantId: (int) $tenant->getKey(),
        familyKey: 'finding_exceptions',
        backLinkUrl: GovernanceInbox::getUrl(panel: 'admin', parameters: [
            'environment_id' => (string) $tenant->getKey(),
            'family' => 'finding_exceptions',
        ]),
    );

    $component = Livewire::withQueryParams(array_replace($context->toQuery(), [
        'environment_id' => (int) $tenant->getKey(),
        'exception' => (int) $exception->getKey(),
    ]))
        ->actingAs($user)
        ->test(FindingExceptionsQueue::class)
        ->assertSet('tableFilters.managed_environment_id.value', (string) $tenant->getKey())
        ->assertSet('selectedFindingExceptionId', (int) $exception->getKey())
        ->assertActionVisible('return_to_governance_inbox')
        ->assertActionVisible('open_selected_exception')
        ->assertActionVisible('open_selected_finding')
        ->assertSee('Exception queue return context')
        ->assertSee('Focused review lane')
        ->assertDontSee('This workspace decision surface routes you');

    expect($component->instance()->selectedExceptionUrl())
        ->toContain('nav%5Bsource_surface%5D=governance.inbox')
        ->toContain('nav%5Bfamily_key%5D=finding_exceptions')
        ->toContain('nav%5Benvironment_id%5D='.(string) $tenant->getKey());

    expect($component->instance()->selectedFindingUrl())
        ->toContain('nav%5Bsource_surface%5D=governance.inbox')
        ->toContain('nav%5Bfamily_key%5D=finding_exceptions')
        ->toContain('nav%5Benvironment_id%5D='.(string) $tenant->getKey());
});
