<?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\Pages\Reviews\ReviewRegister;
use App\Filament\Pages\Settings\WorkspaceSettings;
use App\Filament\Resources\AlertDeliveryResource;
use App\Filament\Resources\AlertDestinationResource;
use App\Filament\Resources\AlertRuleResource;
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;

function spec314QueryKeys(string $url): array
{
    $query = [];
    parse_str((string) parse_url($url, PHP_URL_QUERY), $query);

    return array_keys($query);
}

function spec314NavigationItemUrls(array $items): array
{
    $urls = [];

    foreach ($items as $item) {
        $url = $item->getUrl();

        if (is_string($url) && $url !== '') {
            $urls[] = $url;
        }

        $childItems = $item->getChildItems();

        if ($childItems instanceof Traversable) {
            $childItems = iterator_to_array($childItems);
        }

        if (is_array($childItems) && $childItems !== []) {
            $urls = [
                ...$urls,
                ...spec314NavigationItemUrls($childItems),
            ];
        }
    }

    return $urls;
}

it('Spec314 workspace hub sidebar urls do not include environment query params', function (string $hub, Closure $urlFactory): void {
    $environment = ManagedEnvironment::factory()->active()->create([
        'name' => 'Remembered Environment',
        'external_id' => 'remembered-environment',
    ]);

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

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

    $url = $urlFactory($workspace);

    expect(WorkspaceHubRegistry::cleanUrl($url))->toBe($url)
        ->and(spec314QueryKeys($url))
        ->not->toContain(...WorkspaceHubRegistry::forbiddenQueryKeys());
})->with([
    'workspace home' => ['workspace_home', fn ($workspace): string => route('admin.home')],
    'workspace overview' => ['workspace_overview', fn ($workspace): string => route('admin.workspace.home', ['workspace' => $workspace])],
    'operations' => ['operations', fn ($workspace): string => OperationRunLinks::index()],
    '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')],
    'evidence overview' => ['evidence_overview', fn ($workspace): string => route('admin.evidence.overview')],
    'review register' => ['review_register', fn ($workspace): string => ReviewRegister::getUrl(panel: 'admin')],
    'customer review workspace' => ['customer_review_workspace', fn ($workspace): string => CustomerReviewWorkspace::getUrl(panel: 'admin')],
    'governance inbox' => ['governance_inbox', fn ($workspace): string => GovernanceInbox::getUrl(panel: 'admin')],
    'decision register' => ['decision_register', fn ($workspace): string => DecisionRegister::getUrl(panel: 'admin')],
    'audit log' => ['audit_log', fn ($workspace): string => route('admin.monitoring.audit-log')],
    'alerts' => ['alerts', fn ($workspace): string => route('filament.admin.alerts')],
    'alert deliveries' => ['alert_deliveries', fn ($workspace): string => AlertDeliveryResource::getUrl(panel: 'admin')],
    'alert rules' => ['alert_rules', fn ($workspace): string => AlertRuleResource::getUrl(panel: 'admin')],
    'alert destinations' => ['alert_destinations', fn ($workspace): string => AlertDestinationResource::getUrl(panel: 'admin')],
    'workspace settings' => ['workspace_settings', fn ($workspace): string => WorkspaceSettings::getUrl(panel: 'admin')],
    'manage workspaces' => ['manage_workspaces', fn ($workspace): string => route('filament.admin.resources.workspaces.index')],
    'managed environments landing' => ['managed_environments_landing', fn ($workspace): string => route('admin.workspace.managed-environments.index', ['workspace' => $workspace])],
]);

it('Spec314 central panel navigation workspace hub URLs stay clean', function (): void {
    $environment = ManagedEnvironment::factory()->active()->create([
        'name' => 'Central Navigation Environment',
        'external_id' => 'central-navigation-environment',
    ]);

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

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

    $this->get(route('admin.operations.index', ['workspace' => $workspace]))
        ->assertOk();

    $workspaceHubUrls = collect(spec314NavigationItemUrls(Filament::getCurrentOrDefaultPanel()->getNavigationItems()))
        ->filter(static fn (string $url): bool => WorkspaceHubRegistry::isWorkspaceHubPath((string) parse_url($url, PHP_URL_PATH)))
        ->values();

    expect($workspaceHubUrls)->not->toBeEmpty();

    foreach ($workspaceHubUrls as $url) {
        expect(WorkspaceHubRegistry::cleanUrl($url))->toBe($url)
            ->and(spec314QueryKeys($url))
            ->not->toContain(...WorkspaceHubRegistry::forbiddenQueryKeys());
    }
});
