"""Tests for the pricing engine."""

from discourse_toll.pricing import PricingEngine, PricingConfig, CooldownConfig


def test_base_price():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    result = engine.calculate("a1", "t1")
    assert result.sats == 1


def test_progressive_pricing():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    engine.calculate("a1", "t1")
    result = engine.calculate("a1", "t1")
    assert result.sats > 1
    assert result.breakdown["prior_actions_in_context"] == 1


def test_progressive_cap():
    engine = PricingEngine(PricingConfig(progressive_cap=10, cooldown=CooldownConfig(enabled=False)))
    for _ in range(20):
        engine.calculate("a1", "t1")
    result = engine.calculate("a1", "t1", dry_run=True)
    assert result.sats <= 10


def test_independent_agents():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    for _ in range(3):
        engine.calculate("a1", "t1")
    r1 = engine.calculate("a1", "t1", dry_run=True)
    r2 = engine.calculate("a2", "t1", dry_run=True)
    assert r1.sats > r2.sats


def test_independent_contexts():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    for _ in range(3):
        engine.calculate("a1", "t1")
    r1 = engine.calculate("a1", "t1", dry_run=True)
    r2 = engine.calculate("a1", "t2", dry_run=True)
    assert r1.sats > r2.sats


def test_trust_free():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    result = engine.calculate("a1", "t1", trust_score=85)
    assert result.sats == 0


def test_trust_discount():
    engine = PricingEngine(PricingConfig(base_sats=10, cooldown=CooldownConfig(enabled=False)))
    result = engine.calculate("a1", "t1", trust_score=50)
    assert 0 < result.sats < 10


def test_trust_no_discount():
    engine = PricingEngine(PricingConfig(base_sats=10, cooldown=CooldownConfig(enabled=False)))
    result = engine.calculate("a1", "t1", trust_score=10)
    assert result.sats == 10


def test_dry_run():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    engine.calculate("a1", "t1", dry_run=True)
    assert engine.get_activity_count("a1", "t1") == 0


def test_non_dry_run():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    engine.calculate("a1", "t1", dry_run=False)
    assert engine.get_activity_count("a1", "t1") == 1


def test_cleanup():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    engine.calculate("a1", "t1")
    engine._activity["t1"][0]["timestamp"] = 0  # Very old
    engine.cleanup(86_400_000)
    assert engine.get_activity_count("a1", "t1") == 0


def test_stats():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    engine.calculate("a1", "t1")
    engine.calculate("a2", "t1")
    engine.calculate("a1", "t2")
    stats = engine.stats()
    assert stats["contexts"] == 2
    assert stats["agents"] == 2
    assert stats["total_actions"] == 3


def test_reset():
    engine = PricingEngine(PricingConfig(cooldown=CooldownConfig(enabled=False)))
    engine.calculate("a1", "t1")
    engine.reset()
    assert engine.stats()["total_actions"] == 0


def test_spam_gets_expensive():
    engine = PricingEngine(PricingConfig(
        base_sats=1, progressive_multiplier=2, progressive_cap=100,
        cooldown=CooldownConfig(enabled=False),
    ))
    total = sum(engine.calculate("spammer", "t1").sats for _ in range(10))
    assert total > 50


def test_cross_thread_spam():
    engine = PricingEngine(PricingConfig(base_sats=1, cooldown=CooldownConfig(enabled=False)))
    total = sum(engine.calculate("spammer", f"t-{i}").sats for i in range(50))
    assert total == 50
