<?php

declare(strict_types=1);

use App\Models\ManagedEnvironment;
use App\Models\User;
use App\Support\OperateHub\OperateHubShell;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;

uses(RefreshDatabase::class);

it('keeps workspace hub shell tenantless when an explicit environment filter is present', function (): void {
    $rememberedEnvironment = ManagedEnvironment::factory()->active()->create(['name' => 'Remembered ManagedEnvironment']);
    [$user, $rememberedEnvironment] = createUserWithTenant(tenant: $rememberedEnvironment, role: 'owner');

    $hintedTenant = ManagedEnvironment::factory()->active()->create([
        'workspace_id' => (int) $rememberedEnvironment->workspace_id,
        'name' => 'Hinted ManagedEnvironment',
    ]);

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

    $this->actingAs($user);
    Filament::setTenant(null, true);

    $workspaceId = (int) $rememberedEnvironment->workspace_id;

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

    $request = Request::create(route('admin.operations.index', [
        'workspace' => $workspaceId,
        'environment_id' => (int) $hintedTenant->getKey(),
    ]));
    $request->setLaravelSession(app('session.store'));
    $request->setUserResolver(static fn () => $user);

    $route = app('router')->getRoutes()->match($request);
    $request->setRouteResolver(static fn () => $route);

    $resolved = app(OperateHubShell::class)->resolvedContext($request);

    expect($resolved->workspace?->getKey())->toBe($workspaceId)
        ->and($resolved->tenant)->toBeNull()
        ->and($resolved->tenantSource)->toBe('none')
        ->and($resolved->state)->toBe('tenantless_workspace');
});

it('does not resolve cross-workspace environment filters as shell tenant context on workspace hubs', function (): void {
    $workspaceTenant = ManagedEnvironment::factory()->active()->create(['name' => 'Current Workspace ManagedEnvironment']);
    [$user, $workspaceTenant] = createUserWithTenant(tenant: $workspaceTenant, role: 'owner');

    $foreignTenant = ManagedEnvironment::factory()->active()->create(['name' => 'Foreign ManagedEnvironment']);
    createUserWithTenant(tenant: $foreignTenant, user: User::factory()->create(), role: 'owner');

    $this->actingAs($user);
    Filament::setTenant(null, true);

    $workspaceId = (int) $workspaceTenant->workspace_id;

    session()->put(WorkspaceContext::SESSION_KEY, $workspaceId);

    $request = Request::create(route('admin.operations.index', [
        'workspace' => $workspaceId,
        'environment_id' => (int) $foreignTenant->getKey(),
    ]));
    $request->setLaravelSession(app('session.store'));
    $request->setUserResolver(static fn () => $user);

    $route = app('router')->getRoutes()->match($request);
    $request->setRouteResolver(static fn () => $route);

    $resolved = app(OperateHubShell::class)->resolvedContext($request);

    expect($resolved->workspace?->getKey())->toBe($workspaceId)
        ->and($resolved->tenant)->toBeNull()
        ->and($resolved->state)->toBe('tenantless_workspace')
        ->and($resolved->recoveryReason)->toBeNull();
});

it('uses the routed tenant workspace when the tenant panel is entered without a selected workspace session', function (): void {
    $tenant = ManagedEnvironment::factory()->active()->create(['name' => 'ManagedEnvironment Panel Scope']);
    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner');

    $this->actingAs($user);
    Filament::setTenant(null, true);

    session()->forget(WorkspaceContext::SESSION_KEY);

    $request = Request::create(route('admin.workspace.environments.show', [
        'workspace' => $tenant->workspace,
        'environment' => $tenant,
    ]));
    $request->setLaravelSession(app('session.store'));
    $request->setUserResolver(static fn () => $user);

    $route = app('router')->getRoutes()->match($request);
    $request->setRouteResolver(static fn () => $route);

    $resolved = app(OperateHubShell::class)->resolvedContext($request);

    expect($resolved->workspace?->getKey())->toBe((int) $tenant->workspace_id)
        ->and($resolved->tenant?->is($tenant))->toBeTrue()
        ->and($resolved->workspaceSource)->toBe('route')
        ->and($resolved->tenantSource)->toBe('route');
});
