<?php

declare(strict_types=1);

use App\Filament\Pages\Governance\DecisionRegister;
use App\Filament\Resources\FindingExceptionResource\Pages\ViewFindingException;
use App\Models\Finding;
use App\Models\FindingException;
use App\Models\FindingExceptionDecision;
use App\Models\ManagedEnvironment;
use App\Support\Navigation\CanonicalNavigationContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

it('adds a decision register back action while keeping existing detail actions in place', function (): void {
    $tenant = ManagedEnvironment::factory()->create([
        'status' => 'active',
        'name' => 'Alpha ManagedEnvironment',
    ]);
    [$user, $tenant] = createUserWithTenant($tenant, role: 'owner', workspaceRole: 'owner');

    $finding = Finding::factory()->for($tenant)->create();

    $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(),
        'approved_by_user_id' => (int) $user->getKey(),
        'status' => FindingException::STATUS_ACTIVE,
        'current_validity_state' => FindingException::VALIDITY_VALID,
        'request_reason' => 'Detail continuity context',
        'approval_reason' => 'Active approval still visible',
        'requested_at' => now()->subDays(5),
        'approved_at' => now()->subDays(4),
        'effective_from' => now()->subDays(4),
        'review_due_at' => now()->addDays(2),
        'expires_at' => now()->addDays(10),
        'evidence_summary' => ['reference_count' => 0],
    ]);

    $decision = $exception->decisions()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'actor_user_id' => (int) $user->getKey(),
        'decision_type' => FindingExceptionDecision::TYPE_APPROVED,
        'reason' => 'Approved for detail continuity test',
        'metadata' => [],
        'decided_at' => now()->subDays(4),
    ]);

    $exception->forceFill(['current_decision_id' => (int) $decision->getKey()])->save();

    $this->actingAs($user);
    $tenant->makeCurrent();
    Filament::setTenant($tenant, true);

    $context = CanonicalNavigationContext::forDecisionRegister(
        canonicalRouteName: DecisionRegister::getRouteName(),
        tenantId: (int) $tenant->getKey(),
        backLinkUrl: DecisionRegister::getUrl(panel: 'admin', parameters: [
            'managed_environment_id' => (string) $tenant->getKey(),
        ]),
    );

    Livewire::withQueryParams($context->toQuery())
        ->test(ViewFindingException::class, ['record' => $exception->getKey()])
        ->assertOk()
        ->assertActionVisible('return_to_decision_register')
        ->assertActionVisible('renew_exception')
        ->assertActionVisible('revoke_exception')
        ->assertSee('Opened from the workspace decision register');
});