<?php

declare(strict_types=1);

use App\Filament\Pages\Reviews\CustomerReviewWorkspace;
use App\Models\ManagedEnvironment;
use App\Support\EnvironmentReviewStatus;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Livewire\Livewire;

it('Spec314 customer review workspace sidebar entry is workspace wide', function (): void {
    $environmentA = ManagedEnvironment::factory()->active()->create(['name' => 'Customer Review A']);
    [$user, $environmentA] = createUserWithTenant(tenant: $environmentA, role: 'readonly');

    $environmentB = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $environmentA->workspace_id,
        'name' => 'Customer Review B',
    ]);
    createUserWithTenant(tenant: $environmentB, user: $user, role: 'readonly');

    $reviewA = composeEnvironmentReviewForTest($environmentA, $user, seedEnvironmentReviewEvidence($environmentA));
    $reviewA->forceFill([
        'status' => EnvironmentReviewStatus::Published->value,
        'published_at' => now(),
        'published_by_user_id' => (int) $user->getKey(),
    ])->save();

    $reviewB = composeEnvironmentReviewForTest($environmentB, $user, seedEnvironmentReviewEvidence($environmentB));
    $reviewB->forceFill([
        'status' => EnvironmentReviewStatus::Published->value,
        'published_at' => now(),
        'published_by_user_id' => (int) $user->getKey(),
    ])->save();

    Filament::setTenant($environmentA, true);
    $url = CustomerReviewWorkspace::getUrl(panel: 'admin');

    expect($url)->not->toContain('tenant=')
        ->and($url)->not->toContain('managed_environment_id');

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

    Livewire::actingAs($user)
        ->test(CustomerReviewWorkspace::class)
        ->assertSet('tableFilters.managed_environment_id.value', null)
        ->assertCanSeeTableRecords([$environmentA->fresh(), $environmentB->fresh()]);
});

it('Spec314 customer review workspace ignores stale persisted environment filters on clean entry', function (): void {
    $environmentA = ManagedEnvironment::factory()->active()->create();
    [$user, $environmentA] = createUserWithTenant(tenant: $environmentA, role: 'readonly');

    $environmentB = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $environmentA->workspace_id,
    ]);
    createUserWithTenant(tenant: $environmentB, user: $user, role: 'readonly');

    composeEnvironmentReviewForTest($environmentA, $user, seedEnvironmentReviewEvidence($environmentA))
        ->forceFill([
            'status' => EnvironmentReviewStatus::Published->value,
            'published_at' => now(),
            'published_by_user_id' => (int) $user->getKey(),
        ])->save();
    composeEnvironmentReviewForTest($environmentB, $user, seedEnvironmentReviewEvidence($environmentB))
        ->forceFill([
            'status' => EnvironmentReviewStatus::Published->value,
            'published_at' => now(),
            'published_by_user_id' => (int) $user->getKey(),
        ])->save();

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

    $component = Livewire::actingAs($user)->test(CustomerReviewWorkspace::class);
    $filtersSessionKey = $component->instance()->getTableFiltersSessionKey();

    session()->put($filtersSessionKey, [
        'managed_environment_id' => ['value' => (string) $environmentA->getKey()],
    ]);

    $this->get(CustomerReviewWorkspace::getUrl(panel: 'admin'))
        ->assertOk()
        ->assertSee($environmentA->name)
        ->assertSee($environmentB->name);

    expect(data_get(session()->get($filtersSessionKey, []), 'managed_environment_id.value'))->toBeNull();
});
