<?php

declare(strict_types=1);

use App\Filament\Resources\EvidenceSnapshotResource;
use App\Filament\Resources\ReviewPackResource;
use App\Filament\Resources\TenantReviewResource;
use App\Models\Tenant;
use App\Support\TenantReviewCompletenessState;
use App\Support\TenantReviewStatus;
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 = Tenant::factory()->create(['name' => 'Browser Smoke Stale Tenant']);
    [$user, $staleTenant] = createUserWithTenant(tenant: $staleTenant, role: 'owner');

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

    $staleSnapshot = seedStaleTenantReviewEvidence($staleTenant);
    $partialSnapshot = seedPartialTenantReviewEvidence($partialTenant);

    $partialReview = $this->makeArtifactTruthReview(
        tenant: $partialTenant,
        user: $user,
        snapshot: $partialSnapshot,
        reviewOverrides: [
            'status' => TenantReviewStatus::Ready->value,
            'completeness_state' => TenantReviewCompletenessState::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' => TenantReviewStatus::Published->value,
            'published_at' => now(),
            'published_by_user_id' => (int) $user->getKey(),
            'completeness_state' => TenantReviewCompletenessState::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' => TenantReviewStatus::Published->value,
            'review_completeness_state' => TenantReviewCompletenessState::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: 'tenant'))
        ->waitForText('Outcome summary')
        ->assertNoJavaScriptErrors()
        ->assertSee('Stale')
        ->assertSee('Refresh the stale evidence before relying on this snapshot');

    visit(TenantReviewResource::tenantScopedUrl('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: 'tenant'))
        ->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 = Tenant::factory()->create(['name' => 'Browser Canonical Stale Tenant']);
    [$user, $staleTenant] = createUserWithTenant(tenant: $staleTenant, role: 'owner');

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

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

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

    $this->makeArtifactTruthReview(
        tenant: $staleTenant,
        user: $user,
        snapshot: $staleSnapshot,
        reviewOverrides: [
            'status' => TenantReviewStatus::Published->value,
            'published_at' => now(),
            'published_by_user_id' => (int) $user->getKey(),
            'completeness_state' => TenantReviewCompletenessState::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' => TenantReviewStatus::Ready->value,
            'completeness_state' => TenantReviewCompletenessState::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 Tenant')
        ->assertNoJavaScriptErrors()
        ->assertSee('Browser Canonical Fresh Tenant')
        ->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 Tenant')
        ->assertNoJavaScriptErrors()
        ->assertSee('Browser Canonical Partial Tenant')
        ->assertSee('Internal only')
        ->assertSee('Refresh the evidence basis before publishing this review')
        ->assertSee('Complete the evidence basis before publishing this review')
        ->assertDontSee('Publishable');
});
