<?php

declare(strict_types=1);

use App\Models\PricingTier;
use App\Services\Billing\BalancePreflightService;
use Illuminate\Database\Eloquent\Collection;

function makeTiers(): Collection
{
    // 1 ступень: цена 50₽/лид (5000 копеек), безлимитная.
    return new Collection([
        new PricingTier(['tier_no' => 1, 'leads_in_tier' => null, 'price_per_lead_kopecks' => 5000, 'is_active' => true]),
    ]);
}

it('passes when balance covers full daily limit', function () {
    $service = new BalancePreflightService;
    // 2000₽ / 50₽ = 40 лидов capacity; нужно 30 → проходит.
    $result = $service->evaluate(
        balanceRub: '2000.00',
        deliveredInMonth: 0,
        requiredLeads: 30,
        tiers: makeTiers(),
    );

    expect($result->passes)->toBeTrue();
    expect($result->capacityLeads)->toBe(40);
    expect($result->requiredLeads)->toBe(30);
    expect($result->deficitLeads)->toBe(0);
});

it('fails when balance below full daily limit', function () {
    $service = new BalancePreflightService;
    // 1000₽ / 50₽ = 20 лидов capacity; нужно 30 → не проходит, дефицит 10.
    $result = $service->evaluate(
        balanceRub: '1000.00',
        deliveredInMonth: 0,
        requiredLeads: 30,
        tiers: makeTiers(),
    );

    expect($result->passes)->toBeFalse();
    expect($result->capacityLeads)->toBe(20);
    expect($result->deficitLeads)->toBe(10);
});

it('passes trivially when requiredLeads is zero', function () {
    $service = new BalancePreflightService;
    $result = $service->evaluate('0.00', 0, 0, makeTiers());
    expect($result->passes)->toBeTrue();
});
