<?php

declare(strict_types=1);

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

uses(RefreshDatabase::class);

it('forbids workspaces 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/workspaces')
        ->assertForbidden();
});

it('lists workspaces in the system directory', function () {
    $workspaceA = Workspace::factory()->create(['name' => 'Alpha Workspace']);
    $workspaceB = Workspace::factory()->create(['name' => 'Bravo Workspace']);

    ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $workspaceA->getKey(),
        'name' => 'ManagedEnvironment A',
    ]);

    ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $workspaceB->getKey(),
        'name' => 'ManagedEnvironment B',
    ]);

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

    $this->actingAs($platformUser, 'platform')
        ->get('/system/directory/workspaces')
        ->assertSuccessful()
        ->assertSee('Alpha Workspace')
        ->assertSee('Bravo Workspace');
});
