<?php

declare(strict_types=1);

use App\Models\OperationRun;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

final class KpiHeaderTenantlessTest extends TestCase
{
    use RefreshDatabase;

    public function test_shows_workspace_operations_kpi_stats_when_environment_shell_context_is_absent(): void
    {
        [$user, $tenant] = createUserWithTenant(role: 'owner');

        OperationRun::factory()->count(3)->create([
            'workspace_id' => (int) $tenant->workspace_id,
            'managed_environment_id' => (int) $tenant->getKey(),
        ]);

        Filament::setTenant(null, true);

        $this->actingAs($user)
            ->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id])
            ->get(\App\Support\OperationRunLinks::index())
            ->assertOk()
            ->assertSee('Total Operations (30 days)')
            ->assertSee('Active Operations')
            ->assertSee('Failed/Partial (7 days)')
            ->assertSee('Avg Duration (7 days)');
    }
}
