<?php

declare(strict_types=1);

use App\Notifications\OperationRunQueued;
use App\Services\OperationRunService;
use Filament\Facades\Filament;

it('emits at most one queued database notification per newly created run when explicitly enabled', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

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

    /** @var OperationRunService $service */
    $service = app(OperationRunService::class);

    $run = $service->ensureRun(
        tenant: $tenant,
        type: 'policy.sync',
        inputs: ['scope' => 'all'],
        initiator: $user,
    );

    $service->dispatchOrFail($run, function (): void {
        // no-op (dispatch succeeded)
    }, emitQueuedNotification: true);

    expect($user->notifications()->count())->toBe(1);
    $this->assertDatabaseHas('notifications', [
        'notifiable_id' => $user->getKey(),
        'notifiable_type' => $user->getMorphClass(),
        'type' => OperationRunQueued::class,
    ]);
})->group('ops-ux');
