<?php

use App\Filament\Pages\Tenancy\RegisterTenant;
use Filament\Facades\Filament;
use Livewire\Livewire;

describe('Register tenant page authorization', function () {
    it('is not visible for readonly members', function () {
        [$user, $tenant] = createUserWithTenant(role: 'readonly');

        $this->actingAs($user);
        $tenant->makeCurrent();

        Filament::setCurrentPanel(Filament::getPanel('admin'));

        expect(RegisterTenant::canView())->toBeFalse();

        Filament::setCurrentPanel(null);
    });

    it('is visible for owner members', function () {
        [$user, $tenant] = createUserWithTenant(role: 'owner');

        $this->actingAs($user);
        $tenant->makeCurrent();

        Filament::setCurrentPanel(Filament::getPanel('admin'));

        expect(RegisterTenant::canView())->toBeTrue();

        Filament::setCurrentPanel(null);
    });

    it('rejects readonly members when they try to mount the register-tenant page', function () {
        [$user, $tenant] = createUserWithTenant(role: 'readonly');

        $this->actingAs($user);
        $tenant->makeCurrent();

        Filament::setCurrentPanel(Filament::getPanel('admin'));

        Livewire::actingAs($user)
            ->test(RegisterTenant::class)
            ->assertNotFound();

        Filament::setCurrentPanel(null);
    });
});
