<?php

declare(strict_types=1);

use App\Filament\Resources\ManagedEnvironmentResource;
use App\Models\OperationRun;
use App\Support\ManagedEnvironmentLinks;
use App\Support\OperationRunLinks;
use App\Support\Workspaces\WorkspaceContext;

it('generates canonical managed-environment route families only', function (): void {
    [$user, $tenant] = createMinimalUserWithTenant(role: 'owner');

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id]);

    $urls = [
        ManagedEnvironmentLinks::indexUrl($tenant),
        ManagedEnvironmentLinks::viewUrl($tenant),
        ManagedEnvironmentLinks::requiredPermissionsUrl($tenant),
        ManagedEnvironmentLinks::diagnosticsUrl($tenant),
        ManagedEnvironmentLinks::accessScopesUrl($tenant),
        ManagedEnvironmentLinks::baselineCompareUrl($tenant),
        ManagedEnvironmentLinks::operationsUrl($tenant),
        ManagedEnvironmentLinks::providerConnectionsUrl($tenant),
        ManagedEnvironmentResource::getUrl('index'),
        ManagedEnvironmentResource::getUrl('view', ['record' => $tenant]),
        ManagedEnvironmentResource::getUrl('edit', ['record' => $tenant]),
        ManagedEnvironmentResource::getUrl('memberships', ['record' => $tenant]),
        OperationRunLinks::index($tenant),
    ];

    $run = OperationRun::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
    ]);

    $urls[] = OperationRunLinks::tenantlessView($run);

    foreach ($urls as $url) {
        expect($url)
            ->not->toContain('/admin/tenants')
            ->not->toContain('/admin/t/');
    }

    expect(ManagedEnvironmentLinks::viewUrl($tenant))->toContain('/admin/workspaces/')
        ->and(ManagedEnvironmentLinks::viewUrl($tenant))->toContain('/environments/'.$tenant->getRouteKey())
        ->and(ManagedEnvironmentLinks::requiredPermissionsUrl($tenant))->toEndWith('/required-permissions')
        ->and(ManagedEnvironmentLinks::diagnosticsUrl($tenant))->toEndWith('/diagnostics')
        ->and(ManagedEnvironmentLinks::accessScopesUrl($tenant))->toEndWith('/access-scopes')
        ->and(ManagedEnvironmentLinks::baselineCompareUrl($tenant))->toEndWith('/baseline-compare')
        ->and(ManagedEnvironmentLinks::providerConnectionsUrl($tenant))->toContain('/admin/provider-connections?environment_id='.(int) $tenant->getKey())
        ->and(OperationRunLinks::index($tenant))->toContain('/admin/workspaces/')
        ->and(OperationRunLinks::tenantlessView($run))->toContain('/admin/workspaces/');
});

it('keeps the retired ManagedEnvironmentResource out of global search', function (): void {
    [$user, $tenant] = createMinimalUserWithTenant(role: 'owner');

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id]);

    expect(ManagedEnvironmentResource::getGlobalSearchResults((string) $tenant->name))->toHaveCount(0);
});
