"""Supported-origin selection, full-history refit and hard-score checkpointing.

Each origin/seed owns its preprocessing and reader. Development labels are used
only to score supported faculty cells. Target labels are never read by fitting.
The learned-reader auxiliary heads are used only for reader training.
"""
from __future__ import annotations

from dataclasses import dataclass
import copy
from pathlib import Path
import time

import numpy as np
import pandas as pd
from scipy.special import expit
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.metrics import average_precision_score, roc_auc_score, log_loss
import torch

from data import Preprocessor, Table, assert_future, build_history
from protocol import faculty_weights, origin_cells
from runtime import atomic_csv, atomic_json, print_table, seed_all


VARIANTS = ("stock", "native", "deterministic", "global", "path", "mcgb")
LEARNED = frozenset(("global", "path", "mcgb"))
INTERVENTIONS = ("no_memory", "no_outcomes", "uniform_attention", "no_path",
                 "history_8", "shuffled_history")


@dataclass
class Config:
    depth_grid: tuple = (3, 4)
    tree_grid: tuple = (32, 64, 96, 128)
    path_grid: tuple = (0., 1.5)
    max_trees: int = 128
    bins: int = 63
    min_leaf: int = 40
    min_hessian: float = 1.
    l2: float = 5.
    learning_rate: float = .05
    leaf_clip: float = 2.
    max_history: int = 32
    history_embargo: int = 0
    hidden: int = 24
    reader_epochs: int = 3
    reader_max_rows: int = 120000
    reader_batch: int = 512
    reader_lr: float = .001
    adapt_epochs: int = 1
    adapt_max_rows: int = 10000
    adapt_batch: int = 128
    adapt_lr: float = .0002
    adapt_trees: int = 8
    device: str = "cpu"
    batch_size: int = 2048
    seeds: tuple = (20261002,)
    variants: tuple = VARIANTS

    def __post_init__(self):
        for name, cast in (("depth_grid", int), ("tree_grid", int),
                           ("path_grid", float), ("seeds", int)):
            original = getattr(self, name)
            values = tuple(sorted(set(cast(v) for v in original)))
            if not values:
                raise ValueError(f"{name} cannot be empty")
            if cast is int and any(float(v) != int(v) for v in original):
                raise ValueError(f"{name} must contain integers")
            setattr(self, name, values)
        self.variants = tuple(dict.fromkeys(self.variants))
        if not self.variants or set(self.variants) - set(VARIANTS):
            raise ValueError(f"variants must be a nonempty subset of {VARIANTS}")
        if min(self.depth_grid) < 1 or min(self.tree_grid) < 1:
            raise ValueError("Depths and tree counts must be positive")
        if self.max_trees < max(self.tree_grid):
            raise ValueError("max_trees must cover every selected tree budget")
        if min(self.path_grid) < 0 or not np.isfinite(self.path_grid).all():
            raise ValueError("Path strengths must be finite and nonnegative")
        for name in ("max_trees", "min_leaf", "max_history", "hidden", "reader_max_rows",
                     "reader_batch", "adapt_max_rows", "adapt_batch", "adapt_trees", "batch_size"):
            value = getattr(self, name)
            if value < 1 or value != int(value):
                raise ValueError(f"{name} must be a positive integer")
        if not 2 <= self.bins <= 255:
            raise ValueError("bins must be in 2..255")
        if self.history_embargo < 0 or self.history_embargo != int(self.history_embargo):
            raise ValueError("history_embargo must be a nonnegative integer")
        if min(self.reader_epochs, self.adapt_epochs) < 0:
            raise ValueError("Training epochs must be nonnegative")
        if min(self.min_hessian, self.l2) < 0 or min(self.learning_rate, self.leaf_clip,
                                                     self.reader_lr, self.adapt_lr) <= 0:
            raise ValueError("Invalid learner or optimizer settings")


def _boost_config(config, depth, trees):
    from native import BoostConfig
    return BoostConfig(trees=int(trees), depth=int(depth), bins=config.bins,
                       min_leaf=config.min_leaf, min_hessian=config.min_hessian,
                       l2=config.l2, learning_rate=config.learning_rate,
                       leaf_clip=config.leaf_clip)


