"""
因子抽象基类
"""

from abc import ABC, abstractmethod
import pandas as pd


class FactorBase(ABC):
    """
    因子抽象基类
    
    所有因子必须实现 compute 方法
    """
    
    name: str = "base"
    category: str = "unknown"
    
    def __init__(self, **params):
        """
        初始化因子参数
        
        Args:
            **params: 因子参数（如 n_days, weighted 等）
        """
        self._params = params
    
    @abstractmethod
    def compute(self, data: pd.DataFrame) -> pd.Series:
        """
        计算因子值
        
        Args:
            data: OHLCV 数据，必须包含 'close' 列
        
        Returns:
            因子值序列（与 data 同索引）
        """
        pass
    
    def validate_data(self, data: pd.DataFrame) -> bool:
        """
        验证数据是否满足计算要求
        
        Args:
            data: OHLCV 数据
        
        Returns:
            True 如果数据有效
        """
        if 'close' not in data.columns:
            return False
        
        min_periods = self._params.get('min_periods', 20)
        return len(data) >= min_periods
    
    def __repr__(self) -> str:
        params_str = ', '.join([f"{k}={v}" for k, v in self._params.items()])
        return f"{self.__class__.__name__}({params_str})"
