"""Synthetic fixtures for screener tests — no DB / no FDR / no naver."""

import datetime as dt
import pandas as pd


def make_master(tickers: list[str], market_caps: dict | None = None,
                preferred: set | None = None, managed: set | None = None) -> pd.DataFrame:
    market_caps = market_caps or {t: 100_000_000_000 for t in tickers}
    preferred = preferred or set()
    managed = managed or set()
    return pd.DataFrame([
        {
            "ticker": t,
            "name": f"테스트{t}",
            "market": "KOSPI",
            "market_cap": market_caps.get(t),
            "is_managed": int(t in managed),
            "is_preferred": int(t in preferred),
            "is_spac": 0,
            "listed_date": None,
        }
        for t in tickers
    ]).set_index("ticker")


def make_prices(tickers: list[str], days: int = 260, start_close: int = 50000,
                trend_pct: float = 0.0,
                asof: dt.date = dt.date(2026, 5, 12)) -> pd.DataFrame:
    """trend_pct: 일별 종가 등락률(%). 양수면 상승 추세."""
    rows = []
    for t in tickers:
        close = start_close
        for i in range(days):
            day_idx = days - 1 - i              # asof가 마지막
            date = asof - dt.timedelta(days=day_idx)
            high = int(close * 1.012)
            low  = int(close * 0.988)
            rows.append({
                "ticker": t, "date": date.isoformat(),
                "open": close, "high": high, "low": low, "close": close,
                "volume": 1_000_000, "value": close * 1_000_000,
            })
            close = int(close * (1 + trend_pct / 100))
    return pd.DataFrame(rows)


def make_flow(tickers: list[str], days: int = 260,
              foreign_per_day: dict | None = None,
              asof: dt.date = dt.date(2026, 5, 12)) -> pd.DataFrame:
    foreign_per_day = foreign_per_day or {t: 0 for t in tickers}
    rows = []
    for t in tickers:
        for i in range(days):
            day_idx = days - 1 - i
            date = asof - dt.timedelta(days=day_idx)
            rows.append({
                "ticker": t, "date": date.isoformat(),
                "foreign_net": foreign_per_day.get(t, 0),
                "institution_net": 0,
            })
    return pd.DataFrame(rows)


def make_kospi(days: int = 260, start: int = 2500, trend_pct: float = 0.0,
               asof: dt.date = dt.date(2026, 5, 12)) -> pd.Series:
    values = []
    dates = []
    v = start
    for i in range(days):
        day_idx = days - 1 - i
        d = asof - dt.timedelta(days=day_idx)
        dates.append(d.isoformat())
        values.append(v)
        v = v * (1 + trend_pct / 100)
    return pd.Series(values, index=dates, name="kospi")
