"""Known-case tests for the Multibagger Potential Score (StockLab overhaul, Part 22)."""
from __future__ import annotations

from app.engines.multibagger import MultibaggerInputs, compute_multibagger_score
from app.engines.types import MarketCapBucket


def test_high_growth_small_cap_scores_well():
    inputs = MultibaggerInputs(
        revenue_growth_cagr_3y=0.35, revenue_growth_trend=0.04, operating_margin_trend=0.03,
        reinvestment_rate=0.25, share_count_growth_cagr_3y=-0.01, peg_ratio=0.8,
        market_cap_bucket=MarketCapBucket.SMALL,
    )
    result = compute_multibagger_score(inputs)
    assert result.value is not None
    assert result.value > 75.0


def test_mature_large_cap_low_growth_scores_poorly():
    inputs = MultibaggerInputs(
        revenue_growth_cagr_3y=0.02, revenue_growth_trend=-0.01, operating_margin_trend=0.0,
        reinvestment_rate=0.03, share_count_growth_cagr_3y=0.02, peg_ratio=3.5,
        market_cap_bucket=MarketCapBucket.LARGE,
    )
    result = compute_multibagger_score(inputs)
    assert result.value is not None
    assert result.value < 35.0


def test_no_inputs_is_none_not_zero():
    result = compute_multibagger_score(MultibaggerInputs())
    assert result.value is None
    assert result.components == []


def test_components_sum_weights_to_one_when_fully_populated():
    inputs = MultibaggerInputs(
        revenue_growth_cagr_3y=0.20, revenue_growth_trend=0.01, operating_margin_trend=0.01,
        reinvestment_rate=0.15, share_count_growth_cagr_3y=0.0, peg_ratio=1.5,
        market_cap_bucket=MarketCapBucket.MID,
    )
    result = compute_multibagger_score(inputs)
    assert abs(sum(c.weight for c in result.components) - 1.0) < 1e-6


ALL_TESTS = [obj for name, obj in list(globals().items()) if name.startswith("test_") and callable(obj)]

if __name__ == "__main__":
    passed, failed = 0, []
    for fn in ALL_TESTS:
        try:
            fn()
            passed += 1
            print(f"PASS  {fn.__name__}")
        except AssertionError as e:
            failed.append(fn.__name__)
            print(f"FAIL  {fn.__name__}: {e}")
    print(f"\n{passed}/{len(ALL_TESTS)} passed")
    if failed:
        raise SystemExit(1)
