<?php

use App\Models\Tenant;
use App\Models\User;
use App\Models\Workspace;
use App\Models\WorkspaceMembership;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

test('default Filament tenant selection respects current workspace context', function () {
    $user = User::factory()->create();

    $workspaceA = Workspace::factory()->create(['name' => 'Workspace A']);
    $workspaceB = Workspace::factory()->create(['name' => 'Workspace B']);

    WorkspaceMembership::factory()->create([
        'workspace_id' => $workspaceA->getKey(),
        'user_id' => $user->getKey(),
        'role' => 'owner',
    ]);

    WorkspaceMembership::factory()->create([
        'workspace_id' => $workspaceB->getKey(),
        'user_id' => $user->getKey(),
        'role' => 'owner',
    ]);

    $tenantA = Tenant::factory()->create([
        'workspace_id' => $workspaceA->getKey(),
        'status' => 'active',
    ]);

    $tenantB = Tenant::factory()->create([
        'workspace_id' => $workspaceB->getKey(),
        'status' => 'active',
    ]);

    $user->tenants()->syncWithoutDetaching([
        $tenantA->getKey() => ['role' => 'owner'],
        $tenantB->getKey() => ['role' => 'owner'],
    ]);

    session()->put(WorkspaceContext::SESSION_KEY, (int) $workspaceA->getKey());
    $user->forceFill(['last_workspace_id' => (int) $workspaceA->getKey()])->save();

    $this->actingAs($user)
        ->get(route('filament.admin.pages.choose-tenant'))
        ->assertOk()
        ->assertSee($tenantA->name)
        ->assertDontSee($tenantB->name);
});

test('user menu does not render a workspace switcher (topbar context bar is the single entry point)', function () {
    [$user, $tenant] = createUserWithTenant();

    $workspace = Workspace::query()->whereKey($tenant->workspace_id)->firstOrFail();

    $this->actingAs($user)
        ->get(route('filament.admin.resources.tenants.index', filamentTenantRouteParams($tenant)))
        ->assertOk()
        ->assertSee($workspace->name)
        ->assertDontSee('name="workspace_id"', escape: false);
});

test('workspace-scoped operations honor a valid tenant query hint over remembered tenant context', function () {
    $rememberedTenant = Tenant::factory()->create([
        'workspace_id' => null,
        'status' => 'active',
        'name' => 'Remembered Topbar Tenant',
    ]);
    [$user, $rememberedTenant] = createUserWithTenant(tenant: $rememberedTenant, role: 'owner');

    $hintedTenant = Tenant::factory()->create([
        'workspace_id' => (int) $rememberedTenant->workspace_id,
        'status' => 'active',
        'name' => 'Hinted Topbar Tenant',
    ]);

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

    Filament::setTenant(null, true);

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

    $this->actingAs($user)
        ->withSession([
            WorkspaceContext::SESSION_KEY => $workspaceId,
            WorkspaceContext::LAST_TENANT_IDS_SESSION_KEY => [
                (string) $workspaceId => (int) $rememberedTenant->getKey(),
            ],
        ])
        ->get(route('admin.operations.index', ['tenant' => $hintedTenant->external_id]))
        ->assertOk()
        ->assertSee('Tenant scope: Hinted Topbar Tenant')
        ->assertDontSee('Tenant scope: Remembered Topbar Tenant');
});
