#!/usr/bin/env python3
"""
从CSV导入数据到vnpy sqlite数据库
容器里pandas可以读csv，不需要额外依赖
"""

import pandas as pd
from vnpy.trader.object import BarData
from vnpy.trader.constant import Exchange, Interval
from vnpy.trader.database import get_database
from datetime import datetime

def main():
    print("🚀 [IMPORT] 开始从CSV导入 510300.SSE 日线数据")
    
    # 读取CSV
    csv_file = "/tmp/510300_daily.csv"
    df = pd.read_csv(csv_file)
    
    # 转换trade_date为datetime
    df['trade_date'] = pd.to_datetime(df['trade_date'])
    
    print(f"✅ [IMPORT] 读取CSV完成，共 {len(df)} 行")
    print(f"   时间范围: {df['trade_date'].min()} ~ {df['trade_date'].max()}")
    
    # 转换为BarData
    print(f"🔧 [IMPORT] 转换为 BarData...")
    bars = []
    for idx, row in df.iterrows():
        # vnpy需要datetime对象，不是pandas Timestamp，并且处理时区问题
        dt = row["trade_date"]
        if hasattr(dt, 'to_pydatetime'):
            dt = dt.to_pydatetime()
        bar = BarData(
            symbol="510300",
            exchange=Exchange.SSE,
            interval=Interval.DAILY,
            datetime=dt,
            open_price=row["open"],
            high_price=row["high"],
            low_price=row["low"],
            close_price=row["close"],
            volume=row["volume"],
            turnover=row["amount"],
            gateway_name="DATA"
        )
        bars.append(bar)
    
    print(f"✅ [IMPORT] 转换完成，共 {len(bars)} 个BarData")
    
    # 写入数据库
    print(f"💾 [IMPORT] 写入数据库...")
    db = get_database()
    
    # 保存数据
    db.save_bar_data(bars)
    print(f"✅ [IMPORT] 保存完成，共写入 {len(bars)} 条")
    
    # 验证
    start_target = datetime(2021, 1, 1)
    end_target = datetime(2026, 3, 1)
    target_bars = db.load_bar_data("510300", Exchange.SSE, Interval.DAILY, start_target, end_target)
    print(f"✅ [IMPORT] 验证目标区间 2021-01-01 ~ 2026-03-01: 找到 {len(target_bars)} 条数据")
    
    if len(target_bars) > 0:
        print(f"   第一条: {target_bars[0].datetime}")
        print(f"   最后一条: {target_bars[-1].datetime}")
        print(f"   ✅ 满足需求！导入成功！")
    
    print("\n🎉 [IMPORT] 导入完成！")

if __name__ == '__main__':
    main()
