from __future__ import annotations

from typing import Dict, Iterable

import numpy as np
import pandas as pd


def prune_correlated_features(
    X_train: pd.DataFrame,
    selected: Iterable[str],
    stability_report: pd.DataFrame,
    threshold: float,
) -> tuple[list[str], Dict[str, object]]:
    columns = [c for c in selected if c in X_train.columns]
    if not columns:
        return [], {"threshold": threshold, "cluster_count": 0, "representative_for": {}}
    numeric = X_train[columns].apply(pd.to_numeric, errors="coerce").replace([np.inf, -np.inf], np.nan)
    numeric = numeric.fillna(numeric.median(axis=0)).fillna(0.0)
    corr = numeric.corr().abs().fillna(0.0)

    parent = {c: c for c in columns}

    def find(x: str) -> str:
        while parent[x] != x:
            parent[x] = parent[parent[x]]
            x = parent[x]
        return x

    def union(a: str, b: str) -> None:
        ra, rb = find(a), find(b)
        if ra != rb:
            parent[rb] = ra

    for i, a in enumerate(columns):
        for b in columns[i + 1 :]:
            if float(corr.loc[a, b]) >= threshold:
                union(a, b)

    clusters: Dict[str, list[str]] = {}
    for c in columns:
        clusters.setdefault(find(c), []).append(c)

    priorities = (
        stability_report.set_index("feature")["priority_score"].to_dict()
        if "priority_score" in stability_report.columns
        else {}
    )
    order = {c: i for i, c in enumerate(columns)}
    representative_for: Dict[str, str] = {}
    representatives = set()
    cluster_details = []
    for members in clusters.values():
        representative = sorted(
            members,
            key=lambda c: (-float(priorities.get(c, 0.0)), order[c], c),
        )[0]
        representatives.add(representative)
        for member in members:
            representative_for[member] = representative
        cluster_details.append({"representative": representative, "members": members})
    kept = [c for c in columns if c in representatives]
    return kept, {
        "threshold": float(threshold),
        "input_feature_count": len(columns),
        "output_feature_count": len(kept),
        "cluster_count": len(cluster_details),
        "representative_for": representative_for,
        "clusters": cluster_details,
    }
