<?php

declare(strict_types=1);

use App\Models\Tenant;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    Http::preventStrayRequests();
});

it('does not set or clear tenant context on GET /admin/operations', function (): void {
    $tenant = Tenant::factory()->create();
    [$user, $tenant] = createUserWithTenant($tenant, role: 'owner');

    Filament::setTenant($tenant, true);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id])
        ->get('/admin/operations')
        ->assertOk();

    expect(Filament::getTenant())->toBe($tenant);
});

it('renders workspace scope label when no tenant context is active', function (): void {
    $tenant = Tenant::factory()->create();
    [$user, $tenant] = createUserWithTenant($tenant, role: 'owner');

    Filament::setTenant(null, true);

    $this->actingAs($user)
        ->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id])
        ->get('/admin/operations')
        ->assertOk()
        ->assertSee('All tenants');

    expect(Filament::getTenant())->toBeNull();
});
