import datetime as dt

import pandas as pd

from app.screener.nodes.hygiene import HygieneGate
from app.screener.engine import ScreenContext
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_filter_excludes_small_cap():
    g = HygieneGate()
    ctx = _ctx(
        make_master(["A", "B"], market_caps={"A": 1_000_000_000, "B": 100_000_000_000}),
        make_prices(["A", "B"], days=30),
        make_flow(["A", "B"], days=30),
    )
    out = g.filter(ctx, {**g.default_params, "min_listed_days": 0})
    assert list(out) == ["B"]


def test_filter_excludes_preferred():
    g = HygieneGate()
    ctx = _ctx(
        make_master(["A", "B"], preferred={"B"}),
        make_prices(["A", "B"], days=30),
        make_flow(["A", "B"], days=30),
    )
    out = g.filter(ctx, {**g.default_params, "min_listed_days": 0})
    assert list(out) == ["A"]


def test_filter_excludes_low_value():
    g = HygieneGate()
    prices = make_prices(["A", "B"], days=30)
    prices.loc[prices["ticker"] == "A", "value"] = 100_000     # 매우 작음
    ctx = _ctx(make_master(["A", "B"]), prices, make_flow(["A", "B"], days=30))
    out = g.filter(ctx, {**g.default_params, "min_listed_days": 0})
    assert list(out) == ["B"]
