<?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([
        'tenant_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('/admin/operations')
            ->assertOk()
            ->assertDontSee('Total Runs (30 days)')
            ->assertDontSee('Active Runs')
            ->assertDontSee('Failed/Partial (7 days)')
            ->assertDontSee('Avg Duration (7 days)')
            ->assertSee('All')
            ->assertSee('Active')
            ->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([
        'tenant_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(route('admin.operations.view', ['run' => (int) $run->getKey()]))
            ->assertOk()
            ->assertSee('Operation run');
    });

    Bus::assertNothingDispatched();
});
