<?php

declare(strict_types=1);

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

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

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

    $reviewA = composeEnvironmentReviewForTest($tenantA, $user);
    $reviewB = composeEnvironmentReviewForTest(
        $tenantB,
        $user,
        seedEnvironmentReviewEvidence(
            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_ENVIRONMENT_IDS_SESSION_KEY, [
        (string) $tenantA->workspace_id => (int) $tenantB->getKey(),
    ]);

    Livewire::withHeaders(['referer' => ReviewRegister::getUrl(panel: 'admin')])
        ->actingAs($user)
        ->test(ReviewRegister::class)
        ->assertSet('tableFilters.managed_environment_id.value', null)
        ->assertCanSeeTableRecords([$reviewA, $reviewB])
        ->assertSee('Publication blocked')
        ->assertSee('Resolve the review blockers before publication');
});

it('prefilters the review register from an explicit environment_id query parameter', function (): void {
    $tenantA = ManagedEnvironment::factory()->create(['name' => 'Alpha ManagedEnvironment']);
    [$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');

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

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

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

    Livewire::withHeaders(['referer' => ReviewRegister::getUrl(panel: 'admin')])
        ->withQueryParams(['environment_id' => (int) $tenantA->getKey()])
        ->test(ReviewRegister::class)
        ->assertSet('tableFilters.managed_environment_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 = ManagedEnvironment::factory()->create(['name' => 'Alpha ManagedEnvironment']);
    [$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');

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

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

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

    $component = Livewire::withHeaders(['referer' => ReviewRegister::getUrl(panel: 'admin')])
        ->actingAs($user)
        ->test(ReviewRegister::class);
    $tenantFilter = $component->instance()->getTable()->getFilters()['managed_environment_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());
});
