<?php

declare(strict_types=1);

use App\Filament\Resources\ManagedEnvironmentResource\Pages\ViewManagedEnvironment;
use App\Filament\Resources\ManagedEnvironmentResource\RelationManagers\ManagedEnvironmentMembershipsRelationManager;
use App\Models\User;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Http;
use Livewire\Livewire;

it('renders the tenant members UI DB-only (no outbound HTTP, no background work)', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $tenant->makeCurrent();

    $member = User::factory()->create();
    $member->tenants()->syncWithoutDetaching([
        $tenant->getKey() => ['role' => 'readonly'],
    ]);

    $this->actingAs($user);

    Bus::fake();
    Http::preventStrayRequests();

    Livewire::actingAs($user)
        ->test(ManagedEnvironmentMembershipsRelationManager::class, [
            'ownerRecord' => $tenant,
            'pageClass' => ViewManagedEnvironment::class,
        ])
        ->assertSee($member->email);

    Bus::assertNothingDispatched();
});