def _stock(config, depth, trees, seed):
    return HistGradientBoostingClassifier(
        learning_rate=config.learning_rate, max_iter=int(trees), max_depth=int(depth),
        max_leaf_nodes=None, max_bins=config.bins, min_samples_leaf=config.min_leaf,
        l2_regularization=config.l2, early_stopping=False, random_state=int(seed))


def _failure_scores(model, probabilities):
    """Map actual classes to failure=1, including sklearn's one-class HGB case.

    One-class HGB may emit two numerical probability columns even when classes_
    contains only one label. Interpret the fitted class, never a column position.
    This helper handles both staged selection and refit/reload inference.
    """
    proba = np.asarray(probabilities)
    if proba.ndim != 2:
        raise ValueError("Stock predict_proba must return a probability matrix")
    classes = np.asarray(model.classes_)
    if len(classes) == 1:
        if classes[0] not in (0, 1):
            raise ValueError("Stock model classes must be binary 0/1")
        return np.full(len(proba), float(classes[0]), dtype=np.float64)
    found = np.flatnonzero(classes == 1)
    if len(found) != 1 or proba.shape[1] != len(classes):
        raise ValueError("Stock model lacks a unique failure=1 probability column")
    return proba[:, int(found[0])].astype(np.float64, copy=False)


def _provider(bank, query, config, mode, reader=None, path_strength=0., cache=True):
    from memory import EvidenceProvider
    index = build_history(bank.table, query.table, max_history=config.max_history,
                          embargo=config.history_embargo)
    return EvidenceProvider(bank, query, index, reader=reader,
                            path_strength=float(path_strength), mode=mode,
                            device=config.device, batch_size=config.batch_size,
                            embargo=config.history_embargo, cache=cache)


def _context(phase, seed, **other):
    return dict(phase=phase, seed=int(seed), **other)


def _logs(rows, context):
    # Context belongs to every row even when a trainer returns an explicit skip.
    return [dict(row, **context) for row in rows]


def _new_reader(bank, weights, config, seed, context):
    from memory import Reader, pretrain_reader
    reader = Reader(n_courses=int(bank.course_id.max()) + 1,
                    meta_dim=bank.course_meta.shape[1], hidden=config.hidden).to(config.device)
    # A training provider does not retain a stale learned cache during updates.
    provider = _provider(bank, bank, config, "learned", reader=reader, cache=False)
    rows = pretrain_reader(reader, provider, weights, epochs=config.reader_epochs,
                           max_rows=config.reader_max_rows, batch_size=config.reader_batch,
                           lr=config.reader_lr, seed=int(seed), context=context)
    return reader, _logs(rows, context)


def _adapt(reader, provider, forest, weights, config, seed, context):
    from adapt import propose
    rows = propose(reader, provider, forest, weights, epochs=config.adapt_epochs,
                   max_rows=config.adapt_max_rows, batch_size=config.adapt_batch,
                   lr=config.adapt_lr, max_trees=config.adapt_trees,
                   seed=int(seed), context=context)
    return _logs(rows, context)


def _cell_records(table, score, names, **candidate):
    rows = []
    for name in names:
        fi = table.faculty_names.index(name)
        idx = table.faculty == fi
        y = table.y[idx]
        if not len(y) or len(np.unique(y)) != 2:
            raise ValueError(f"Supported selection cell {name} has no rows or only one class")
        row = dict(candidate, faculty=name, rows=len(y), positives=int(y.sum()),
                   negatives=int(len(y) - y.sum()),
                   ap=float(average_precision_score(y, score[idx])))
        rows.append(row)
        print("SELECTION CHECKPOINT " + str(row), flush=True)
    return rows


