<?php

use App\Models\OperationRun;
use App\Support\Workspaces\WorkspaceContext;
use Filament\Facades\Filament;
use Illuminate\Support\Facades\Bus;

it('renders Monitoring → Operations index DB-only (no outbound HTTP, no background work)', function () {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    OperationRun::factory()->create([
        'managed_environment_id' => $tenant->getKey(),
        'type' => 'policy.sync',
        'status' => 'queued',
        'outcome' => 'pending',
        'initiator_name' => 'System',
    ]);

    $this->actingAs($user);

    Bus::fake();

    Filament::setTenant(null, true);

    assertNoOutboundHttp(function () use ($tenant) {
        $this->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id])
            ->get(\App\Support\OperationRunLinks::index())
            ->assertOk()
            ->assertDontSee('Total Operations (30 days)')
            ->assertDontSee('Active Operations')
            ->assertDontSee('Failed/Partial (7 days)')
            ->assertDontSee('Avg Duration (7 days)')
            ->assertSee('All')
            ->assertSee('Active')
            ->assertSee('Likely stale')
            ->assertSee('Terminal follow-up')
            ->assertSee('Succeeded')
            ->assertSee('Partial')
            ->assertSee('Failed');
    });

    Bus::assertNothingDispatched();
});

it('renders Monitoring → Operations detail DB-only (no outbound HTTP, no background work)', function () {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $run = OperationRun::factory()->create([
        'managed_environment_id' => $tenant->getKey(),
        'type' => 'policy.sync',
        'status' => 'queued',
        'outcome' => 'pending',
        'initiator_name' => 'System',
    ]);

    $this->actingAs($user);

    Bus::fake();

    Filament::setTenant(null, true);

    assertNoOutboundHttp(function () use ($tenant, $run) {
        $this->withSession([WorkspaceContext::SESSION_KEY => (int) $tenant->workspace_id])
            ->get(\App\Support\OperationRunLinks::tenantlessView($run))
            ->assertOk()
            ->assertSee(\App\Support\OperationRunLinks::identifier($run));
    });

    Bus::assertNothingDispatched();
});
