<?php

declare(strict_types=1);

use App\Filament\Pages\Operations\TenantlessOperationRunViewer;
use App\Models\OperationRun;
use Filament\Facades\Filament;
use Livewire\Livewire;

it('shows a safe empty state when a verification report is missing', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'operator');
    $this->actingAs($user);

    $tenant->makeCurrent();
    Filament::setTenant($tenant, true);

    $run = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'user_id' => (int) $user->getKey(),
        'type' => 'provider.connection.check',
        'status' => 'completed',
        'outcome' => 'failed',
        'context' => [],
    ]);

    assertNoOutboundHttp(function () use ($run): void {
        Livewire::test(TenantlessOperationRunViewer::class, ['run' => $run])
            ->assertSee('Verification report')
            ->assertSee('Verification report unavailable');
    });
});

it('shows a safe empty state when a verification report is malformed', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'operator');
    $this->actingAs($user);

    $tenant->makeCurrent();
    Filament::setTenant($tenant, true);

    $run = OperationRun::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'user_id' => (int) $user->getKey(),
        'type' => 'provider.connection.check',
        'status' => 'completed',
        'outcome' => 'failed',
        'context' => [
            'verification_report' => [
                'schema_version' => '1.0.0',
                'flow' => 'provider.connection.check',
            ],
        ],
    ]);

    assertNoOutboundHttp(function () use ($run): void {
        Livewire::test(TenantlessOperationRunViewer::class, ['run' => $run])
            ->assertSee('Verification report')
            ->assertSee('Verification report unavailable');
    });
});
