<?php

declare(strict_types=1);

use App\Filament\Pages\Governance\DecisionRegister;
use App\Models\Finding;
use App\Models\FindingException;
use App\Models\Tenant;
use App\Models\User;
use App\Services\Findings\FindingExceptionService;
use Illuminate\Foundation\Testing\RefreshDatabase;

pest()->browser()->timeout(20_000);

uses(RefreshDatabase::class);

function spec265ApprovedFindingException(Tenant $tenant, User $requester): FindingException
{
    $approver = User::factory()->create();
    createUserWithTenant(
        tenant: $tenant,
        user: $approver,
        role: 'owner',
        workspaceRole: 'manager',
        ensureDefaultMicrosoftProviderConnection: false,
    );

    $finding = Finding::factory()->for($tenant)->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'status' => Finding::STATUS_RISK_ACCEPTED,
    ]);

    /** @var FindingExceptionService $service */
    $service = app(FindingExceptionService::class);

    $requested = $service->request($finding, $tenant, $requester, [
        'owner_user_id' => (int) $requester->getKey(),
        'request_reason' => 'Spec265 browser smoke request.',
        'review_due_at' => now()->addDays(7)->toDateTimeString(),
        'expires_at' => now()->addDays(14)->toDateTimeString(),
    ]);

    return $service->approve($requested, $approver, [
        'effective_from' => now()->subDay()->toDateTimeString(),
        'expires_at' => now()->addDays(14)->toDateTimeString(),
        'approval_reason' => 'Spec265 browser smoke approval.',
    ]);
}

function spec265SmokeLoginUrl(User $user, Tenant $tenant, string $redirect = ''): string
{
    return route('admin.local.smoke-login', array_filter([
        'email' => $user->email,
        'tenant' => $tenant->external_id,
        'workspace' => $tenant->workspace->slug,
        'redirect' => $redirect,
    ], static fn (?string $value): bool => filled($value)));
}

it('smokes the decision register continuity to the existing exception detail page', function (): void {
    [$user, $tenant] = createUserWithTenant(
        role: 'owner',
        workspaceRole: 'manager',
        ensureDefaultMicrosoftProviderConnection: false,
    );

    spec265ApprovedFindingException($tenant, $user);

    $decisionRegisterUrl = DecisionRegister::getUrl(panel: 'admin', parameters: [
        'tenant_id' => (string) $tenant->getKey(),
    ]);

    visit(spec265SmokeLoginUrl($user, $tenant))
        ->waitForText('Dashboard')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    visit($decisionRegisterUrl)
        ->waitForText('Decision register')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->assertSee('The register is currently filtered to one tenant.')
        ->assertSee($tenant->name)
        ->assertSee('Open decision')
        ->click('Open decision')
        ->waitForText('Opened from the workspace decision register')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->assertSee('Back to decision register')
        ->assertSee('Renew exception')
        ->assertSee('Revoke exception')
        ->click('Back to decision register')
        ->waitForText('Decision register')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->assertSee('The register is currently filtered to one tenant.')
        ->assertSee($tenant->name)
        ->assertSee('Open decision');
});