def _select_best(summary, variant):
    candidates = summary.loc[summary.variant == variant].copy()
    candidates = candidates[np.isfinite(candidates.development_ap)]
    if candidates.empty:
        raise ValueError(f"No supported development candidates for {variant}")
    # Exact, stable deterministic ordering; AP dominates all simplicity criteria.
    best = candidates.sort_values(["development_ap", "cycle", "trees", "depth", "path_strength"],
                                  ascending=[False, True, True, True, True], kind="stable").iloc[0]
    return dict(depth=int(best.depth), trees=int(best.trees),
                path_strength=float(best.path_strength), cycle=int(best.cycle),
                development_ap=float(best.development_ap))


def _fit_native(train_provider, y, weights, config, depth, trees, context,
                forest_log, query_provider=None, callback=None):
    from native import Booster
    started = time.perf_counter()
    forest = Booster(_boost_config(config, depth, trees))
    fit_context = dict(context, depth=int(depth), path_strength=float(train_provider.path_strength),
                       cycle=int(context.get("cycle", 0)), tree_budget=int(trees),
                       train_rows=len(y),
                       train_max_semester=int(train_provider.query.table.semester.max()),
                       reader_hash=train_provider.reader_hash)
    print("FOREST START " + str(fit_context), flush=True)

    def progress(model, number):
        if number == 1 or number % 16 == 0 or number == trees:
            print("FOREST PROGRESS " + str(dict(fit_context, trees=int(number),
                                               seconds=time.perf_counter() - started)), flush=True)
        if callback is not None:
            callback(model, number)
    forest.fit(train_provider, y, weights=weights, callback=progress)
    record = dict(context, depth=int(depth), trees=len(forest.trees),
                  train_rows=len(y), train_max_semester=int(train_provider.query.table.semester.max()),
                  reader_hash=forest.reader_hash, seconds=time.perf_counter() - started)
    forest_log.append(record)
    print("FOREST " + str(record), flush=True)
    return forest


def _native_selection(train, validation, weights, config, depth, budget, context,
                      names, candidates, forest_log, variant_cycles):
    """One validation traversal of each new tree, never repeated prefix scoring."""
    logits = None
    grid = set(config.tree_grid)

    def callback(model, number):
        nonlocal logits
        if logits is None:
            logits = np.full(len(validation.raw), model.intercept, dtype=np.float64)
        logits += model.config.learning_rate * model.tree_values(model.trees[-1], validation)
        if number in grid:
            score = expit(logits)
            for variant, cycle in variant_cycles:
                fields = dict(context, variant=variant, cycle=cycle, depth=int(depth),
                              trees=int(number), path_strength=float(train.path_strength))
                candidates.extend(_cell_records(validation.query.table, score, names, **fields))
    return _fit_native(train, train.query.table.y, weights, config, depth, budget,
                       context, forest_log, callback=callback)


