<?php

declare(strict_types=1);

use App\Filament\Pages\Governance\DecisionRegister;
use App\Filament\Pages\Governance\GovernanceInbox;
use App\Filament\Pages\Monitoring\FindingExceptionsQueue;
use App\Filament\Pages\Reviews\CustomerReviewWorkspace;
use App\Filament\Resources\ProviderConnectionResource;
use App\Models\ManagedEnvironment;
use App\Support\Navigation\WorkspaceHubRegistry;
use App\Support\OperationRunLinks;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;

it('Spec314 sidebar from environment dashboard opens critical workspace hubs without environment context', function (string $hub, Closure $urlFactory): void {
    $environment = ManagedEnvironment::factory()->active()->create([
        'name' => 'Active Environment Context',
        'external_id' => 'active-environment-context',
    ]);

    [$user, $environment] = createUserWithTenant(tenant: $environment, role: 'owner');
    $workspace = $environment->workspace()->firstOrFail();

    Filament::setTenant($environment, true);

    $url = $urlFactory($workspace);

    expect(WorkspaceHubRegistry::hasForbiddenQuery($url))->toBeFalse();

    $this->actingAs($user)
        ->withSession([
            WorkspaceContext::SESSION_KEY => (int) $workspace->getKey(),
            WorkspaceContext::LAST_ENVIRONMENT_IDS_SESSION_KEY => [
                (string) $workspace->getKey() => (int) $environment->getKey(),
            ],
        ])
        ->get($url)
        ->assertOk()
        ->assertSee($workspace->name)
        ->assertSee(__('localization.shell.no_environment_selected'))
        ->assertDontSee(__('localization.shell.environment_scope').': Active Environment Context');
})->with([
    'provider connections' => ['provider_connections', fn ($workspace): string => ProviderConnectionResource::getUrl('index', panel: 'admin')],
    'finding exceptions queue' => ['finding_exceptions_queue', fn ($workspace): string => FindingExceptionsQueue::getUrl(panel: 'admin')],
    'operations' => ['operations', fn ($workspace): string => OperationRunLinks::index()],
    'decision register' => ['decision_register', fn ($workspace): string => DecisionRegister::getUrl(panel: 'admin')],
    'customer reviews' => ['customer_reviews', fn ($workspace): string => CustomerReviewWorkspace::getUrl(panel: 'admin')],
    'governance inbox' => ['governance_inbox', fn ($workspace): string => GovernanceInbox::getUrl(panel: 'admin')],
]);

it('Spec314 remembered environment does not affect workspace hub sidebar urls or shell context', function (): void {
    $rememberedEnvironment = ManagedEnvironment::factory()->active()->create([
        'name' => 'Remembered Environment Boundary',
        'external_id' => 'remembered-environment-boundary',
    ]);

    [$user, $rememberedEnvironment] = createUserWithTenant(tenant: $rememberedEnvironment, role: 'owner');
    $workspace = $rememberedEnvironment->workspace()->firstOrFail();

    $this->actingAs($user);
    Filament::setTenant($rememberedEnvironment, true);
    session()->put(WorkspaceContext::SESSION_KEY, (int) $workspace->getKey());
    session()->put(WorkspaceContext::LAST_ENVIRONMENT_IDS_SESSION_KEY, [
        (string) $workspace->getKey() => (int) $rememberedEnvironment->getKey(),
    ]);

    $url = ProviderConnectionResource::getUrl('index', panel: 'admin');

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

    $this->get($url)
        ->assertOk()
        ->assertSee(__('localization.shell.no_environment_selected'))
        ->assertDontSee(__('localization.shell.environment_scope').': Remembered Environment Boundary');
});
