from __future__ import annotations

import copy
import math
import pickle
from pathlib import Path
from typing import Optional, Sequence, Union

import numpy as np


class AdaptiveRLFeatureSubspaceBoostingClassifier:
    """Adaptive Reinforcement-Learning Feature-Subspace ensemble.

    Designed to wrap an existing binary classifier such as
    RL_GradientBoostingClassifierfromScratch.

    Required base-estimator interface:
        fit(X, y, X_val=None, y_val=None)
        predict_proba(X)
        predict(X)
        classes_

    Pipeline:
        1. Remove constant / mostly-missing features.
        2. Compute feature entropy and target information gain.
        3. Build correlation clusters to reduce redundancy.
        4. Sample adaptive feature subspaces.
        5. Train one classifier per RL round.
        6. Calculate validation reward:
             0.35 * minority F1
           + 0.30 * minority recall
           + 0.20 * majority recall
           + 0.15 * balanced accuracy
        7. Update feature probabilities from reward advantage.
        8. Select a validation-quality/diversity ensemble.
        9. Calibrate the final classification threshold.

    Notes:
        - X_val/y_val must come from the training fold, not the test fold.
        - Positive class should be the minority/low-grade class, normally 1.
    """

    def __init__(
        self,
        base_estimator,
        feature_names: Optional[Sequence[str]] = None,
        positive_class=1,

        # Feature quality profile
        entropy_bins: int = 16,
        maximum_missing_fraction: float = 0.90,
        minimum_unique_values: int = 2,
        initial_information_gain_weight: float = 0.75,
        initial_entropy_weight: float = 0.15,
        initial_coverage_weight: float = 0.10,

        # Correlation clustering
        correlation_threshold: float = 0.85,
        correlation_sample_size: int = 12000,

        # Adaptive subspace search
        n_rounds: int = 30,
        subspace_sizes: Sequence[Union[int, float]] = (0.25, 0.40, 0.55),
        minimum_features_per_subspace: int = 12,
        maximum_features_per_cluster: int = 3,
        exploration_rate: float = 0.20,
        exploration_decay: float = 0.97,
        minimum_exploration_rate: float = 0.05,
        feature_learning_rate: float = 0.35,
        feature_probability_floor: float = 1e-5,
        probability_temperature: float = 0.80,

        # Reward requested by the user
        reward_minority_f1_weight: float = 0.35,
        reward_minority_recall_weight: float = 0.30,
        reward_majority_recall_weight: float = 0.20,
        reward_balanced_accuracy_weight: float = 0.15,

        # Optional recall penalties
        minimum_majority_recall: float = 0.985,
        minimum_minority_recall: float = 0.20,
        majority_recall_penalty: float = 1.50,
        minority_recall_penalty: float = 0.75,

        # Final ensemble
        max_selected_models: int = 5,
        diversity_strength: float = 0.25,
        minimum_model_reward: float = -np.inf,

        # Threshold search
        decision_threshold: Union[str, float] = "auto",
        threshold_min: float = 0.05,
        threshold_max: float = 0.95,
        threshold_grid_size: int = 181,

        random_state: int = 42,
        verbose: bool = True,
    ):
        self.base_estimator = base_estimator
        self.feature_names = None if feature_names is None else list(feature_names)
        self.positive_class = positive_class

        self.entropy_bins = int(entropy_bins)
        self.maximum_missing_fraction = float(maximum_missing_fraction)
        self.minimum_unique_values = int(minimum_unique_values)
        self.initial_information_gain_weight = float(initial_information_gain_weight)
        self.initial_entropy_weight = float(initial_entropy_weight)
        self.initial_coverage_weight = float(initial_coverage_weight)

        self.correlation_threshold = float(correlation_threshold)
        self.correlation_sample_size = int(correlation_sample_size)

        self.n_rounds = int(n_rounds)
        self.subspace_sizes = tuple(subspace_sizes)
        self.minimum_features_per_subspace = int(minimum_features_per_subspace)
        self.maximum_features_per_cluster = int(maximum_features_per_cluster)
        self.exploration_rate = float(exploration_rate)
        self.exploration_decay = float(exploration_decay)
        self.minimum_exploration_rate = float(minimum_exploration_rate)
        self.feature_learning_rate = float(feature_learning_rate)
        self.feature_probability_floor = float(feature_probability_floor)
        self.probability_temperature = float(probability_temperature)

        self.reward_minority_f1_weight = float(reward_minority_f1_weight)
        self.reward_minority_recall_weight = float(reward_minority_recall_weight)
        self.reward_majority_recall_weight = float(reward_majority_recall_weight)
        self.reward_balanced_accuracy_weight = float(
            reward_balanced_accuracy_weight
        )

        self.minimum_majority_recall = float(minimum_majority_recall)
        self.minimum_minority_recall = float(minimum_minority_recall)
        self.majority_recall_penalty = float(majority_recall_penalty)
        self.minority_recall_penalty = float(minority_recall_penalty)

        self.max_selected_models = int(max_selected_models)
        self.diversity_strength = float(diversity_strength)
        self.minimum_model_reward = float(minimum_model_reward)

        self.decision_threshold = decision_threshold
        self.threshold_min = float(threshold_min)
        self.threshold_max = float(threshold_max)
        self.threshold_grid_size = int(threshold_grid_size)

        self.random_state = int(random_state)
        self.verbose = bool(verbose)

        self._validate_parameters()
        self._reset_fitted_state()

    def _validate_parameters(self):
        if self.entropy_bins < 2:
            raise ValueError("entropy_bins must be at least 2.")
        if not 0.0 <= self.maximum_missing_fraction <= 1.0:
            raise ValueError(
                "maximum_missing_fraction must be between 0 and 1."
            )
        initial_weight_sum = (
            self.initial_information_gain_weight
            + self.initial_entropy_weight
            + self.initial_coverage_weight
        )
        if not np.isclose(initial_weight_sum, 1.0):
            raise ValueError("Initial feature-score weights must sum to 1.")
        reward_weight_sum = (
            self.reward_minority_f1_weight
            + self.reward_minority_recall_weight
            + self.reward_majority_recall_weight
            + self.reward_balanced_accuracy_weight
        )
        if not np.isclose(reward_weight_sum, 1.0):
            raise ValueError("Reward weights must sum to 1.")
        if self.n_rounds < 1:
            raise ValueError("n_rounds must be at least 1.")
        if not self.subspace_sizes:
            raise ValueError("subspace_sizes cannot be empty.")
        if self.max_selected_models < 1:
            raise ValueError("max_selected_models must be at least 1.")
        if not 0.0 <= self.diversity_strength <= 1.0:
            raise ValueError("diversity_strength must be in [0, 1].")
        if not 0.0 < self.threshold_min < self.threshold_max < 1.0:
            raise ValueError("Threshold limits must satisfy 0 < min < max < 1.")

    def _reset_fitted_state(self):
        self.n_features_in_ = None
        self.classes_ = None
        self.positive_class_ = None
        self.negative_class_ = None
        self.positive_class_index_ = None
        self.negative_class_index_ = None

        self.feature_entropy_ = None
        self.feature_information_gain_ = None
        self.feature_normalized_information_gain_ = None
        self.feature_missing_fraction_ = None
        self.feature_unique_counts_ = None
        self.feature_initial_scores_ = None
        self.feature_logits_ = None
        self.feature_probabilities_ = None
        self.feature_selection_counts_ = None
        self.feature_reward_sums_ = None

        self.usable_feature_mask_ = None
        self.usable_feature_indices_ = None
        self.excluded_feature_indices_ = None

        self.feature_cluster_labels_ = None
        self.feature_clusters_ = []
        self.cluster_probabilities_ = None

        self.round_results_ = []
        self.selected_models_ = []
        self.selected_model_weights_ = None
        self.classification_threshold_ = 0.5
        self.validation_ensemble_metrics_ = None
        self.feature_importances_ = None
        self.reward_baseline_ = 0.0

        self.rng_ = np.random.default_rng(self.random_state)
        self.is_fitted_ = False

    @staticmethod
    def _prepare_X_raw(X):
        if hasattr(X, "to_numpy"):
            X = X.to_numpy()
        X = np.asarray(X, dtype=np.float64)
        if X.ndim != 2:
            raise ValueError(f"X must be 2D, received shape {X.shape}.")
        X = X.copy()
        X[~np.isfinite(X)] = np.nan
        return X

    @classmethod
    def _prepare_X(cls, X):
        X = cls._prepare_X_raw(X)
        return np.nan_to_num(X, nan=0.0, posinf=0.0, neginf=0.0)

    @staticmethod
    def _prepare_y(y):
        if hasattr(y, "to_numpy"):
            y = y.to_numpy()
        y = np.asarray(y).reshape(-1)
        if y.size == 0:
            raise ValueError("y cannot be empty.")
        return y

    @staticmethod
    def _entropy_from_counts(counts):
        counts = np.asarray(counts, dtype=np.float64)
        total = counts.sum()
        if total <= 0:
            return 0.0
        probability = counts[counts > 0] / total
        return float(-np.sum(probability * np.log2(probability)))

    def _discretize_feature(self, values):
        values = np.asarray(values, dtype=np.float64)
        finite = np.isfinite(values)
        encoded = np.full(len(values), -1, dtype=np.int64)
        finite_values = values[finite]

        if finite_values.size == 0:
            return encoded

        unique = np.unique(finite_values)
        if unique.size <= self.entropy_bins:
            mapping = {value: index for index, value in enumerate(unique)}
            encoded[finite] = np.asarray(
                [mapping[value] for value in finite_values],
                dtype=np.int64,
            )
            return encoded

        edges = np.unique(
            np.quantile(
                finite_values,
                np.linspace(0.0, 1.0, self.entropy_bins + 1),
            )
        )
        if edges.size <= 2:
            encoded[finite] = 0
        else:
            encoded[finite] = np.digitize(
                finite_values,
                edges[1:-1],
                right=True,
            )
        return encoded

    def _initialize_classes(self, y):
        self.classes_ = np.unique(y)
        if self.classes_.size != 2:
            raise ValueError(
                f"This classifier requires exactly two classes; got {self.classes_}."
            )
        if self.positive_class not in self.classes_:
            raise ValueError(
                f"positive_class={self.positive_class!r} is not in {self.classes_}."
            )
        self.positive_class_ = self.positive_class
        self.negative_class_ = self.classes_[
            self.classes_ != self.positive_class_
        ][0]
        self.positive_class_index_ = int(
            np.where(self.classes_ == self.positive_class_)[0][0]
        )
        self.negative_class_index_ = int(
            np.where(self.classes_ == self.negative_class_)[0][0]
        )

    def _initialize_feature_names(self):
        if self.feature_names is None:
            self.feature_names = [
                f"feature_{index}" for index in range(self.n_features_in_)
            ]
        if len(self.feature_names) != self.n_features_in_:
            raise ValueError(
                f"feature_names contains {len(self.feature_names)} names but "
                f"X contains {self.n_features_in_} columns."
            )

    def _profile_features(self, X_raw, y):
        positive = (y == self.positive_class_).astype(np.int64)
        target_entropy = self._entropy_from_counts(
            np.bincount(positive, minlength=2)
        )

        n_features = X_raw.shape[1]
        entropy = np.zeros(n_features, dtype=np.float64)
        information_gain = np.zeros(n_features, dtype=np.float64)
        normalized_information_gain = np.zeros(
            n_features,
            dtype=np.float64,
        )
        missing_fraction = np.zeros(n_features, dtype=np.float64)
        unique_counts = np.zeros(n_features, dtype=np.int64)
        usable = np.ones(n_features, dtype=bool)

        for feature_index in range(n_features):
            values = X_raw[:, feature_index]
            finite = np.isfinite(values)
            missing_fraction[feature_index] = 1.0 - float(np.mean(finite))
            unique_counts[feature_index] = int(
                np.unique(values[finite]).size
            )

            if (
                missing_fraction[feature_index]
                > self.maximum_missing_fraction
                or unique_counts[feature_index]
                < self.minimum_unique_values
            ):
                usable[feature_index] = False
                continue

            bins = self._discretize_feature(values) + 1
            bin_counts = np.bincount(bins)
            raw_entropy = self._entropy_from_counts(bin_counts)
            maximum_entropy = math.log2(max(np.count_nonzero(bin_counts), 2))
            entropy[feature_index] = (
                raw_entropy / max(maximum_entropy, 1e-12)
            )

            conditional_entropy = 0.0
            for bin_id in np.unique(bins):
                mask = bins == bin_id
                conditional_entropy += (
                    float(np.mean(mask))
                    * self._entropy_from_counts(
                        np.bincount(positive[mask], minlength=2)
                    )
                )

            gain = max(target_entropy - conditional_entropy, 0.0)
            information_gain[feature_index] = gain
            normalized_information_gain[feature_index] = (
                gain / max(target_entropy, 1e-12)
            )

        coverage = 1.0 - missing_fraction
        initial_scores = (
            self.initial_information_gain_weight
            * normalized_information_gain
            + self.initial_entropy_weight * entropy
            + self.initial_coverage_weight * coverage
        )
        initial_scores[~usable] = 0.0

        self.feature_entropy_ = entropy
        self.feature_information_gain_ = information_gain
        self.feature_normalized_information_gain_ = (
            normalized_information_gain
        )
        self.feature_missing_fraction_ = missing_fraction
        self.feature_unique_counts_ = unique_counts
        self.feature_initial_scores_ = initial_scores
        self.usable_feature_mask_ = usable
        self.usable_feature_indices_ = np.flatnonzero(usable)
        self.excluded_feature_indices_ = np.flatnonzero(~usable)

        if self.usable_feature_indices_.size == 0:
            raise ValueError("No usable features remain after profiling.")

        usable_scores = initial_scores[self.usable_feature_indices_]
        usable_scores = usable_scores - np.max(usable_scores)
        self.feature_logits_ = np.full(n_features, -np.inf)
        self.feature_logits_[self.usable_feature_indices_] = (
            usable_scores / max(self.probability_temperature, 1e-6)
        )

        self.feature_selection_counts_ = np.zeros(
            n_features,
            dtype=np.int64,
        )
        self.feature_reward_sums_ = np.zeros(
            n_features,
            dtype=np.float64,
        )
        self._refresh_feature_probabilities()

    def _refresh_feature_probabilities(self):
        logits = self.feature_logits_[self.usable_feature_indices_]
        logits = logits - np.max(logits)
        probability = np.exp(logits)
        probability = np.maximum(
            probability,
            self.feature_probability_floor,
        )
        probability /= probability.sum()

        self.feature_probabilities_ = np.zeros(
            self.n_features_in_,
            dtype=np.float64,
        )
        self.feature_probabilities_[self.usable_feature_indices_] = (
            probability
        )

    def _build_correlation_clusters(self, X_raw):
        usable = self.usable_feature_indices_
        if usable.size == 1:
            self.feature_cluster_labels_ = np.full(
                self.n_features_in_,
                -1,
                dtype=np.int64,
            )
            self.feature_cluster_labels_[usable] = 0
            self.feature_clusters_ = [usable.copy()]
            self.cluster_probabilities_ = np.asarray([1.0])
            return

        row_count = X_raw.shape[0]
        if row_count > self.correlation_sample_size:
            rows = self.rng_.choice(
                row_count,
                size=self.correlation_sample_size,
                replace=False,
            )
            sample = X_raw[rows][:, usable]
        else:
            sample = X_raw[:, usable]

        # Median-impute only for correlation calculation.
        sample = sample.copy()
        medians = np.nanmedian(sample, axis=0)
        medians[~np.isfinite(medians)] = 0.0
        missing_rows, missing_columns = np.where(~np.isfinite(sample))
        sample[missing_rows, missing_columns] = medians[missing_columns]

        standard_deviation = np.std(sample, axis=0)
        variable = standard_deviation > 1e-12
        standardized = np.zeros_like(sample)
        standardized[:, variable] = (
            sample[:, variable]
            - np.mean(sample[:, variable], axis=0)
        ) / standard_deviation[variable]

        correlation = np.abs(
            np.corrcoef(standardized, rowvar=False)
        )
        correlation = np.nan_to_num(
            correlation,
            nan=0.0,
            posinf=0.0,
            neginf=0.0,
        )
        np.fill_diagonal(correlation, 1.0)

        # Greedy connected-component clustering at the threshold.
        unassigned = set(range(usable.size))
        local_clusters = []

        while unassigned:
            seed = max(
                unassigned,
                key=lambda local_index: self.feature_initial_scores_[
                    usable[local_index]
                ],
            )
            queue = [seed]
            component = set()

            while queue:
                current = queue.pop()
                if current not in unassigned:
                    continue
                unassigned.remove(current)
                component.add(current)

                neighbours = np.flatnonzero(
                    correlation[current] >= self.correlation_threshold
                )
                for neighbour in neighbours:
                    neighbour = int(neighbour)
                    if neighbour in unassigned:
                        queue.append(neighbour)

            local_clusters.append(
                usable[np.asarray(sorted(component), dtype=np.int64)]
            )

        self.feature_clusters_ = local_clusters
        labels = np.full(self.n_features_in_, -1, dtype=np.int64)
        for cluster_index, cluster in enumerate(local_clusters):
            labels[cluster] = cluster_index
        self.feature_cluster_labels_ = labels
        self._refresh_cluster_probabilities()

    def _refresh_cluster_probabilities(self):
        cluster_scores = np.asarray(
            [
                self.feature_probabilities_[cluster].sum()
                for cluster in self.feature_clusters_
            ],
            dtype=np.float64,
        )
        cluster_scores = np.maximum(
            cluster_scores,
            self.feature_probability_floor,
        )
        self.cluster_probabilities_ = cluster_scores / cluster_scores.sum()

    def _resolve_subspace_size(self, requested_size):
        usable_count = len(self.usable_feature_indices_)
        if isinstance(requested_size, float):
            if not 0.0 < requested_size <= 1.0:
                raise ValueError(
                    "Float subspace sizes must be in (0, 1]."
                )
            size = int(round(requested_size * usable_count))
        else:
            size = int(requested_size)

        size = max(size, self.minimum_features_per_subspace)
        return int(np.clip(size, 1, usable_count))

    def _sample_subspace(self, requested_size, epsilon):
        target_size = self._resolve_subspace_size(requested_size)
        chosen = []
        chosen_set = set()
        cluster_counts = np.zeros(
            len(self.feature_clusters_),
            dtype=np.int64,
        )

        attempts = 0
        maximum_attempts = max(1000, target_size * 100)

        while len(chosen) < target_size and attempts < maximum_attempts:
            attempts += 1

            if self.rng_.random() < epsilon:
                cluster_index = int(
                    self.rng_.integers(len(self.feature_clusters_))
                )
            else:
                cluster_index = int(
                    self.rng_.choice(
                        len(self.feature_clusters_),
                        p=self.cluster_probabilities_,
                    )
                )

            if (
                cluster_counts[cluster_index]
                >= self.maximum_features_per_cluster
            ):
                continue

            cluster = self.feature_clusters_[cluster_index]
            available = np.asarray(
                [
                    feature
                    for feature in cluster
                    if int(feature) not in chosen_set
                ],
                dtype=np.int64,
            )
            if available.size == 0:
                continue

            if self.rng_.random() < epsilon:
                feature = int(self.rng_.choice(available))
            else:
                probability = self.feature_probabilities_[available]
                probability = probability / probability.sum()
                feature = int(
                    self.rng_.choice(available, p=probability)
                )

            chosen.append(feature)
            chosen_set.add(feature)
            cluster_counts[cluster_index] += 1

        # Fill remaining positions without the cluster cap if necessary.
        if len(chosen) < target_size:
            remaining = np.asarray(
                [
                    feature
                    for feature in self.usable_feature_indices_
                    if int(feature) not in chosen_set
                ],
                dtype=np.int64,
            )
            needed = min(target_size - len(chosen), remaining.size)
            if needed > 0:
                probability = self.feature_probabilities_[remaining]
                probability = probability / probability.sum()
                extra = self.rng_.choice(
                    remaining,
                    size=needed,
                    replace=False,
                    p=probability,
                )
                chosen.extend(int(value) for value in extra)

        return np.asarray(sorted(chosen), dtype=np.int64)

    @staticmethod
    def _binary_metrics(y_true, y_pred, positive_class):
        positive = y_true == positive_class
        predicted_positive = y_pred == positive_class

        tp = int(np.sum(positive & predicted_positive))
        fp = int(np.sum(~positive & predicted_positive))
        fn = int(np.sum(positive & ~predicted_positive))
        tn = int(np.sum(~positive & ~predicted_positive))

        precision = tp / max(tp + fp, 1)
        minority_recall = tp / max(tp + fn, 1)
        majority_recall = tn / max(tn + fp, 1)
        minority_f1 = (
            2.0 * precision * minority_recall
            / max(precision + minority_recall, 1e-15)
        )
        balanced_accuracy = 0.5 * (
            minority_recall + majority_recall
        )
        gmean = math.sqrt(
            max(minority_recall, 0.0)
            * max(majority_recall, 0.0)
        )
        accuracy = (tp + tn) / max(len(y_true), 1)

        return {
            "accuracy": float(accuracy),
            "minority_precision": float(precision),
            "minority_recall": float(minority_recall),
            "majority_recall": float(majority_recall),
            "minority_f1": float(minority_f1),
            "balanced_accuracy": float(balanced_accuracy),
            "gmean": float(gmean),
            "true_positive": tp,
            "false_positive": fp,
            "false_negative": fn,
            "true_negative": tn,
        }

    def _calculate_reward(self, metrics):
        reward = (
            self.reward_minority_f1_weight
            * metrics["minority_f1"]
            + self.reward_minority_recall_weight
            * metrics["minority_recall"]
            + self.reward_majority_recall_weight
            * metrics["majority_recall"]
            + self.reward_balanced_accuracy_weight
            * metrics["balanced_accuracy"]
        )

        if metrics["majority_recall"] < self.minimum_majority_recall:
            reward -= self.majority_recall_penalty * (
                self.minimum_majority_recall
                - metrics["majority_recall"]
            )

        if metrics["minority_recall"] < self.minimum_minority_recall:
            reward -= self.minority_recall_penalty * (
                self.minimum_minority_recall
                - metrics["minority_recall"]
            )

        return float(reward)

    def _clone_estimator(self, round_index):
        estimator = copy.deepcopy(self.base_estimator)
        seed = self.random_state + round_index + 1

        if hasattr(estimator, "random_state"):
            estimator.random_state = seed
        if hasattr(estimator, "rng"):
            estimator.rng = np.random.default_rng(seed)
        if hasattr(estimator, "verbose"):
            estimator.verbose = False

        return estimator

    def _positive_probability(self, estimator, probabilities):
        estimator_classes = np.asarray(estimator.classes_)
        positions = np.where(
            estimator_classes == self.positive_class_
        )[0]
        if positions.size != 1:
            raise RuntimeError(
                "The base estimator did not preserve the positive class."
            )
        return np.asarray(probabilities)[:, int(positions[0])]

    def _best_threshold_for_probability(self, y_val, probability):
        candidates = []
        for threshold in np.linspace(
            self.threshold_min,
            self.threshold_max,
            self.threshold_grid_size,
        ):
            prediction = np.where(
                probability >= threshold,
                self.positive_class_,
                self.negative_class_,
            )
            metrics = self._binary_metrics(
                y_val,
                prediction,
                self.positive_class_,
            )
            reward = self._calculate_reward(metrics)
            candidates.append((float(threshold), reward, metrics))

        strict = [
            candidate
            for candidate in candidates
            if candidate[2]["majority_recall"]
            >= self.minimum_majority_recall
            and candidate[2]["minority_recall"]
            >= self.minimum_minority_recall
        ]
        majority_valid = [
            candidate
            for candidate in candidates
            if candidate[2]["majority_recall"]
            >= self.minimum_majority_recall
        ]
        eligible = strict or majority_valid or candidates

        return max(
            eligible,
            key=lambda candidate: (
                candidate[1],
                candidate[2]["minority_f1"],
                candidate[2]["balanced_accuracy"],
                candidate[2]["majority_recall"],
                -abs(candidate[0] - 0.5),
            ),
        )

    def _update_feature_policy(self, indices, reward, round_index):
        if round_index == 0:
            self.reward_baseline_ = reward
        else:
            baseline_rate = 0.15
            self.reward_baseline_ = (
                (1.0 - baseline_rate) * self.reward_baseline_
                + baseline_rate * reward
            )

        advantage = reward - self.reward_baseline_
        update = self.feature_learning_rate * advantage

        self.feature_logits_[indices] += update
        self.feature_selection_counts_[indices] += 1
        self.feature_reward_sums_[indices] += reward

        # Small negative update for usable but unselected features. This makes
        # probabilities relative, while remaining much weaker than selected
        # feature updates.
        not_selected = np.setdiff1d(
            self.usable_feature_indices_,
            indices,
            assume_unique=True,
        )
        if not_selected.size:
            self.feature_logits_[not_selected] -= (
                update * len(indices) / max(len(not_selected), 1) * 0.10
            )

        # Avoid numerical explosion.
        usable_logits = self.feature_logits_[
            self.usable_feature_indices_
        ]
        usable_logits = np.clip(usable_logits, -20.0, 20.0)
        self.feature_logits_[self.usable_feature_indices_] = usable_logits

        self._refresh_feature_probabilities()
        self._refresh_cluster_probabilities()

    @staticmethod
    def _safe_correlation(first, second):
        if np.std(first) < 1e-12 or np.std(second) < 1e-12:
            return 1.0
        correlation = np.corrcoef(first, second)[0, 1]
        if not np.isfinite(correlation):
            return 1.0
        return float(np.clip(correlation, -1.0, 1.0))

    def _model_similarity(self, first, second):
        probability_similarity = max(
            self._safe_correlation(
                first["validation_positive_probability"],
                second["validation_positive_probability"],
            ),
            0.0,
        )
        prediction_agreement = float(
            np.mean(
                first["validation_predictions"]
                == second["validation_predictions"]
            )
        )
        first_features = set(first["indices"].tolist())
        second_features = set(second["indices"].tolist())
        union = len(first_features | second_features)
        jaccard = (
            len(first_features & second_features) / max(union, 1)
        )

        return float(
            np.clip(
                0.45 * probability_similarity
                + 0.35 * prediction_agreement
                + 0.20 * jaccard,
                0.0,
                1.0,
            )
        )

    def _select_final_models(self):
        pool = [
            result
            for result in self.round_results_
            if result["reward"] >= self.minimum_model_reward
        ]
        if not pool:
            raise RuntimeError("No adaptive subspace model was trained.")

        constrained = [
            result
            for result in pool
            if result["metrics"]["majority_recall"]
            >= self.minimum_majority_recall
            and result["metrics"]["minority_recall"]
            >= self.minimum_minority_recall
        ]
        if constrained:
            pool = constrained

        remaining = sorted(
            pool,
            key=lambda result: result["reward"],
            reverse=True,
        )
        selected = [remaining.pop(0)]

        while remaining and len(selected) < self.max_selected_models:
            best_candidate = None
            best_selection_score = -np.inf

            for candidate in remaining:
                maximum_similarity = max(
                    self._model_similarity(candidate, chosen)
                    for chosen in selected
                )
                diversity = 1.0 - maximum_similarity
                selection_score = (
                    (1.0 - self.diversity_strength)
                    * candidate["reward"]
                    + self.diversity_strength * diversity
                )

                if selection_score > best_selection_score:
                    best_selection_score = selection_score
                    best_candidate = candidate

            selected.append(best_candidate)
            remaining.remove(best_candidate)

        self.selected_models_ = selected

        raw_weights = np.asarray(
            [
                max(result["reward"], 0.0) + 1e-6
                for result in selected
            ],
            dtype=np.float64,
        )
        raw_weights = np.square(raw_weights)
        if raw_weights.sum() <= 0 or not np.isfinite(raw_weights).all():
            raw_weights = np.ones(len(selected), dtype=np.float64)
        self.selected_model_weights_ = raw_weights / raw_weights.sum()

    def _ensemble_validation_probability(self):
        matrix = np.column_stack(
            [
                result["validation_positive_probability"]
                for result in self.selected_models_
            ]
        )
        return matrix @ self.selected_model_weights_

    def _build_global_feature_importance(self):
        importance = np.zeros(self.n_features_in_, dtype=np.float64)

        for result, model_weight in zip(
            self.selected_models_,
            self.selected_model_weights_,
        ):
            local = getattr(result["model"], "feature_importances_", None)
            if local is None:
                # Use the adaptive feature policy when the base estimator does
                # not expose tree importances.
                local = self.feature_probabilities_[result["indices"]]
            local = np.asarray(local, dtype=np.float64)
            if local.size != result["indices"].size:
                continue
            importance[result["indices"]] += model_weight * local

        total = importance.sum()
        if total > 0:
            importance /= total
        self.feature_importances_ = importance

    def fit(self, X, y, X_val=None, y_val=None):
        if X_val is None or y_val is None:
            raise ValueError(
                "X_val and y_val are required. Split them from the training "
                "fold; do not use the test fold for adaptive search."
            )

        X_raw = self._prepare_X_raw(X)
        X_val_raw = self._prepare_X_raw(X_val)
        X_clean = np.nan_to_num(
            X_raw,
            nan=0.0,
            posinf=0.0,
            neginf=0.0,
        )
        X_val_clean = np.nan_to_num(
            X_val_raw,
            nan=0.0,
            posinf=0.0,
            neginf=0.0,
        )
        y = self._prepare_y(y)
        y_val = self._prepare_y(y_val)

        if len(X_clean) != len(y):
            raise ValueError("X and y have different sample counts.")
        if len(X_val_clean) != len(y_val):
            raise ValueError("X_val and y_val have different sample counts.")
        if X_clean.shape[1] != X_val_clean.shape[1]:
            raise ValueError(
                "X and X_val have different feature counts."
            )

        self._reset_fitted_state()
        self.n_features_in_ = X_clean.shape[1]
        self._initialize_classes(y)
        self._initialize_feature_names()
        self._profile_features(X_raw, y)
        self._build_correlation_clusters(X_raw)

        if self.verbose:
            print(
                f"ARL-FSGB usable features: "
                f"{len(self.usable_feature_indices_)}/{self.n_features_in_}"
            )
            print(
                f"Correlation clusters: {len(self.feature_clusters_)} "
                f"(threshold={self.correlation_threshold:.2f})"
            )
            print(f"Adaptive RL rounds: {self.n_rounds}")

        epsilon = self.exploration_rate

        for round_index in range(self.n_rounds):
            requested_size = self.subspace_sizes[
                round_index % len(self.subspace_sizes)
            ]
            indices = self._sample_subspace(
                requested_size,
                epsilon,
            )
            estimator = self._clone_estimator(round_index)

            try:
                estimator.fit(
                    X_clean[:, indices],
                    y,
                    X_val=X_val_clean[:, indices],
                    y_val=y_val,
                )
                raw_probability = estimator.predict_proba(
                    X_val_clean[:, indices]
                )
                positive_probability = self._positive_probability(
                    estimator,
                    raw_probability,
                )

                threshold, reward, metrics = (
                    self._best_threshold_for_probability(
                        y_val,
                        positive_probability,
                    )
                )
                validation_predictions = np.where(
                    positive_probability >= threshold,
                    self.positive_class_,
                    self.negative_class_,
                )
            except Exception as error:
                if self.verbose:
                    print(
                        f"  Round {round_index + 1:02d}: SKIPPED "
                        f"{type(error).__name__}: {error}"
                    )
                epsilon = max(
                    self.minimum_exploration_rate,
                    epsilon * self.exploration_decay,
                )
                continue

            result = {
                "round": round_index + 1,
                "indices": indices,
                "feature_names": [
                    self.feature_names[index] for index in indices
                ],
                "n_features": len(indices),
                "model": estimator,
                "threshold": float(threshold),
                "reward": float(reward),
                "metrics": metrics,
                "epsilon": float(epsilon),
                "validation_positive_probability": (
                    positive_probability.copy()
                ),
                "validation_predictions": (
                    validation_predictions.copy()
                ),
            }
            self.round_results_.append(result)
            self._update_feature_policy(
                indices,
                reward,
                len(self.round_results_) - 1,
            )

            if self.verbose:
                print(
                    f"  Round {round_index + 1:02d} | "
                    f"features={len(indices):3d} | "
                    f"reward={reward:.4f} | "
                    f"threshold={threshold:.3f} | "
                    f"majR={metrics['majority_recall']:.4f} | "
                    f"minR={metrics['minority_recall']:.4f} | "
                    f"F1={metrics['minority_f1']:.4f} | "
                    f"epsilon={epsilon:.3f}"
                )

            epsilon = max(
                self.minimum_exploration_rate,
                epsilon * self.exploration_decay,
            )

        if not self.round_results_:
            raise RuntimeError(
                "Every adaptive subspace round failed."
            )

        self._select_final_models()
        ensemble_probability = (
            self._ensemble_validation_probability()
        )

        if self.decision_threshold == "auto":
            threshold, _, metrics = (
                self._best_threshold_for_probability(
                    y_val,
                    ensemble_probability,
                )
            )
            self.classification_threshold_ = threshold
            self.validation_ensemble_metrics_ = metrics
        else:
            threshold = float(self.decision_threshold)
            prediction = np.where(
                ensemble_probability >= threshold,
                self.positive_class_,
                self.negative_class_,
            )
            self.classification_threshold_ = threshold
            self.validation_ensemble_metrics_ = (
                self._binary_metrics(
                    y_val,
                    prediction,
                    self.positive_class_,
                )
            )

        self._build_global_feature_importance()
        self.is_fitted_ = True

        if self.verbose:
            print("\nSelected ARL-FSGB models:")
            for result, weight in zip(
                self.selected_models_,
                self.selected_model_weights_,
            ):
                print(
                    f"  round={result['round']:02d} "
                    f"weight={weight:.4f} "
                    f"features={result['n_features']:3d} "
                    f"reward={result['reward']:.4f}"
                )
            print(
                f"Final ensemble threshold: "
                f"{self.classification_threshold_:.4f}"
            )
            print(
                "Validation ensemble metrics: "
                f"{self.validation_ensemble_metrics_}"
            )

        return self

    def _check_fitted(self):
        if not self.is_fitted_:
            raise RuntimeError(
                "The ARL-FSGB classifier must be fitted first."
            )

    def predict_positive_proba(self, X):
        self._check_fitted()
        X = self._prepare_X(X)
        if X.shape[1] != self.n_features_in_:
            raise ValueError(
                f"Expected {self.n_features_in_} features, "
                f"received {X.shape[1]}."
            )

        probability_matrix = []
        for result in self.selected_models_:
            raw_probability = result["model"].predict_proba(
                X[:, result["indices"]]
            )
            probability_matrix.append(
                self._positive_probability(
                    result["model"],
                    raw_probability,
                )
            )

        return (
            np.column_stack(probability_matrix)
            @ self.selected_model_weights_
        )

    def predict_proba(self, X):
        positive_probability = self.predict_positive_proba(X)
        result = np.zeros(
            (len(positive_probability), 2),
            dtype=np.float64,
        )
        result[:, self.positive_class_index_] = positive_probability
        result[:, self.negative_class_index_] = (
            1.0 - positive_probability
        )
        return result

    def predict(self, X, threshold=None):
        threshold = (
            self.classification_threshold_
            if threshold is None
            else float(threshold)
        )
        positive_probability = self.predict_positive_proba(X)
        return np.where(
            positive_probability >= threshold,
            self.positive_class_,
            self.negative_class_,
        )

    def score(self, X, y):
        y = self._prepare_y(y)
        return float(np.mean(self.predict(X) == y))

    def evaluate_binary(self, X, y, threshold=None):
        y = self._prepare_y(y)
        prediction = self.predict(X, threshold=threshold)
        return self._binary_metrics(
            y,
            prediction,
            self.positive_class_,
        )

    def get_round_summary(self):
        return sorted(
            [
                {
                    "round": result["round"],
                    "n_features": result["n_features"],
                    "threshold": result["threshold"],
                    "reward": result["reward"],
                    "epsilon": result["epsilon"],
                    **result["metrics"],
                }
                for result in self.round_results_
            ],
            key=lambda row: row["reward"],
            reverse=True,
        )

    def get_feature_policy_report(self):
        self._check_fitted()
        rows = []

        for index, name in enumerate(self.feature_names):
            count = int(self.feature_selection_counts_[index])
            average_reward = (
                float(self.feature_reward_sums_[index] / count)
                if count > 0
                else 0.0
            )
            rows.append(
                {
                    "feature_index": index,
                    "feature_name": name,
                    "usable": bool(self.usable_feature_mask_[index]),
                    "cluster": int(
                        self.feature_cluster_labels_[index]
                    ),
                    "unique_values": int(
                        self.feature_unique_counts_[index]
                    ),
                    "missing_fraction": float(
                        self.feature_missing_fraction_[index]
                    ),
                    "normalized_entropy": float(
                        self.feature_entropy_[index]
                    ),
                    "normalized_information_gain": float(
                        self.feature_normalized_information_gain_[index]
                    ),
                    "initial_score": float(
                        self.feature_initial_scores_[index]
                    ),
                    "final_probability": float(
                        self.feature_probabilities_[index]
                    ),
                    "selection_count": count,
                    "average_selected_reward": average_reward,
                    "ensemble_importance": float(
                        self.feature_importances_[index]
                    ),
                }
            )

        return sorted(
            rows,
            key=lambda row: (
                row["usable"],
                row["final_probability"],
                row["ensemble_importance"],
            ),
            reverse=True,
        )

    def get_selected_model_summary(self):
        self._check_fitted()
        rows = []

        for result, weight in zip(
            self.selected_models_,
            self.selected_model_weights_,
        ):
            rows.append(
                {
                    "round": result["round"],
                    "weight": float(weight),
                    "n_features": result["n_features"],
                    "threshold": result["threshold"],
                    "reward": result["reward"],
                    **result["metrics"],
                }
            )
        return rows

    def save_model(self, path):
        path = Path(path)
        path.parent.mkdir(parents=True, exist_ok=True)
        with path.open("wb") as file:
            pickle.dump(self, file)

    @staticmethod
    def load_model(path):
        with Path(path).open("rb") as file:
            model = pickle.load(file)
        if not isinstance(
            model,
            AdaptiveRLFeatureSubspaceBoostingClassifier,
        ):
            raise TypeError(
                "The loaded object is not an ARL-FSGB classifier."
            )
        return model
