from typing import List, Optional
from src.jixia.engines.data_abstraction import DataProvider
from src.jixia.models.financial_data_models import StockQuote, HistoricalPrice, CompanyProfile, FinancialNews
from src.jixia.engines.openbb_engine import OpenBBEngine

class OpenBBDataProvider(DataProvider):
    """OpenBB引擎适配器"""
    
    def __init__(self):
        self.engine = OpenBBEngine()
        self._name = "OpenBB"
        self._priority = 1  # 最高优先级
    
    def get_quote(self, symbol: str) -> Optional[StockQuote]:
        result = self.engine.get_immortal_data("吕洞宾", "price", symbol)
        if result.success and result.data:
            # 解析OpenBB返回的数据并转换为StockQuote
            # 注意：这里需要根据OpenBB实际返回的数据结构进行调整
            data = result.data
            if isinstance(data, list) and len(data) > 0:
                item = data[0]  # 取第一条数据
            elif hasattr(data, '__dict__'):
                item = data
            else:
                item = {}
                
            # 提取价格信息（根据openbb_stock_data.py中的字段）
            price = 0
            if hasattr(item, 'close'):
                price = float(item.close)
            elif isinstance(item, dict) and 'close' in item:
                price = float(item['close'])
            
            volume = 0
            if hasattr(item, 'volume'):
                volume = int(item.volume)
            elif isinstance(item, dict) and 'volume' in item:
                volume = int(item['volume'])
                
            # 日期处理
            timestamp = None
            if hasattr(item, 'date'):
                timestamp = item.date
            elif isinstance(item, dict) and 'date' in item:
                timestamp = item['date']
            
            return StockQuote(
                symbol=symbol,
                price=price,
                change=0,  # 需要计算
                change_percent=0,  # 需要计算
                volume=volume,
                timestamp=timestamp
            )
        return None
    
    def get_historical_prices(self, symbol: str, days: int = 30) -> List[HistoricalPrice]:
        # 实现历史价格数据获取逻辑
        pass
    
    def get_company_profile(self, symbol: str) -> Optional[CompanyProfile]:
        # 实现公司概况获取逻辑
        pass
    
    def get_news(self, symbol: str, limit: int = 10) -> List[FinancialNews]:
        # 实现新闻获取逻辑
        pass
    
    @property
    def name(self) -> str:
        return self._name
    
    @property
    def priority(self) -> int:
        return self._priority