import datetime as dt
import pandas as pd

from app.screener.engine import ScreenContext
from app.screener.nodes.foreign_buy import ForeignBuy
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_higher_foreign_buy_gets_higher_score():
    asof = dt.date(2026, 5, 12)
    master = make_master(["A", "B"])
    prices = make_prices(["A", "B"], days=30, asof=asof)
    flow = make_flow(["A", "B"], days=30, asof=asof,
                     foreign_per_day={"A": 100_000_000, "B": 0})
    out = ForeignBuy().compute(_ctx(master, prices, flow), {"window_days": 5})
    assert out["A"] > out["B"]
    assert 0 <= out.min() <= out.max() <= 100


def test_all_zero_returns_50():
    asof = dt.date(2026, 5, 12)
    master = make_master(["A", "B"])
    prices = make_prices(["A", "B"], days=30, asof=asof)
    flow = make_flow(["A", "B"], days=30, asof=asof, foreign_per_day={"A": 0, "B": 0})
    out = ForeignBuy().compute(_ctx(master, prices, flow), {"window_days": 5})
    assert (out == 50.0).all()
