<?php

declare(strict_types=1);

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

uses(DatabaseTransactions::class);

test('PricingTier can be created via factory', function () {
    $t = PricingTier::factory()->create();
    expect($t->tier_no)->toBeInt()->toBeBetween(1, 7);
    expect($t->price_per_lead_kopecks)->toBeInt()->toBeGreaterThan(0);
});

test('priceRubles accessor converts kopecks to rubles', function () {
    $t = PricingTier::factory()->create([
        'tier_no' => 1,
        'price_per_lead_kopecks' => 6000,
    ]);
    expect($t->price_rubles)->toBe(60.0);
});

test('scopeActive returns only is_active=true with effective_from <= today', function () {
    // baseline: PricingTierSeeder seed'ит 7 ступеней с effective_from='1970-01-01' через
    // DatabaseSeeder (Plan 4 Task 1). Тест проверяет delta от baseline после factory-create.
    $baseline = PricingTier::active()->count();

    PricingTier::factory()->create([
        'is_active' => true,
        'effective_from' => now()->subDay(),
        'tier_no' => 1,
    ]);
    PricingTier::factory()->create([
        'is_active' => false,
        'effective_from' => now()->subDay(),
        'tier_no' => 2,
    ]);
    PricingTier::factory()->create([
        'is_active' => true,
        'effective_from' => now()->addDay(),
        'tier_no' => 3,
    ]);

    expect(PricingTier::active()->count())->toBe($baseline + 1);
});

test('current() returns today active tier set keyed by tier_no', function () {
    foreach (range(1, 7) as $n) {
        PricingTier::factory()->create([
            'tier_no' => $n,
            'is_active' => true,
            'effective_from' => now()->subDay(),
            'leads_in_tier' => $n === 7 ? null : 100,
            'price_per_lead_kopecks' => 7000 - ($n * 500),
        ]);
    }

    $current = PricingTier::current();
    expect($current)->toHaveCount(7);
    expect($current[1]->price_per_lead_kopecks)->toBe(6500);
    expect($current[7]->leads_in_tier)->toBeNull();
});
