<?php

declare(strict_types=1);

use App\Models\PlatformUser;
use App\Models\Tenant;
use App\Models\Workspace;
use App\Support\Auth\PlatformCapabilities;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('forbids tenants directory when platform.directory.view is missing', function () {
    $platformUser = PlatformUser::factory()->create([
        'capabilities' => [
            PlatformCapabilities::ACCESS_SYSTEM_PANEL,
        ],
        'is_active' => true,
    ]);

    $this->actingAs($platformUser, 'platform')
        ->get('/system/directory/tenants')
        ->assertForbidden();
});

it('lists tenants in the system directory', function () {
    $workspace = Workspace::factory()->create(['name' => 'Directory Workspace']);

    Tenant::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Contoso',
        'status' => Tenant::STATUS_ACTIVE,
    ]);

    Tenant::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'name' => 'Fabrikam',
        'status' => Tenant::STATUS_ONBOARDING,
    ]);

    $platformUser = PlatformUser::factory()->create([
        'capabilities' => [
            PlatformCapabilities::ACCESS_SYSTEM_PANEL,
            PlatformCapabilities::DIRECTORY_VIEW,
        ],
        'is_active' => true,
    ]);

    $this->actingAs($platformUser, 'platform')
        ->get('/system/directory/tenants')
        ->assertSuccessful()
        ->assertSee('Contoso')
        ->assertSee('Fabrikam');
});
