<?php

declare(strict_types=1);

use App\Filament\Resources\EnvironmentReviewResource;
use App\Filament\Resources\EvidenceSnapshotResource;
use App\Filament\Resources\FindingResource;
use App\Filament\Resources\InventoryItemResource;
use App\Filament\Resources\StoredReportResource;
use App\Models\Finding;
use App\Models\InventoryItem;
use App\Models\StoredReport;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

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

it('smokes descriptor-first artifact source surfaces in the Filament shell', function (): void {
    [$user, $tenant] = createUserWithTenant(ensureDefaultMicrosoftProviderConnection: true);

    $finding = Finding::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'subject_external_id' => 'spec-284-policy',
        'evidence_jsonb' => ['policy_type' => 'deviceCompliancePolicy'],
    ]);
    $inventory = InventoryItem::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
        'policy_type' => 'deviceCompliancePolicy',
        'display_name' => 'Spec 284 Compliance Inventory',
    ]);
    $report = StoredReport::factory()->permissionPosture()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'workspace_id' => (int) $tenant->workspace_id,
    ]);
    $snapshot = seedEnvironmentReviewEvidence($tenant, findingCount: 1, driftCount: 1);
    $review = composeEnvironmentReviewForTest($tenant, $user, $snapshot);

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

    visit(FindingResource::getUrl('view', ['record' => $finding], tenant: $tenant))
        ->waitForText('Artifact source')
        ->assertSee('Source family')
        ->assertSee('Provider finding type')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    visit(InventoryItemResource::getUrl('view', ['record' => $inventory], tenant: $tenant))
        ->waitForText('Artifact source')
        ->assertSee('Canonical type')
        ->assertSee('Provider display type')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    visit(StoredReportResource::getUrl('view', ['record' => $report], tenant: $tenant))
        ->waitForText('Artifact source')
        ->assertSee('Provider report type')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    visit(EvidenceSnapshotResource::getUrl('view', ['record' => $snapshot], tenant: $tenant))
        ->waitForText('Evidence dimensions')
        ->assertSee('Source family')
        ->assertSee('Provider source detail')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();

    visit(EnvironmentReviewResource::getUrl('view', ['record' => $review], tenant: $tenant))
        ->waitForText('Sections')
        ->click('Details')
        ->waitForText('Artifact source')
        ->assertSee('Artifact source')
        ->assertSee('Source family')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();
});
