<?php

declare(strict_types=1);

use App\Filament\Resources\EnvironmentReviewResource;
use App\Filament\Resources\EvidenceSnapshotResource;
use App\Filament\Resources\ReviewPackResource;
use App\Models\ManagedEnvironment;
use App\Support\EnvironmentReviewCompletenessState;
use App\Support\EnvironmentReviewStatus;
use App\Support\Workspaces\WorkspaceContext;
use Tests\Feature\Concerns\BuildsGovernanceArtifactTruthFixtures;

uses(BuildsGovernanceArtifactTruthFixtures::class);

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

it('smokes tenant-scoped evidence freshness and publication trust surfaces', function (): void {
    $staleTenant = ManagedEnvironment::factory()->create(['name' => 'Browser Smoke Stale ManagedEnvironment']);
    [$user, $staleTenant] = createUserWithTenant(tenant: $staleTenant, role: 'owner');

    $partialTenant = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $staleTenant->workspace_id,
        'name' => 'Browser Smoke Partial ManagedEnvironment',
    ]);
    createUserWithTenant(tenant: $partialTenant, user: $user, role: 'owner');

    $staleSnapshot = seedStaleEnvironmentReviewEvidence($staleTenant);
    $partialSnapshot = seedPartialEnvironmentReviewEvidence($partialTenant);

    $partialReview = $this->makeArtifactTruthReview(
        tenant: $partialTenant,
        user: $user,
        snapshot: $partialSnapshot,
        reviewOverrides: [
            'status' => EnvironmentReviewStatus::Ready->value,
            'completeness_state' => EnvironmentReviewCompletenessState::Complete->value,
        ],
        summaryOverrides: [
            'publish_blockers' => [],
            'section_state_counts' => [
                'complete' => 6,
                'partial' => 0,
                'missing' => 0,
                'stale' => 0,
            ],
        ],
    );

    $staleReview = $this->makeArtifactTruthReview(
        tenant: $staleTenant,
        user: $user,
        snapshot: $staleSnapshot,
        reviewOverrides: [
            'status' => EnvironmentReviewStatus::Published->value,
            'published_at' => now(),
            'published_by_user_id' => (int) $user->getKey(),
            'completeness_state' => EnvironmentReviewCompletenessState::Complete->value,
        ],
        summaryOverrides: [
            'publish_blockers' => [],
            'section_state_counts' => [
                'complete' => 6,
                'partial' => 0,
                'missing' => 0,
                'stale' => 0,
            ],
        ],
    );

    $stalePack = $this->makeArtifactTruthReviewPack(
        tenant: $staleTenant,
        user: $user,
        snapshot: $staleSnapshot,
        review: $staleReview,
        summaryOverrides: [
            'review_status' => EnvironmentReviewStatus::Published->value,
            'review_completeness_state' => EnvironmentReviewCompletenessState::Complete->value,
        ],
    );

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

    visit(EvidenceSnapshotResource::getUrl('view', ['record' => $staleSnapshot], tenant: $staleTenant, panel: 'admin'))
        ->waitForText('Outcome summary')
        ->assertNoJavaScriptErrors()
        ->assertSee('Stale')
        ->assertSee('Refresh the stale evidence before relying on this snapshot');

    visit(EnvironmentReviewResource::environmentScopedUrl('view', ['record' => $partialReview], $partialTenant))
        ->waitForText('Outcome summary')
        ->assertNoJavaScriptErrors()
        ->assertSee('Internal only')
        ->assertSee('Complete the evidence basis before publishing this review');

    visit(ReviewPackResource::getUrl('index', tenant: $staleTenant))
        ->waitForText('Download')
        ->assertNoJavaScriptErrors()
        ->assertSee('Internal only')
        ->assertSee('Refresh the source review before sharing this pack')
        ->assertSee('Download');

    visit(ReviewPackResource::getUrl('view', ['record' => $stalePack], tenant: $staleTenant, panel: 'admin'))
        ->waitForText('Outcome summary')
        ->assertNoJavaScriptErrors()
        ->assertSee('Internal only')
        ->assertSee('Refresh the source review before sharing this pack')
        ->assertSee('Download');
});

it('smokes canonical evidence and review trust surfaces', function (): void {
    $staleTenant = ManagedEnvironment::factory()->create(['name' => 'Browser Canonical Stale ManagedEnvironment']);
    [$user, $staleTenant] = createUserWithTenant(tenant: $staleTenant, role: 'owner');

    $partialTenant = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $staleTenant->workspace_id,
        'name' => 'Browser Canonical Partial ManagedEnvironment',
    ]);
    createUserWithTenant(tenant: $partialTenant, user: $user, role: 'owner');

    $freshTenant = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $staleTenant->workspace_id,
        'name' => 'Browser Canonical Fresh ManagedEnvironment',
    ]);
    createUserWithTenant(tenant: $freshTenant, user: $user, role: 'owner');

    $staleSnapshot = $this->makeStaleArtifactTruthEvidenceSnapshot($staleTenant);
    $freshSnapshot = $this->makeArtifactTruthEvidenceSnapshot($freshTenant);
    $partialSnapshot = seedPartialEnvironmentReviewEvidence($partialTenant);

    $this->makeArtifactTruthReview(
        tenant: $staleTenant,
        user: $user,
        snapshot: $staleSnapshot,
        reviewOverrides: [
            'status' => EnvironmentReviewStatus::Published->value,
            'published_at' => now(),
            'published_by_user_id' => (int) $user->getKey(),
            'completeness_state' => EnvironmentReviewCompletenessState::Complete->value,
        ],
        summaryOverrides: [
            'publish_blockers' => [],
            'section_state_counts' => [
                'complete' => 6,
                'partial' => 0,
                'missing' => 0,
                'stale' => 0,
            ],
        ],
    );

    $this->makeArtifactTruthReview(
        tenant: $partialTenant,
        user: $user,
        snapshot: $partialSnapshot,
        reviewOverrides: [
            'status' => EnvironmentReviewStatus::Ready->value,
            'completeness_state' => EnvironmentReviewCompletenessState::Complete->value,
        ],
        summaryOverrides: [
            'publish_blockers' => [],
            'section_state_counts' => [
                'complete' => 6,
                'partial' => 0,
                'missing' => 0,
                'stale' => 0,
            ],
        ],
    );

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

    visit(route('admin.evidence.overview'))
        ->waitForText('Browser Canonical Stale ManagedEnvironment')
        ->assertNoJavaScriptErrors()
        ->assertSee('Browser Canonical Fresh ManagedEnvironment')
        ->assertSee('Refresh the stale evidence before relying on this snapshot')
        ->assertSee('Create a current review from this evidence snapshot');

    visit('/admin/reviews')
        ->waitForText('Browser Canonical Stale ManagedEnvironment')
        ->assertNoJavaScriptErrors()
        ->assertSee('Browser Canonical Partial ManagedEnvironment')
        ->assertSee('Internal only')
        ->assertSee('Refresh the evidence basis before publishing this review')
        ->assertSee('Complete the evidence basis before publishing this review')
        ->assertDontSee('Publishable');
});
