<?php

declare(strict_types=1);

use App\Models\ManagedEnvironment;
use App\Models\User;
use App\Support\Workspaces\WorkspaceContext;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('shows a recovery label when workspace-scoped surfaces render without an active workspace', function (): void {
    $tenant = ManagedEnvironment::factory()->active()->create(['name' => 'Recovery ManagedEnvironment']);
    [$user] = createUserWithTenant(tenant: $tenant, role: 'owner');

    session()->forget(WorkspaceContext::SESSION_KEY);

    $this->actingAs($user)
        ->get('/admin/workspaces')
        ->assertOk()
        ->assertSee('Context unavailable')
        ->assertSee('Choose workspace');
});

it('shows explicit recovery wording when an invalid tenant hint is discarded on a workspace route', function (): void {
    $validTenant = ManagedEnvironment::factory()->active()->create(['name' => 'Valid Workspace ManagedEnvironment']);
    [$user, $validTenant] = createUserWithTenant(tenant: $validTenant, role: 'owner');

    $foreignTenant = ManagedEnvironment::factory()->active()->create(['name' => 'Foreign Workspace ManagedEnvironment']);
    createUserWithTenant(tenant: $foreignTenant, user: User::factory()->create(), role: 'owner');

    $this->actingAs($user)
        ->withSession([
            WorkspaceContext::SESSION_KEY => (int) $validTenant->workspace_id,
        ])
        ->get(route('admin.operations.index', ['workspace' => $validTenant->workspace, 'tenant' => $foreignTenant->external_id]))
        ->assertOk()
        ->assertSee('Context unavailable')
        ->assertSee(__('localization.shell.no_environment_selected'))
        ->assertDontSee(__('localization.shell.environment_scope').': '.$foreignTenant->name);
});
