"""
CatBoost 模型封装。
"""

import numpy as np
import pandas as pd
from catboost import CatBoostRegressor, Pool

from models.base import BaseModel
from sklearn.model_selection import TimeSeriesSplit

_DEFAULT_PARAMS = {
    "loss_function": "RMSE",
    "iterations": 1000,
    "learning_rate": 0.03,
    "depth": 5,
    "random_seed": 42,
    "verbose": False,
    "allow_writing_files": False,
    "min_data_in_leaf": 20,
}


class CatBoostModel(BaseModel):
    """CatBoost 回归模型。"""

    name = "catboost"

    def __init__(
        self,
        params: dict | None = None,
        early_stopping: int = 50,
        eval_ratio: float = 0.2,
        random_seed: int = 42,
    ):
        self.params = params or _DEFAULT_PARAMS.copy()
        self.params["random_seed"] = random_seed
        self.early_stopping = early_stopping
        self.eval_ratio = eval_ratio
        self._model: CatBoostRegressor | None = None
        self._feature_names: list[str] = []

    def fit(self, X: pd.DataFrame, y: pd.Series) -> "CatBoostModel":
        self._feature_names = list(X.columns)

        n = len(X)
        val_size = int(n * self.eval_ratio)

        X_train, y_train = X, y
        eval_set = None

        if val_size >= 50:
            split_idx = n - val_size
            X_train, X_val = X.iloc[:split_idx], X.iloc[split_idx:]
            y_train, y_val = y.iloc[:split_idx], y.iloc[split_idx:]
            eval_set = Pool(X_val, y_val)

        self._model = CatBoostRegressor(**self.params)
        self._model.fit(
            X_train, y_train,
            eval_set=eval_set,
            early_stopping_rounds=self.early_stopping if eval_set else None,
            verbose=False,
        )
        return self

    def predict(self, X: pd.DataFrame) -> pd.Series:
        if self._model is None:
            raise RuntimeError("模型尚未训练")
        preds = self._model.predict(X[self._feature_names])
        return pd.Series(preds, index=X.index, name="pred")

    def get_feature_importance(self) -> pd.DataFrame:
        if self._model is None:
            return pd.DataFrame()
        imp = self._model.get_feature_importance()
        names = self._feature_names
        df = pd.DataFrame({"feature": names, "importance": imp})
        total = df["importance"].sum()
        df["importance_pct"] = df["importance"] / total * 100 if total > 0 else 0
        return df.sort_values("importance", ascending=False)

    def cv_evaluate(
        self, X: pd.DataFrame, y: pd.Series, n_folds: int = 5
    ) -> pd.DataFrame:
        """时间序列交叉验证评估。"""
        tscv = TimeSeriesSplit(n_splits=n_folds)
        results = []
        for fold, (train_idx, test_idx) in enumerate(tscv.split(X)):
            X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]
            y_train, y_test = y.iloc[train_idx], y.iloc[test_idx]

            model = CatBoostModel(
                params=self.params,
                early_stopping=self.early_stopping,
                eval_ratio=0.0,
            )
            model.fit(X_train, y_train)
            preds = model.predict(X_test)
            ic = preds.corr(y_test)
            mse = ((preds - y_test) ** 2).mean()
            results.append({"fold": fold, "ic": round(ic, 4), "mse": round(mse, 4)})

        df = pd.DataFrame(results)
        df.loc["mean"] = df.mean()
        return df

    @property
    def n_estimators_used(self) -> int | None:
        """实际使用的树数量。"""
        if self._model is None:
            return None
        return self._model.tree_count_
