from typing import List, Dict, Optional, Any
from dataclasses import dataclass, asdict
from abc import ABC, abstractmethod

# ============================================================================
# 検索エンジンクラス
# ============================================================================

class SearchEngine(ABC):
    """検索エンジンの抽象基底クラス"""
    
    def __init__(self, name: str, timeout: int = 30):
        self.name = name
        self.timeout = timeout
    
    @abstractmethod
    def search(self, query: str, max_results: int = 5) -> tuple[List[SearchResult], float, Optional[str]]:
        """
        検索を実行
        
        Returns:
            tuple: (検索結果リスト, 応答時間, エラーメッセージ)
        """
        pass
    
    def _format_result(self, title: str, url: str, description: str,
                      response_time: float, extra_data: Optional[Dict] = None) -> SearchResult:
        """検索結果を統一フォーマットに変換"""
        
        # descriptionの長さを制限
        if len(description) > MAX_DESCRIPTION_LENGTH:
            description = description[:MAX_DESCRIPTION_LENGTH] + "..."
        
        # 短すぎる場合は警告（デバッグ用）
        if len(description) < MIN_DESCRIPTION_LENGTH:
            # 注: 短い場合でもそのまま使用（タイトルなどで補完しない）
            pass
        
        return SearchResult(
            title=title,
            url=url,
            description=description,
            source=self.name,
            timestamp=datetime.now().isoformat(),
            response_time=response_time,
            extra_data=extra_data
        )



