<?php

declare(strict_types=1);

use App\Filament\Pages\Monitoring\FindingExceptionsQueue;
use App\Models\Finding;
use App\Models\FindingException;
use App\Models\OperationRun;
use App\Models\ManagedEnvironment;
use App\Models\ManagedEnvironmentMembership;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;

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

uses(RefreshDatabase::class);

it('smokes remediated, calm-reference, and explicit-exception monitoring surfaces', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner', workspaceRole: 'manager');

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

    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' => 'Browser hierarchy smoke',
        'requested_at' => now()->subDay(),
        'review_due_at' => now()->addDay(),
        'evidence_summary' => ['reference_count' => 0],
    ]);

    $run = OperationRun::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => null,
        'type' => 'provider.connection.check',
        'status' => OperationRunStatus::Completed->value,
        'outcome' => OperationRunOutcome::Succeeded->value,
    ]);

    $diagnosticsTenant = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
    ]);

    createUserWithTenant(
        tenant: $diagnosticsTenant,
        user: $user,
        role: 'readonly',
        workspaceRole: 'manager',
        ensureDefaultMicrosoftProviderConnection: false,
    );

    ManagedEnvironmentMembership::query()
        ->where('managed_environment_id', (int) $diagnosticsTenant->getKey())
        ->update(['role' => 'readonly']);

    $this->actingAs($user)->withSession([
        WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id,
    ]);
    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);

    Filament::setTenant(null, true);

    visit(FindingExceptionsQueue::getUrl(panel: 'admin'))
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->assertSee('Quiet monitoring mode');

    visit(\App\Support\OperationRunLinks::tenantlessView($run))
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->assertSee('Monitoring detail')
        ->assertSee('Follow-up lane');

    visit('/admin/alerts')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->assertSee('Alert deliveries');

    visit(\App\Filament\Pages\EnvironmentDashboard::getUrl(tenant: $diagnosticsTenant))
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->assertSee($diagnosticsTenant->name)
        ->click('[aria-label="More"]')
        ->assertSee('Open support diagnostics');
});
