<?php

declare(strict_types=1);

use App\Models\Tenant;
use App\Models\Workspace;
use Filament\Facades\Filament;

it('switches tenant when visiting the tenant menu URL', function (): void {
    $workspace = Workspace::factory()->create();

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

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

    [$user] = createUserWithTenant($tenantA, role: 'owner');

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

    Filament::setTenant($tenantA, true);
    expect(Filament::getTenant()?->is($tenantA))->toBeTrue();

    $response = $this->actingAs($user)
        ->get('/admin/t/'.$tenantB->external_id);

    expect(in_array($response->getStatusCode(), [200, 302], true))->toBeTrue();
    expect(Filament::getTenant())->toBeInstanceOf(Tenant::class);
    expect(Filament::getTenant()?->is($tenantB))->toBeTrue();
});
