<?php

declare(strict_types=1);

use App\Services\LeadDistributor;
use Illuminate\Support\Collection;
use Random\Engine\Mt19937;
use Random\Randomizer;

function projects(int $n): Collection
{
    return collect(range(1, $n))->map(fn (int $i) => (object) ['id' => $i]);
}

it('returns all when eligible count <= cap (3)', function (): void {
    $d = new LeadDistributor;
    expect($d->selectRecipients(projects(2)))->toHaveCount(2)
        ->and($d->selectRecipients(projects(3)))->toHaveCount(3);
});

it('caps at 3 when more eligible', function (): void {
    $d = new LeadDistributor;
    expect($d->selectRecipients(projects(7)))->toHaveCount(3);
});

it('selection is a subset of eligible and deterministic under seeded RNG', function (): void {
    $eligible = projects(7);
    $d = new LeadDistributor(new Randomizer(new Mt19937(42)));
    $picked = $d->selectRecipients($eligible)->pluck('id')->all();

    expect($picked)->toHaveCount(3)
        ->and(collect($picked)->every(fn ($id) => $id >= 1 && $id <= 7))->toBeTrue();

    // тот же seed → тот же выбор
    $d2 = new LeadDistributor(new Randomizer(new Mt19937(42)));
    expect($d2->selectRecipients($eligible)->pluck('id')->all())->toBe($picked);
});
