"""
ML 模型抽象基类。

统一接口：fit(X, y) → predict(X) → save/load。
"""

from abc import ABC, abstractmethod
import pickle

import pandas as pd


class BaseModel(ABC):
    """ML 模型抽象基类。"""

    name: str = ""

    @abstractmethod
    def fit(self, X: pd.DataFrame, y: pd.Series) -> "BaseModel":
        """训练模型。返回 self 支持链式调用。"""
        ...

    @abstractmethod
    def predict(self, X: pd.DataFrame) -> pd.Series:
        """返回预测值（回归值）。"""
        ...

    @abstractmethod
    def get_feature_importance(self) -> pd.DataFrame:
        """特征重要性 DataFrame，columns=[feature, importance]。"""
        ...

    def save(self, path: str) -> None:
        """保存模型到文件（pickle）。"""
        with open(path, "wb") as f:
            pickle.dump(self, f)

    @classmethod
    def load(cls, path: str) -> "BaseModel":
        """从文件加载模型。"""
        with open(path, "rb") as f:
            return pickle.load(f)
