from __future__ import annotations
from typing import Any, Dict
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

def build_model(name: str, cfg: Dict[str, Any]) -> Pipeline:
    name = name.lower()
    if name == "random_forest":
        clf = RandomForestClassifier(
            n_estimators=int(cfg.get("n_estimators", 200)),
            max_depth=int(cfg.get("max_depth", 12)),
            random_state=42,
            n_jobs=-1,
        )
    elif name == "gradient_boosting":
        clf = GradientBoostingClassifier(
            n_estimators=int(cfg.get("n_estimators", 300)),
            learning_rate=float(cfg.get("learning_rate", 0.05)),
            random_state=42,
        )
    else:
        raise ValueError(f"Unknown model: {name}")
    # Scale → clf
    pipe = Pipeline([("scaler", StandardScaler(with_mean=True, with_std=True)), ("clf", clf)])
    return pipe
