<?php

declare(strict_types=1);

use App\Models\PricingTier;
use Illuminate\Foundation\Testing\DatabaseTransactions;

uses(DatabaseTransactions::class);

it('returns current active pricing ladder without auth', function () {
    PricingTier::query()->delete();
    $tiers = [
        [1, 100, 7000], [2, 250, 6500], [3, 500, 6000], [4, 1000, 5500],
        [5, 2500, 5000], [6, 5000, 4500], [7, null, 4000],
    ];
    foreach ($tiers as [$no, $leads, $kop]) {
        PricingTier::query()->create([
            'tier_no' => $no,
            'leads_in_tier' => $leads,
            'price_per_lead_kopecks' => $kop,
            'is_active' => true,
            'effective_from' => '2026-06-22',
        ]);
    }

    $res = $this->getJson('/api/public/pricing');

    $res->assertOk();
    $res->assertJsonCount(7, 'tiers');
    $res->assertJsonPath('tiers.0.tier_no', 1);
    $res->assertJsonPath('tiers.0.price_rub', '70.00');
    $res->assertJsonPath('tiers.0.leads_in_tier', 100);
    $res->assertJsonPath('tiers.6.tier_no', 7);
    $res->assertJsonPath('tiers.6.leads_in_tier', null);
    $res->assertJsonPath('tiers.6.price_rub', '40.00');
});

it('prefers the latest effective ladder over older one', function () {
    PricingTier::query()->delete();
    PricingTier::query()->create(['tier_no' => 1, 'leads_in_tier' => 100, 'price_per_lead_kopecks' => 50000, 'is_active' => true, 'effective_from' => '1970-01-01']);
    PricingTier::query()->create(['tier_no' => 1, 'leads_in_tier' => 100, 'price_per_lead_kopecks' => 7000, 'is_active' => true, 'effective_from' => '2026-06-22']);

    $this->getJson('/api/public/pricing')->assertJsonPath('tiers.0.price_rub', '70.00');
});
