<?php

declare(strict_types=1);

use App\Filament\Resources\TenantReviewResource;
use App\Models\ReviewPack;
use App\Models\Tenant;
use App\Support\TenantReviewStatus;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;

uses(RefreshDatabase::class);

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

beforeEach(function (): void {
    Storage::fake('exports');
});

it('smokes the customer review workspace handoff from tenant review detail', function (): void {
    $tenantPublished = Tenant::factory()->create(['name' => 'Published Tenant']);
    [$user, $tenantPublished] = createUserWithTenant(
        tenant: $tenantPublished,
        role: 'owner',
        workspaceRole: 'manager',
    );

    $tenantWithoutPublished = Tenant::factory()->create([
        'workspace_id' => (int) $tenantPublished->workspace_id,
        'name' => 'No Published Tenant',
    ]);

    createUserWithTenant(
        tenant: $tenantWithoutPublished,
        user: $user,
        role: 'owner',
        workspaceRole: 'manager',
    );

    $publishedSnapshot = seedTenantReviewEvidence($tenantPublished);
    $noPublishedSnapshot = seedTenantReviewEvidence($tenantWithoutPublished);

    $publishedReview = composeTenantReviewForTest($tenantPublished, $user, $publishedSnapshot);
    $publishedReview->forceFill([
        'status' => TenantReviewStatus::Published->value,
        'published_at' => now(),
        'published_by_user_id' => (int) $user->getKey(),
    ])->save();

    $internalOnlyReview = composeTenantReviewForTest($tenantWithoutPublished, $user, $noPublishedSnapshot);
    $internalOnlyReview->forceFill([
        'status' => TenantReviewStatus::Ready->value,
        'published_at' => null,
        'published_by_user_id' => null,
    ])->save();

    Storage::disk('exports')->put('review-packs/customer-review-workspace-smoke.zip', 'PK-test');

    $pack = ReviewPack::factory()->ready()->create([
        'tenant_id' => (int) $tenantPublished->getKey(),
        'workspace_id' => (int) $tenantPublished->workspace_id,
        'tenant_review_id' => (int) $publishedReview->getKey(),
        'evidence_snapshot_id' => (int) $publishedSnapshot->getKey(),
        'initiated_by_user_id' => (int) $user->getKey(),
        'file_path' => 'review-packs/customer-review-workspace-smoke.zip',
        'file_disk' => 'exports',
    ]);

    $publishedReview->forceFill(['current_export_review_pack_id' => (int) $pack->getKey()])->save();

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

    visit(TenantReviewResource::tenantScopedUrl('view', ['record' => $publishedReview], $tenantPublished))
        ->waitForText('Related context')
        ->assertSee('Open customer workspace')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs()
        ->click('Open customer workspace')
        ->waitForText('Customer-safe governance package index')
        ->assertSee('Clear filters')
        ->assertSee('Open review')
        ->assertSee('Governance package')
        ->assertSee('Status')
        ->assertSee('Evidence')
        ->assertSee('Review the executive-ready governance package status')
        ->assertSee('This workspace summarizes current review evidence for service delivery. It does not replace a formal audit opinion, certification, or legal attestation.')
        ->assertSee('Partial')
        ->assertSee('Review required')
        ->assertSee('Available')
        ->assertSee('Review package')
        ->assertDontSee('Publishable')
        ->assertDontSee('No mapped controls')
        ->assertDontSee('Compliance evidence mapping v1')
        ->assertDontSee('Publish review')
        ->assertDontSee('Refresh review')
        ->click('Clear filters')
        ->waitForText('Published Tenant')
        ->assertDontSee('No Published Tenant')
        ->assertDontSee('No published review available yet')
        ->click('Open review')
        ->waitForText('Outcome summary')
        ->assertSee('Download governance package')
        ->assertSee('Governance package')
        ->assertSee('Released governance record')
        ->assertSee('Review status')
        ->assertSee('Primary action')
        ->assertSee('Executive entrypoint')
        ->assertSee('Structured auditor appendix')
        ->assertSee('Assessment basis')
        ->assertDontSee('Control readiness interpretation')
        ->assertDontSee('Compliance evidence mapping v1')
        ->assertDontSee('Publish review')
        ->assertDontSee('Refresh review')
        ->assertDontSee('Create next review')
        ->assertDontSee('Export executive pack')
        ->assertDontSee('Archive review')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();
});
