from __future__ import annotations

import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

TRADE_DATE = "20240902"

RAW = {
    "trade_cal": [
        {"exchange": "SSE", "cal_date": "20240902", "is_open": 1, "pretrade_date": "20240830"},
        {"exchange": "SSE", "cal_date": "20240903", "is_open": 1, "pretrade_date": "20240902"},
        {"exchange": "SSE", "cal_date": "20240907", "is_open": 0, "pretrade_date": "20240906"},
    ],
    "stock_basic": [
        {"ts_code": "600000.SH", "symbol": "600000", "name": "浦发银行", "area": "上海", "industry": "银行", "market": "主板", "list_status": "L", "list_date": "19991110"},
        {"ts_code": "000001.SZ", "symbol": "000001", "name": "平安银行", "area": "深圳", "industry": "银行", "market": "主板", "list_status": "L", "list_date": "19910403"},
    ],
    "daily": [
        {"ts_code": "600000.SH", "trade_date": "20240902", "open": 10.11, "high": 10.25, "low": 10.01, "close": 10.20, "pct_chg": 1.2345, "vol": 1000.0, "amount": 2000.0},
        {"ts_code": "000001.SZ", "trade_date": "20240902", "open": 11.00, "high": 11.20, "low": 10.90, "close": 11.10, "pct_chg": -0.5, "vol": 2000.0, "amount": 4000.0},
    ],
    "daily_basic": [
        {"ts_code": "600000.SH", "trade_date": "20240902", "turnover_rate": 1.2, "volume_ratio": 0.8, "total_mv": 1000.0, "circ_mv": 800.0, "pe_ttm": 5.1, "pb": 0.6, "ps_ttm": 1.1, "dv_ttm": 4.0},
        {"ts_code": "000001.SZ", "trade_date": "20240902", "turnover_rate": 2.2, "volume_ratio": 1.1, "total_mv": 2000.0, "circ_mv": 1500.0, "pe_ttm": 6.2, "pb": 0.7, "ps_ttm": 1.2, "dv_ttm": None},
    ],
    "adj_factor": [
        {"ts_code": "600000.SH", "trade_date": "20240902", "adj_factor": 1.1},
        {"ts_code": "000001.SZ", "trade_date": "20240902", "adj_factor": 2.0},
    ],
    "index_daily": [
        {"ts_code": "000001.SH", "trade_date": "20240902", "open": 2700, "high": 2750, "low": 2690, "close": 2740, "pct_chg": 0.5, "vol": 3000.0, "amount": 500000.0},
        {"ts_code": "399001.SZ", "trade_date": "20240902", "open": 8000, "high": 8100, "low": 7900, "close": 8050, "pct_chg": 0.4, "vol": 2000.0, "amount": 300000.0},
        {"ts_code": "399006.SZ", "trade_date": "20240902", "open": 1600, "high": 1620, "low": 1580, "close": 1610, "pct_chg": 0.3, "vol": 1000.0, "amount": 100000.0},
        {"ts_code": "000300.SH", "trade_date": "20240902", "open": 3500, "high": 3550, "low": 3480, "close": 3520, "pct_chg": 0.2, "vol": 1500.0, "amount": 200000.0},
    ],
    "moneyflow": [
        {"ts_code": "600000.SH", "trade_date": "20240902", "buy_sm_amount": 10, "sell_sm_amount": 8, "buy_md_amount": 20, "sell_md_amount": 15, "buy_lg_amount": 30, "sell_lg_amount": 25, "buy_elg_amount": 40, "sell_elg_amount": 35, "net_mf_amount": 17},
        {"ts_code": "000001.SZ", "trade_date": "20240902", "buy_sm_amount": 11, "sell_sm_amount": 9, "buy_md_amount": 21, "sell_md_amount": 16, "buy_lg_amount": 31, "sell_lg_amount": 26, "buy_elg_amount": 41, "sell_elg_amount": 36, "net_mf_amount": 18},
    ],
    "stk_auction": [
        {"ts_code": "600000.SH", "trade_date": "20240902", "vol": 100, "price": 10.15, "amount": 1500000, "pre_close": 10.00, "turnover_rate": 0.1, "volume_ratio": 1.2, "float_share": 2000},
        {"ts_code": "000001.SZ", "trade_date": "20240902", "vol": 80, "price": 11.05, "amount": 1200000, "pre_close": 11.10, "turnover_rate": 0.2, "volume_ratio": 0.9, "float_share": 1800},
    ],
}


def fake_transport(api_name: str, params: dict, fields: str):
    if api_name == "index_daily":
        code = params.get("ts_code")
        rows = [row for row in RAW["index_daily"] if row["ts_code"] == code]
        trade_date = str(params.get("trade_date") or "")
        start = str(params.get("start_date") or "")
        end = str(params.get("end_date") or "")
        if trade_date:
            rows = [row for row in rows if row["trade_date"] == trade_date]
        if start:
            rows = [row for row in rows if row["trade_date"] >= start]
        if end:
            rows = [row for row in rows if row["trade_date"] <= end]
        return rows
    if api_name == "trade_cal":
        start = str(params.get("start_date") or "")
        end = str(params.get("end_date") or "99999999")
        return [row for row in RAW["trade_cal"] if start <= row["cal_date"] <= end]
    return list(RAW.get(api_name) or [])
