from __future__ import annotations

from decimal import Decimal

from src.execution.size_impact_curve import analyze_size_curve


def _book():
    return {"levels": [
        [{"px": "99.99", "sz": "0.20"}, {"px": "99.95", "sz": "1.00"}],
        [{"px": "100.01", "sz": "0.20"}, {"px": "100.05", "sz": "1.00"}],
    ]}


def test_size_curve_uses_actual_levels_and_risk_cap():
    report = analyze_size_curve(
        _book(),
        notionals=(Decimal("10"), Decimal("15"), Decimal("25"), Decimal("50"), Decimal("100")),
        expected_move_pct=Decimal("0.80"),
        risk_budget_usd=Decimal("0.12"),
        stop_distance_pct=Decimal("0.75"),
    )
    assert report["mid_price"] == "100.00"
    assert [row["notional_usd"] for row in report["rows"]] == ["10", "15", "25", "50", "100"]
    assert Decimal(report["rows"][0]["buy"]["impact_pct"]) == Decimal("0.0100")
    assert Decimal(report["rows"][2]["buy"]["impact_pct"]) > Decimal(report["rows"][0]["buy"]["impact_pct"])
    assert report["rows"][0]["risk_allowed"] is True
    assert report["rows"][2]["risk_allowed"] is False
    assert report["max_recommended_notional_usd"] == "15"
    assert report["read_only"] is True
    assert report["live_order_allowed"] is False


def test_size_curve_blocks_when_depth_is_insufficient():
    report = analyze_size_curve(
        _book(), notionals=(Decimal("10"), Decimal("1000")), expected_move_pct=Decimal("1"),
        risk_budget_usd=Decimal("10"), stop_distance_pct=Decimal("1"),
    )
    assert report["rows"][1]["buy"]["depth_sufficient"] is False
    assert report["rows"][1]["cost_allowed"] is False
