from __future__ import annotations

from config import BotConfig
from position_sizing import calculate_position_size


def test_risk_based_position_size_uses_wallet_risk_and_stop_distance() -> None:
    cfg = BotConfig(
        wallet_equity_usdc=500.0,
        risk_per_trade_pct=1.0,
        position_sizing_mode="risk",
        max_position_margin_pct=25.0,
        max_position_notional_pct=100.0,
        min_order_notional_usd=12.0,
    )

    decision = calculate_position_size(
        coin="ETH",
        current_price=100.0,
        leverage=3.0,
        stop_distance_pct=2.0,
        cfg=cfg,
    )

    assert decision.notional_usd == 250.0
    assert round(decision.margin_usd, 4) == 83.3333
    assert decision.size == 2.5
    assert decision.risk_usd == 5.0
    assert decision.capped_by == "risk"


def test_position_size_respects_margin_cap_before_live_scaling() -> None:
    cfg = BotConfig(
        wallet_equity_usdc=500.0,
        risk_per_trade_pct=2.0,
        position_sizing_mode="risk",
        max_position_margin_pct=10.0,
        max_position_notional_pct=100.0,
        min_order_notional_usd=12.0,
    )

    decision = calculate_position_size(
        coin="ORDI",
        current_price=50.0,
        leverage=3.0,
        stop_distance_pct=2.0,
        cfg=cfg,
    )

    assert decision.notional_usd == 150.0
    assert decision.margin_usd == 50.0
    assert decision.size == 3.0
    assert decision.capped_by == "margin_cap"


def test_fixed_position_size_keeps_research_sampler_compatibility() -> None:
    cfg = BotConfig(position_sizing_mode="fixed", trade_size_usd=15.0, min_order_notional_usd=12.0)

    decision = calculate_position_size(
        coin="SOL",
        current_price=25.0,
        leverage=3.0,
        stop_distance_pct=2.0,
        cfg=cfg,
    )

    assert decision.notional_usd == 15.0
    assert decision.size == 0.6
    assert decision.capped_by == "fixed"
