<?php

declare(strict_types=1);

use App\Filament\Pages\Reviews\ReviewRegister;
use App\Models\Tenant;
use App\Support\Workspaces\WorkspaceContext;
use Livewire\Livewire;

it('defaults the canonical review register to the remembered tenant when tenant context is available', function (): void {
    $tenantA = Tenant::factory()->create(['name' => 'Alpha Tenant']);
    [$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');

    $tenantB = Tenant::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
        'name' => 'Beta Tenant',
    ]);
    createUserWithTenant(tenant: $tenantB, user: $user, role: 'readonly');

    $reviewA = composeTenantReviewForTest($tenantA, $user);
    $reviewB = composeTenantReviewForTest(
        $tenantB,
        $user,
        seedTenantReviewEvidence(
            tenant: $tenantB,
            permissionPayload: [
                'required_count' => 11,
                'granted_count' => 7,
            ],
            operationRunCount: 0,
        ),
    );

    $this->actingAs($user);
    setAdminPanelContext();
    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenantA->workspace_id);
    session()->put(WorkspaceContext::LAST_TENANT_IDS_SESSION_KEY, [
        (string) $tenantA->workspace_id => (int) $tenantB->getKey(),
    ]);

    Livewire::actingAs($user)
        ->test(ReviewRegister::class)
        ->assertSet('tableFilters.tenant_id.value', (string) $tenantB->getKey())
        ->assertCanSeeTableRecords([$reviewB])
        ->assertCanNotSeeTableRecords([$reviewA])
        ->assertSee('Publication blocked')
        ->assertSee('Resolve the review blockers before publication')
        ->assertDontSee('Publishable');
});

it('prefilters the review register from a tenant query parameter and accepts external tenant identifiers', function (): void {
    $tenantA = Tenant::factory()->create(['name' => 'Alpha Tenant']);
    [$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');

    $tenantB = Tenant::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
        'name' => 'Beta Tenant',
    ]);
    createUserWithTenant(tenant: $tenantB, user: $user, role: 'readonly');

    $reviewA = composeTenantReviewForTest(
        $tenantA,
        $user,
        seedTenantReviewEvidence(
            tenant: $tenantA,
            permissionPayload: [
                'required_count' => 11,
                'granted_count' => 7,
            ],
            operationRunCount: 0,
        ),
    );
    $reviewB = composeTenantReviewForTest($tenantB, $user);

    $this->actingAs($user);
    setAdminPanelContext();
    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenantA->workspace_id);

    Livewire::withQueryParams(['tenant' => (string) $tenantA->external_id])
        ->test(ReviewRegister::class)
        ->assertSet('tableFilters.tenant_id.value', (string) $tenantA->getKey())
        ->assertCanSeeTableRecords([$reviewA])
        ->assertCanNotSeeTableRecords([$reviewB])
        ->assertSee('Publication blocked')
        ->assertSee('Resolve the review blockers before publication')
        ->assertDontSee('Publishable');
});

it('scopes canonical tenant filter options to entitled tenants only', function (): void {
    $tenantA = Tenant::factory()->create(['name' => 'Alpha Tenant']);
    [$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');

    $tenantB = Tenant::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
        'name' => 'Beta Tenant',
    ]);
    createUserWithTenant(tenant: $tenantB, user: $user, role: 'readonly');

    $tenantC = Tenant::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
        'name' => 'Gamma Tenant',
    ]);

    $this->actingAs($user);
    setAdminPanelContext();
    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenantA->workspace_id);

    $component = Livewire::actingAs($user)->test(ReviewRegister::class);
    $tenantFilter = $component->instance()->getTable()->getFilters()['tenant_id'] ?? null;

    expect($tenantFilter)->not->toBeNull()
        ->and($tenantFilter?->getOptions())->toBe([
            (string) $tenantA->getKey() => $tenantA->name,
            (string) $tenantB->getKey() => $tenantB->name,
        ])
        ->and($tenantFilter?->getOptions())->not->toHaveKey((string) $tenantC->getKey());
});