def select_recipes(history: Table, origins: list, origin_audit: pd.DataFrame,
                   config: Config, out_dir: Path) -> dict:
    """Select every requested variant on the same supported origin/seed cells."""
    origins = sorted(set(int(o) for o in origins))
    if not origins:
        raise ValueError("Selection requires supported origins")
    for origin in origins:
        audited = origin_audit.loc[origin_audit.semester == origin]
        if audited.empty or "origin_eligible" not in audited or not audited.origin_eligible.all():
            raise ValueError(f"Origin {origin} is not supported by the supplied audit")
    candidates, reader_log, adapt_log, forest_log = [], [], [], []
    expected_cells = 0
    for origin in origins:
        names = tuple(origin_cells(origin_audit, origin))
        if not names:
            raise ValueError(f"Origin {origin} has no supported faculty cells")
        expected_cells += len(names) * len(config.seeds)
        train_table = history.subset(history.semester < origin)
        validation_table = history.subset(history.semester == origin)
        assert_future(train_table, validation_table)
        prep = Preprocessor.fit(train_table, np.arange(len(train_table)))
        bank, query = prep.transform(train_table), prep.transform(validation_table)
        weights = faculty_weights(train_table)
        for seed in config.seeds:
            seed_all(seed)
            base_context = _context("selection", seed, origin=origin)
            if "stock" in config.variants:
                for depth in config.depth_grid:
                    context = dict(base_context, variant="stock", depth=depth, cycle=0, path_strength=0.)
                    started = time.perf_counter()
                    model = _stock(config, depth, max(config.tree_grid), seed)
                    print("STOCK START " + str(dict(context, tree_budget=max(config.tree_grid),
                                                    train_rows=len(bank),
                                                    train_max_semester=int(train_table.semester.max()))), flush=True)
                    model.fit(bank.x, train_table.y, sample_weight=weights)
                    for number, probabilities in enumerate(model.staged_predict_proba(query.x), 1):
                        if number in config.tree_grid:
                            candidates.extend(_cell_records(validation_table, _failure_scores(model, probabilities),
                                                             names, **dict(context, trees=number)))
                    forest_log.append(dict(context, trees=int(model.n_iter_), train_rows=len(bank),
                                           train_max_semester=int(train_table.semester.max()),
                                           reader_hash=None, seconds=time.perf_counter() - started))
            for variant, mode in (("native", "current"), ("deterministic", "deterministic")):
                if variant not in config.variants:
                    continue
                train = _provider(bank, bank, config, mode)
                validation = _provider(bank, query, config, mode)
                for depth in config.depth_grid:
                    context = dict(base_context, variant=variant, depth=depth, cycle=0, path_strength=0.)
                    _native_selection(train, validation, weights, config, depth, max(config.tree_grid),
                                      context, names, candidates, forest_log, [(variant, 0)])
                del train, validation
            if not set(config.variants) & LEARNED:
                continue
            reader_context = dict(base_context, variant="shared_learned", cycle=0)
            reader, logs = _new_reader(bank, weights, config, seed, reader_context)
            reader_log.extend(logs)
            train = _provider(bank, bank, config, "learned", reader)
            validation = _provider(bank, query, config, "learned", reader)
            strengths = set(config.path_grid) if set(config.variants) & {"path", "mcgb"} else set()
            if "global" in config.variants:
                strengths.add(0.)
            # Frozen root caches are shared across strengths; root evidence does
            # not depend on path strength, and path evidence is computed per node.
            for strength in sorted(strengths):
                train.path_strength = validation.path_strength = float(strength)
                variant_cycles = []
                if "global" in config.variants and strength == 0.:
                    variant_cycles.append(("global", 0))
                if "path" in config.variants and strength in config.path_grid:
                    variant_cycles.append(("path", 0))
                has_mcgb = "mcgb" in config.variants and strength in config.path_grid
                if has_mcgb:
                    variant_cycles.append(("mcgb", 0))
                for depth in config.depth_grid:
                    context = dict(base_context, variant="learned_shared", depth=depth,
                                   path_strength=strength, cycle=0)
                    budget = config.max_trees if has_mcgb else max(config.tree_grid)
                    forest = _native_selection(train, validation, weights, config, depth, budget,
                                               context, names, candidates, forest_log, variant_cycles)
                    if not has_mcgb:
                        continue
                    # Never mutate the baseline reader or reuse its forests after
                    # a proposal. Adapt on a separate reader; then discard the
                    # full reference forest and grow a new forest from intercept.
                    adapted = copy.deepcopy(reader)
                    adapt_provider = _provider(bank, bank, config, "learned", adapted,
                                               path_strength=strength, cache=False)
                    adapt_context = dict(base_context, variant="mcgb", depth=depth,
                                         path_strength=strength, cycle=1)
                    adapt_log.extend(_adapt(adapted, adapt_provider, forest, weights,
                                             config, seed, adapt_context))
                    del adapt_provider, forest
                    adapted_train = _provider(bank, bank, config, "learned", adapted, strength)
                    adapted_validation = _provider(bank, query, config, "learned", adapted, strength)
                    _native_selection(adapted_train, adapted_validation, weights, config, depth,
                                      config.max_trees, adapt_context, names, candidates,
                                      forest_log, [("mcgb", 1)])
                    del adapted_train, adapted_validation, adapted
            del train, validation, reader
    cells = pd.DataFrame(candidates)
    keys = ["variant", "depth", "trees", "path_strength", "cycle"]
    if cells.empty:
        raise ValueError("No selection candidates were scored")
    unique = keys + ["origin", "seed", "faculty"]
    if cells.duplicated(unique).any():
        raise RuntimeError("Duplicate origin/faculty/seed selection cell")
    summary = cells.groupby(keys, as_index=False, sort=True).agg(
        development_ap=("ap", "mean"), cells=("ap", "size"),
        rows=("rows", "sum"), positives=("positives", "sum"))
    if not (summary.cells == expected_cells).all():
        raise RuntimeError("Every candidate must use identical supported origin/faculty/seed cells")
    recipes = {variant: _select_best(summary, variant) for variant in config.variants}
    out_dir = Path(out_dir)
    atomic_csv(out_dir / "selection_cells.csv", cells)
    atomic_csv(out_dir / "selection_summary.csv", summary)
    atomic_csv(out_dir / "reader_training.csv", pd.DataFrame(reader_log, columns=None if reader_log else ["phase", "origin", "seed", "variant", "cycle", "epoch"]))
    atomic_csv(out_dir / "adaptation.csv", pd.DataFrame(adapt_log, columns=None if adapt_log else ["phase", "origin", "seed", "variant", "cycle", "epoch"]))
    atomic_csv(out_dir / "forest_training.csv", pd.DataFrame(forest_log))
    atomic_json(out_dir / "selected_recipes.json", recipes)
    print_table("Selection cells (faculty/origin/seed weighted equally)", cells)
    print_table("Selection summary", summary)
    print_table("Reader training", pd.DataFrame(reader_log))
    print_table("Co-adaptation proposals", pd.DataFrame(adapt_log))
    print_table("Forest training", pd.DataFrame(forest_log))
    print("Selected recipes: " + str(recipes), flush=True)
    return recipes


