"""
DataService 使用示例 — 读取各周期 K 线并打印

用法：
    python example/data.py
"""

import asyncio
import sys
from pathlib import Path

# 确保项目根目录在 sys.path 中，以便使用 engine 包导入
_project_root = Path(__file__).resolve().parent.parent.parent
if str(_project_root) not in sys.path:
    sys.path.insert(0, str(_project_root))

from engine.data import DataService
from engine.common.config import config


async def main():

    print(config.db)

    ds = DataService(config.db)
    await ds.connect()

    try:
        # 1. 查看可用交易对
        symbols = await ds.fetch_available_symbols("1m")
        print(f"可用交易对: {symbols}")

        # 2. 各周期读取最新 3 条 BTCUSDT K 线
        target = "BTCUSDT"
        for interval in ["1m", "5m", "15m", "30m", "1h", "4h", "1d"]:
            klines = await ds.fetch_klines(
                symbol=target,
                interval=interval,
                limit=3,
            )
            print(f"\n{'─' * 70}")
            print(f"  [{interval}] {target}  —  {len(klines)} 条")
            print(f"{'─' * 70}")
            for k in klines:
                print(
                    f"  {k.open_time:.0f}  O={k.open:>12.4f}  H={k.high:>12.4f}"
                    f"  L={k.low:>12.4f}  C={k.close:>12.4f}"
                    f"  V={k.volume:>10.4f}  trades={k.trade_count}"
                )

        # 3. 日期范围
        start, end = await ds.fetch_symbol_date_range(target, "1d")
        print(f"\n{target} 1d 数据范围: {start} ~ {end}")

        print("\n完成")

    finally:
        await ds.close()


if __name__ == "__main__":
    asyncio.run(main())
