import datetime as dt
import pandas as pd

from app.screener.engine import ScreenContext
from app.screener.position_sizer import compute_atr_wilder, plan_positions
from app.screener._test_fixtures import make_master, make_prices, make_flow


def _ctx(master, prices, flow):
    return ScreenContext(master=master, prices=prices, flow=flow,
                         kospi=pd.Series(dtype=float, name="kospi"),
                         asof=dt.date(2026, 5, 12))


def test_atr_wilder_positive_and_smooth():
    df = make_prices(["A"], days=30)
    atr = compute_atr_wilder(df[df["ticker"] == "A"], window=14)
    assert atr > 0


def test_plan_positions_returns_entry_stop_target():
    asof = dt.date(2026, 5, 12)
    master = make_master(["A"])
    prices = make_prices(["A"], days=30, asof=asof, start_close=50000)
    flow = make_flow(["A"], days=30, asof=asof)
    ctx = _ctx(master, prices, flow)
    sizing = plan_positions(ctx, ["A"], {"atr_window": 14, "atr_stop_mult": 2.0, "rr_ratio": 2.0})

    row = sizing["A"]
    assert row["entry_price"] > 0
    assert row["stop_price"] < row["entry_price"]
    assert row["target_price"] > row["entry_price"]
    assert row["atr14"] > 0