def _validate_recipe(variant, recipe, config):
    if variant not in VARIANTS:
        raise ValueError(f"Unknown variant {variant}")
    depth, trees = int(recipe["depth"]), int(recipe["trees"])
    cycle, strength = int(recipe.get("cycle", 0)), float(recipe.get("path_strength", 0.))
    if depth < 1 or trees < 1 or trees > config.max_trees or cycle not in (0, 1):
        raise ValueError("Invalid selected recipe")
    if cycle and variant != "mcgb":
        raise ValueError("Only mcgb permits an adaptation cycle")
    if strength < 0 or not np.isfinite(strength):
        raise ValueError("Invalid selected path strength")
    if variant not in ("path", "mcgb") and strength != 0.:
        raise ValueError(f"{variant} requires zero path strength")
    return depth, trees, strength, cycle


def refit_variant(history, target, variant, recipe, config, seed):
    """Refit a fresh reader and full forest, reproducing selected update cycles."""
    depth, trees, strength, cycle = _validate_recipe(variant, recipe, config)
    assert_future(history, target)
    seed_all(seed)
    prep = Preprocessor.fit(history, np.arange(len(history)))
    bank, query = prep.transform(history), prep.transform(target)
    weights = faculty_weights(history)
    context = _context("refit", seed, variant=variant, depth=depth,
                       path_strength=strength, cycle=0)
    reader_log, adapt_log, forest_log = [], [], []
    reader = None
    if variant in LEARNED:
        reader, reader_log = _new_reader(bank, weights, config, seed, context)
    mode = "current" if variant in ("stock", "native") else "deterministic" if variant == "deterministic" else "learned"
    train = _provider(bank, bank, config, mode, reader, strength)
    provider = None
    if variant == "stock":
        started = time.perf_counter()
        print("STOCK START " + str(dict(context, tree_budget=trees, train_rows=len(history),
                                        train_max_semester=int(history.semester.max()))), flush=True)
        model = _stock(config, depth, trees, seed).fit(bank.x, history.y, sample_weight=weights)
        forest_log.append(dict(context, trees=int(model.n_iter_), train_rows=len(history),
                               train_max_semester=int(history.semester.max()), reader_hash=None,
                               seconds=time.perf_counter() - started))
        provider = _provider(bank, query, config, mode)
        scores = _failure_scores(model, model.predict_proba(query.x))
        tree_summary, tree_nodes = dict(trees=int(model.n_iter_), backend="sklearn_hgb"), pd.DataFrame(columns=["tree", "node", "feature", "threshold", "gain"])
    else:
        if cycle:
            # Proposal reference ALWAYS uses the fixed maximum budget. The
            # selected final score budget cannot alter the proposal trajectory.
            reference = _fit_native(train, history.y, weights, config, depth,
                                    config.max_trees, context, forest_log)
            context = dict(context, cycle=1)
            adapt_log = _adapt(reader, train, reference, weights, config, seed, context)
            del reference, train
            # Recompute every frozen feature; never continue previous trees.
            train = _provider(bank, bank, config, mode, reader, strength)
        model = _fit_native(train, history.y, weights, config, depth, trees, context, forest_log)
        del train
        provider = _provider(bank, query, config, mode, reader, strength)
        scores = model.predict_proba(provider)
        tree_summary, tree_nodes = model.diagnostics()
    bundle = dict(format_version=1, variant=variant, seed=int(seed), recipe=dict(recipe),
                  preprocessor=prep.to_dict(), reader_config=dict(reader.config) if reader is not None else None,
                  reader_state={k: v.detach().cpu().clone() for k, v in reader.state_dict().items()} if reader is not None else None,
                  reader_hash=provider.reader_hash,
                  forest=model.to_dict() if variant != "stock" else None,
                  stock_model=model if variant == "stock" else None,
                  max_history=config.max_history, history_embargo=config.history_embargo)
    # A checkpoint contains preprocessing and weights, never a copy of the bank.
    artifacts = dict(reader_log=reader_log, adapt_log=adapt_log, forest_log=forest_log,
                     tree_summary=tree_summary, tree_nodes=tree_nodes,
                     memory_diagnostics=provider.diagnostics(), provider=provider, model=model)
    return bundle, np.asarray(scores), artifacts


