"""
数据源扩展示例
演示如何添加新的数据源
"""
from applications.reporter.base_reporter import DataSource
from typing import List, Dict, Any
from datetime import datetime
from loguru import logger


class ComplaintDataSource(DataSource):
    """投诉数据源示例（可根据实际情况实现）"""
    
    def __init__(self, db_agent, table_name: str = "complaint_data"):
        """
        Args:
            db_agent: MySQLAgent实例
            table_name: 数据表名
        """
        self.db_agent = db_agent
        self.table_name = table_name
        self.logger = logger.bind(module="ComplaintDataSource")
    
    def fetch_data(self, start_time: datetime, end_time: datetime) -> List[Dict[str, Any]]:
        """从投诉数据表获取数据"""
        try:
            sql = f"""
                SELECT 
                    `标题` as title,
                    `链接` as link,
                    `内容` as summary,
                    `发布时间` as publish_time,
                    `来源` as source_url
                FROM `{self.table_name}`
                WHERE `发布时间` >= %s AND `发布时间` < %s
                ORDER BY `发布时间` DESC
            """
            
            params = (
                start_time.strftime('%Y-%m-%d %H:%M:%S'),
                end_time.strftime('%Y-%m-%d %H:%M:%S')
            )
            
            df = self.db_agent.query_to_df(sql, params=params, is_print=False)
            
            if df.empty:
                self.logger.info(f"时间范围 {start_time} 到 {end_time} 内没有投诉数据")
                return []
            
            data_list = df.to_dict('records')
            self.logger.info(f"获取到 {len(data_list)} 条投诉数据")
            return data_list
            
        except Exception as e:
            self.logger.error(f"获取投诉数据失败: {str(e)}", exc_info=True)
            return []
    
    def get_source_name(self) -> str:
        return "投诉数据"


class CustomAPIDataSource(DataSource):
    """外部API数据源示例"""
    
    def __init__(self, api_url: str, api_key: str = None):
        """
        Args:
            api_url: API地址
            api_key: API密钥（如果需要）
        """
        self.api_url = api_url
        self.api_key = api_key
        self.logger = logger.bind(module="CustomAPIDataSource")
    
    def fetch_data(self, start_time: datetime, end_time: datetime) -> List[Dict[str, Any]]:
        """从外部API获取数据"""
        import requests
        
        try:
            headers = {}
            if self.api_key:
                headers['Authorization'] = f'Bearer {self.api_key}'
            
            params = {
                'start_time': start_time.isoformat(),
                'end_time': end_time.isoformat()
            }
            
            response = requests.get(self.api_url, headers=headers, params=params, timeout=30)
            response.raise_for_status()
            
            data = response.json()
            
            # 将API返回的数据转换为标准格式
            articles = []
            for item in data.get('articles', []):
                articles.append({
                    'title': item.get('title', ''),
                    'link': item.get('url', ''),
                    'summary': item.get('description', ''),
                    'publish_time': item.get('published_at', ''),
                    'source_url': self.api_url
                })
            
            self.logger.info(f"从API获取到 {len(articles)} 条数据")
            return articles
            
        except Exception as e:
            self.logger.error(f"从API获取数据失败: {str(e)}", exc_info=True)
            return []
    
    def get_source_name(self) -> str:
        return "外部API"


# 使用示例：
"""
from applications.reporter.daily import DailyReporter
from applications.reporter.data_source_example import ComplaintDataSource
from utils.mysql_agent import MySQLAgent
from config import Config

# 创建日报生成器
reporter = DailyReporter()

# 添加投诉数据源
db_agent = MySQLAgent(Config.MYSQL_CONFIG)
complaint_source = ComplaintDataSource(db_agent, table_name="complaint_data")
reporter.add_data_source(complaint_source)

# 生成报告
report_path = reporter.generate()
print(f"报告已生成: {report_path}")
"""

