<?php

declare(strict_types=1);

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

uses(RefreshDatabase::class);

pest()->browser()->timeout(20_000);

it('smokes workspace overview environment-first final cutover copy', function (): void {
    $user = User::factory()->create();
    $workspace = Workspace::factory()->create([
        'name' => 'Spec 299 Workspace',
    ]);

    WorkspaceMembership::factory()->create([
        'workspace_id' => (int) $workspace->getKey(),
        'user_id' => (int) $user->getKey(),
        'role' => 'owner',
    ]);

    $this->actingAs($user)->withSession([
        WorkspaceContext::SESSION_KEY => (int) $workspace->getKey(),
    ]);
    session()->put(WorkspaceContext::SESSION_KEY, (int) $workspace->getKey());

    visit(route('admin.workspace.home', ['workspace' => $workspace]))
        ->waitForText('Workspace overview')
        ->assertSee('No accessible environments in this workspace')
        ->assertSee('Governance risk counts affected environments')
        ->assertSee('Calm wording stays bounded to visible environments and checked domains')
        ->assertDontSee('No accessible tenants in this workspace')
        ->assertDontSee('Governance risk counts affected tenants')
        ->assertDontSee('Calm wording stays bounded to visible tenants and checked domains')
        ->assertNoJavaScriptErrors()
        ->assertNoConsoleLogs();
});
