from decimal import Decimal

from src.execution.cost_model import CostModel, TradeCostInput
from src.execution.paper_executor import PaperExecutor
from src.execution.order_intent import OrderIntent


def _intent():
    return OrderIntent(
        strategy_id="candidate_v76_hl_confirmed_squeeze_hybrid",
        symbol="BTC/USDC:USDC",
        coin="BTC",
        side="buy",
        reduce_only=False,
        order_type="market",
        tif="Ioc",
        size=Decimal("0.001"),
        price=None,
        trigger_price=None,
        stop_loss=Decimal("99000"),
        take_profit=None,
        client_order_id="v76-BTC-cost-1",
        reason="cost_model",
        risk_usd=Decimal("1.25"),
        estimated_notional_usd=Decimal("100"),
    )


def test_cost_model_blocks_expected_move_below_3x_roundtrip_cost():
    model = CostModel(taker_fee_rate=Decimal("0.00045"), min_slippage_pct=Decimal("0.02"))
    decision = model.estimate(
        TradeCostInput(
            notional_usd=Decimal("100"),
            expected_move_pct=Decimal("0.10"),
            half_spread_pct=Decimal("0.03"),
            depth_penalty_pct=Decimal("0.01"),
            hold_hours=Decimal("0"),
        )
    )

    assert decision.allowed is False
    assert decision.reason == "expected_move_below_3x_roundtrip_cost"
    assert decision.roundtrip_cost_pct > Decimal("0")


def test_paper_executor_reports_realistic_cost_fields():
    executor = PaperExecutor(cost_model=CostModel())

    fill = executor.execute(_intent(), mark_price=Decimal("100000"), half_spread_pct=Decimal("0.02"), expected_move_pct=Decimal("1.0"))

    assert fill.entry_fee_usd > 0
    assert fill.spread_cost_usd > 0
    assert fill.slippage_cost_usd > 0
    assert fill.estimated_roundtrip_cost_pct > 0
    assert fill.blocked_by_cost is False
