<?php

declare(strict_types=1);

use App\Models\OperationRun;
use App\Models\Tenant;
use App\Support\OpsUx\ActiveRuns;

it('returns false when tenant has no active runs', function (): void {
    $tenant = Tenant::factory()->create();

    OperationRun::factory()->create([
        'tenant_id' => $tenant->getKey(),
        'type' => 'inventory_sync',
        'status' => 'completed',
        'outcome' => 'succeeded',
        'initiator_name' => 'System',
    ]);

    expect(ActiveRuns::existForTenant($tenant))->toBeFalse();
});

it('returns true when tenant has queued runs', function (): void {
    $tenant = Tenant::factory()->create();

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

    expect(ActiveRuns::existForTenant($tenant))->toBeTrue();
});

it('returns true when tenant has running runs', function (): void {
    $tenant = Tenant::factory()->create();

    OperationRun::factory()->create([
        'tenant_id' => $tenant->getKey(),
        'type' => 'inventory_sync',
        'status' => 'running',
        'outcome' => 'pending',
        'initiator_name' => 'System',
    ]);

    expect(ActiveRuns::existForTenant($tenant))->toBeTrue();
});

it('is tenant scoped (other tenant active runs do not count)', function (): void {
    $tenantA = Tenant::factory()->create();
    $tenantB = Tenant::factory()->create();

    OperationRun::factory()->create([
        'tenant_id' => $tenantB->getKey(),
        'type' => 'inventory_sync',
        'status' => 'running',
        'outcome' => 'pending',
        'initiator_name' => 'System',
    ]);

    expect(ActiveRuns::existForTenant($tenantA))->toBeFalse();
});