def load_bundle(bundle, history, target, device="cpu"):
    from memory import Reader
    from native import Booster
    if bundle.get("format_version") != 1 or bundle.get("variant") not in VARIANTS:
        raise ValueError("Unsupported Suite27 checkpoint format/variant")
    assert_future(history, target)
    variant = bundle["variant"]
    prep = Preprocessor.from_dict(bundle["preprocessor"])
    bank, query = prep.transform(history), prep.transform(target)
    reader = None
    if variant in LEARNED:
        reader = Reader(**bundle["reader_config"]).to(device)
        reader.load_state_dict(bundle["reader_state"])
    elif bundle.get("reader_state") is not None:
        raise ValueError("Unexpected reader in a raw/deterministic checkpoint")
    config = Config(max_history=int(bundle["max_history"]),
                    history_embargo=int(bundle.get("history_embargo", 0)), device=device,
                    variants=(variant,))
    strength = float(bundle["recipe"].get("path_strength", 0.))
    mode = "current" if variant in ("stock", "native") else "deterministic" if variant == "deterministic" else "learned"
    provider = _provider(bank, query, config, mode, reader, strength)
    if provider.reader_hash != bundle.get("reader_hash"):
        raise ValueError("Checkpoint reader fingerprint does not match saved reader/forest pair")
    recipe = bundle["recipe"]
    if variant == "stock":
        model = bundle["stock_model"]
        if model.n_iter_ != recipe["trees"] or model.max_depth != recipe["depth"] or strength != 0.:
            raise ValueError("Checkpoint stock model and selected recipe do not match")
        scores = _failure_scores(model, model.predict_proba(query.x))
    else:
        model = Booster.from_dict(bundle["forest"])
        if model.reader_hash != provider.reader_hash:
            raise ValueError("Checkpoint forest and reader fingerprints do not match")
        if (len(model.trees) != recipe["trees"] or model.config.depth != recipe["depth"]
                or model.path_strength != strength):
            raise ValueError("Checkpoint forest and selected recipe do not match")
        if (list(model.memory_names) != list(provider.memory_names)
                or model.comparable_features != provider.comparable_features):
            raise ValueError("Checkpoint forest and provider memory feature schema do not match")
        # predict_proba performs the existing immutable reader/raw schema check.
        # Baseline path/recipe consistency is enforced here so intentional
        # provider interventions after a proper load remain available.
        scores = model.predict_proba(provider)
    return model, provider, np.asarray(scores)


