from decimal import Decimal

from src.strategies.v76_hl_confirmed_squeeze_hybrid import MarketContext, build_v76_order_intent, strict_anti_chase_blockers


def test_v76_strategy_returns_order_intent_without_exchange_calls():
    context = MarketContext(
        coin="BTC",
        symbol="BTC/USDC:USDC",
        current_price=Decimal("100000"),
        recent_high=Decimal("99800"),
        sma_fast=Decimal("99750"),
        sma_slow=Decimal("99500"),
        volume_24h=Decimal("100000000"),
        breadth_positive_candidates=4,
        relative_strength_rank=1,
        baseline_price=Decimal("99000"),
        wallet_equity_usdc=Decimal("500"),
    )

    intent = build_v76_order_intent(context, client_order_id="v76-BTC-1")

    assert intent is not None
    assert intent.coin == "BTC"
    assert intent.reason == "confirmed_squeeze_breakout"
    assert intent.stop_loss is not None
    assert intent.risk_usd == Decimal("1.25")


def test_v76_strategy_rejects_wld_when_leakage_gate_blocks():
    context = MarketContext(
        coin="WLD",
        symbol="WLD/USDC:USDC",
        current_price=Decimal("2.00"),
        recent_high=Decimal("1.98"),
        sma_fast=Decimal("1.99"),
        sma_slow=Decimal("1.95"),
        volume_24h=Decimal("100000000"),
        breadth_positive_candidates=4,
        relative_strength_rank=1,
        baseline_price=Decimal("1.90"),
        wallet_equity_usdc=Decimal("500"),
        coin_leakage_blocked=True,
    )

    assert build_v76_order_intent(context, client_order_id="v76-WLD-1") is None


def test_v76_strict_anti_chase_blocks_overextended_no_retest():
    context = MarketContext(
        coin="BTC",
        symbol="BTC/USDC:USDC",
        current_price=Decimal("104.50"),
        recent_high=Decimal("104.40"),
        sma_fast=Decimal("103.90"),
        sma_slow=Decimal("102.80"),
        volume_24h=Decimal("100000000"),
        breadth_positive_candidates=4,
        relative_strength_rank=1,
        baseline_price=Decimal("100"),
        wallet_equity_usdc=Decimal("500"),
        strict_anti_chase=True,
        retest_low=Decimal("103.10"),
        atr_pct=Decimal("1.20"),
        move_15m_pct=Decimal("2.40"),
        move_1h_pct=Decimal("4.00"),
        pullback_from_high_pct=Decimal("0.05"),
    )

    blockers = strict_anti_chase_blockers(context)

    assert "anti_chase_overextended_no_retest" in blockers
    assert build_v76_order_intent(context, client_order_id="v76-BTC-chase") is None


def test_v76_strict_anti_chase_allows_retest_reclaim():
    context = MarketContext(
        coin="BTC",
        symbol="BTC/USDC:USDC",
        current_price=Decimal("100"),
        recent_high=Decimal("101"),
        sma_fast=Decimal("99.80"),
        sma_slow=Decimal("99.20"),
        volume_24h=Decimal("100000000"),
        breadth_positive_candidates=4,
        relative_strength_rank=1,
        baseline_price=Decimal("100.50"),
        wallet_equity_usdc=Decimal("500"),
        strict_anti_chase=True,
        retest_low=Decimal("99.30"),
        atr_pct=Decimal("1.20"),
        move_15m_pct=Decimal("0.30"),
        move_1h_pct=Decimal("0.90"),
        pullback_from_high_pct=Decimal("0.99"),
    )

    assert strict_anti_chase_blockers(context) == []
    intent = build_v76_order_intent(context, client_order_id="v76-BTC-retest")
    assert intent is not None
    assert intent.reason == "survival_rebound_sma_reclaim"


def test_v76_strict_anti_chase_blocks_vwap_pump_and_wick_exhaustion():
    context = MarketContext(
        coin="BTC",
        symbol="BTC/USDC:USDC",
        current_price=Decimal("105"),
        recent_high=Decimal("106"),
        sma_fast=Decimal("104"),
        sma_slow=Decimal("103"),
        volume_24h=Decimal("100000000"),
        breadth_positive_candidates=4,
        relative_strength_rank=1,
        baseline_price=Decimal("100"),
        wallet_equity_usdc=Decimal("500"),
        strict_anti_chase=True,
        retest_low=Decimal("103.50"),
        atr_pct=Decimal("1.0"),
        move_15m_pct=Decimal("2.7"),
        move_1h_pct=Decimal("4.0"),
        pullback_from_high_pct=Decimal("0.95"),
        vwap_distance_pct=Decimal("1.35"),
        upper_wick_pct=Decimal("0.80"),
        candle_body_pct=Decimal("0.30"),
    )

    blockers = strict_anti_chase_blockers(context)

    assert "anti_chase_vwap_distance_too_high" in blockers
    assert "anti_chase_atr_pump" in blockers
    assert "anti_chase_upper_wick_rejection" in blockers
    assert "anti_chase_exhaustion_candle" in blockers
    assert build_v76_order_intent(context, client_order_id="v76-BTC-exhaustion") is None


def test_v76_strict_anti_chase_blocks_real_derivatives_crowding():
    context = MarketContext(
        coin="ETH",
        symbol="ETH/USDC:USDC",
        current_price=Decimal("100"),
        recent_high=Decimal("101"),
        sma_fast=Decimal("99.80"),
        sma_slow=Decimal("99.20"),
        volume_24h=Decimal("100000000"),
        breadth_positive_candidates=4,
        relative_strength_rank=1,
        baseline_price=Decimal("100.50"),
        wallet_equity_usdc=Decimal("500"),
        strict_anti_chase=True,
        retest_low=Decimal("99.30"),
        atr_pct=Decimal("1.20"),
        move_15m_pct=Decimal("0.30"),
        move_1h_pct=Decimal("0.90"),
        pullback_from_high_pct=Decimal("0.99"),
        funding_rate_hourly_pct=Decimal("0.006"),
        open_interest_change_pct=Decimal("14"),
        premium_pct=Decimal("0.12"),
    )

    blockers = strict_anti_chase_blockers(context)

    assert "anti_chase_oi_funding_crowded" in blockers
    assert "anti_chase_premium_too_high" in blockers
    assert build_v76_order_intent(context, client_order_id="v76-ETH-crowded") is None
