<?php

declare(strict_types=1);

use App\Filament\Resources\EntraGroupResource;
use App\Models\EntraGroup;
use App\Models\ManagedEnvironment;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

function entraGroupSearchTitles($results): array
{
    return collect($results)->map(fn ($result): string => (string) $result->title)->all();
}

it('returns no admin global-search results without canonical tenant context', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    EntraGroup::factory()->for($tenant)->create([
        'display_name' => 'Admin search group',
    ]);

    $this->actingAs($user);
    Filament::setCurrentPanel('admin');
    Filament::setTenant(null, true);
    Filament::bootCurrentPanel();

    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenant->workspace_id);

    expect(EntraGroupResource::getGlobalSearchResults('Admin search'))
        ->toHaveCount(0);
});

it('scopes admin global-search results to the remembered environment context', function (): void {
    $tenantA = ManagedEnvironment::factory()->create();
    [$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');

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

    createUserWithTenant(tenant: $tenantB, user: $user, role: 'owner');

    $tenantC = ManagedEnvironment::factory()->create();
    createUserWithTenant(tenant: $tenantC, user: $user, role: 'owner');

    $groupA = EntraGroup::factory()->for($tenantA)->create([
        'display_name' => 'Remembered search group',
    ]);

    EntraGroup::factory()->for($tenantB)->create([
        'display_name' => 'Other search group',
    ]);

    EntraGroup::factory()->for($tenantC)->create([
        'display_name' => 'Other workspace search group',
    ]);

    $this->actingAs($user);
    Filament::setCurrentPanel('admin');
    Filament::setTenant(null, true);
    Filament::bootCurrentPanel();

    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenantA->workspace_id);
    session()->put(WorkspaceContext::LAST_ENVIRONMENT_IDS_SESSION_KEY, [
        (string) $tenantA->workspace_id => (int) $tenantA->getKey(),
    ]);

    $results = EntraGroupResource::getGlobalSearchResults('search group');

    expect(entraGroupSearchTitles($results))->toBe([
        'Remembered search group',
    ]);

    $url = $results->first()?->url;

    expect($url)
        ->toBe(EntraGroupResource::getUrl('view', ['record' => $groupA], panel: 'admin', tenant: $tenantA))
        ->not->toContain('/admin/t/');

    $this->get($url)->assertOk();
});

it('scopes admin global-search results to the selected environment context', function (): void {
    $tenantA = ManagedEnvironment::factory()->create();
    [$user, $tenantA] = createUserWithTenant(tenant: $tenantA, role: 'owner');

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

    createUserWithTenant(tenant: $tenantB, user: $user, role: 'owner');

    $groupA = EntraGroup::factory()->for($tenantA)->create([
        'display_name' => 'Selected environment group',
    ]);

    EntraGroup::factory()->for($tenantB)->create([
        'display_name' => 'Selected environment outsider',
    ]);

    $this->actingAs($user);
    Filament::setCurrentPanel('admin');
    Filament::setTenant($tenantA, true);
    Filament::bootCurrentPanel();

    session()->put(WorkspaceContext::SESSION_KEY, (int) $tenantA->workspace_id);

    $results = EntraGroupResource::getGlobalSearchResults('Selected environment');

    expect(entraGroupSearchTitles($results))->toBe([
        'Selected environment group',
    ]);

    $url = $results->first()?->url;

    expect($url)
        ->toBe(EntraGroupResource::getUrl('view', ['record' => $groupA], panel: 'admin', tenant: $tenantA))
        ->not->toContain('/admin/tenants')
        ->not->toContain('/admin/t/');

    $this->get($url)->assertOk();
});