def _metrics(y, score):
    y, score = np.asarray(y), np.asarray(score, dtype=float)
    both = len(np.unique(y)) == 2
    return dict(rows=len(y), positives=int(y.sum()), negatives=int(len(y) - y.sum()),
                ap=float(average_precision_score(y, score)) if both else np.nan,
                auc=float(roc_auc_score(y, score)) if both else np.nan,
                brier=float(np.mean((score - y) ** 2)) if len(y) else np.nan,
                log_loss=float(log_loss(y, np.clip(score, 1e-7, 1 - 1e-7), labels=[0, 1])) if len(y) else np.nan)


def interventions(model, provider, y, variant, seed):
    metric_columns = ["variant", "seed", "faculty", "intervention", "rows", "positives", "negatives",
                      "ap", "auc", "brier", "log_loss", "baseline_ap", "ap_delta",
                      "changed_score_fraction", "mean_abs_delta", "max_abs_delta"]
    score_columns = ["sample_id", "student_id", "course_id", "semester", "faculty", "actual",
                     f"suite27_{variant}"] + [f"suite27_{variant}_{i}" for i in INTERVENTIONS]
    if variant in ("stock", "native"):
        return pd.DataFrame(columns=metric_columns), pd.DataFrame(columns=score_columns)
    baseline = model.predict_proba(provider)
    table = provider.query.table
    if len(y) != len(table):
        raise ValueError("Intervention labels must match query rows")
    scores = pd.DataFrame(dict(sample_id=table.sample_id, student_id=table.student,
                               course_id=table.course, semester=table.semester,
                               faculty=[table.faculty_names[int(f)] for f in table.faculty],
                               actual=np.asarray(y), **{f"suite27_{variant}": baseline}))
    rows = []
    for intervention in INTERVENTIONS:
        changed = provider.clone(intervention, seed=int(seed))
        np.testing.assert_array_equal(changed.raw, provider.raw)
        predicted = model.predict_proba(changed)
        scores[f"suite27_{variant}_{intervention}"] = predicted
        for fi, name in enumerate(table.faculty_names):
            idx = table.faculty == fi
            delta = np.abs(predicted[idx] - baseline[idx])
            metrics = _metrics(np.asarray(y)[idx], predicted[idx])
            base_ap = _metrics(np.asarray(y)[idx], baseline[idx])["ap"]
            rows.append(dict(variant=variant, seed=int(seed), faculty=name, intervention=intervention,
                             **metrics, baseline_ap=base_ap, ap_delta=metrics["ap"] - base_ap,
                             changed_score_fraction=float(np.mean(delta > 1e-12)) if len(delta) else np.nan,
                             mean_abs_delta=float(delta.mean()) if len(delta) else np.nan,
                             max_abs_delta=float(delta.max()) if len(delta) else np.nan))
        del changed
    return pd.DataFrame(rows, columns=metric_columns), scores[score_columns]


def regimes(table, scores, provider, variant, seed):
    """Every faculty receives all strata, including explicitly unsupported rows."""
    scores = np.asarray(scores)
    if len(scores) != len(table):
        raise ValueError("Regime scores must match the table")
    counts = np.asarray(provider.history.full_count)
    masks = [("history", "0", counts == 0),
             ("history", "1-4", (counts >= 1) & (counts <= 4)),
             ("history", "5-16", (counts >= 5) & (counts <= 16)),
             ("history", "17+", counts >= 17),
             ("course", "known", provider.query.course_id != 0),
             ("course", "unknown", provider.query.course_id == 0)]
    rows = []
    for fi, name in enumerate(table.faculty_names):
        for kind, regime, mask in masks:
            idx = (table.faculty == fi) & mask
            rows.append(dict(variant=variant, seed=int(seed), faculty=name,
                             regime_type=kind, regime=regime, **_metrics(table.y[idx], scores[idx])))
    return pd.DataFrame(rows)
