from __future__ import annotations

import json
from pathlib import Path
from typing import Dict, List, Mapping, MutableMapping, Sequence, Tuple

import numpy as np
import pandas as pd

from .config import SuiteConfig
from .data import FoldData, load_raw_fold
from .feature_sets import EXPERIMENTS, PreparedMatrices, prepare_experiment_matrices
from .historical import build_leak_safe_historical_features
from .metrics import (
    average_precision,
    best_f1_threshold,
    confusion_metrics,
    equal_budget_metrics,
    roc_auc,
    warn_by_rate,
)
from .model import train_predict


EQUAL_BUDGET_RATES = (0.05, 0.10, 0.15, 0.20)
SIGNALS = ("blended", "classification")


def _grade_band(grades: np.ndarray) -> np.ndarray:
    g = np.asarray(grades, dtype=float)
    labels = np.full(len(g), "9-10", dtype=object)
    labels[g < 9] = "8-9"
    labels[g < 8] = "7-8"
    labels[g < 7] = "6-7"
    labels[g < 6] = "5-6"
    labels[g < 5] = "4-5"
    labels[g < 4] = "3-4"
    labels[g < 3] = "0-3"
    labels[g == 0] = "grade=0"
    return labels


def _fold_prediction_frame(
    fold: int,
    prepared: PreparedMatrices,
    blended_grade: np.ndarray,
    cls_score: np.ndarray,
    config: SuiteConfig,
) -> pd.DataFrame:
    out = prepared.metadata_test.copy()
    out.insert(0, "fold", fold)
    out["true_grade"] = prepared.y_test
    out["true_failure"] = (prepared.y_test < config.event).astype(int)
    out["grade_band"] = _grade_band(prepared.y_test)
    out["predicted_blended_grade"] = blended_grade
    out["blended_risk"] = config.event - blended_grade
    out["classification_score"] = cls_score
    return out


def _cache_signature(fold_data: FoldData, config: SuiteConfig) -> Dict[str, object]:
    return {
        "train_path": str(fold_data.train_path.resolve()),
        "test_path": str(fold_data.test_path.resolve()),
        "train_size": fold_data.train_path.stat().st_size,
        "test_size": fold_data.test_path.stat().st_size,
        "train_mtime_ns": fold_data.train_path.stat().st_mtime_ns,
        "test_mtime_ns": fold_data.test_path.stat().st_mtime_ns,
        "event": config.event,
        "recent_semesters": config.recent_semesters,
        "course_prior_strength": config.course_prior_strength,
        "default_failure_prior": config.default_failure_prior,
        "long_duration_semesters": config.long_duration_semesters,
    }


def _load_or_build_historical_cache(
    fold_data: FoldData,
    faculty: str,
    output_root: Path,
    config: SuiteConfig,
) -> Tuple[pd.DataFrame, pd.DataFrame]:
    cache_dir = Path(output_root) / "_feature_cache" / faculty / f"fold_{fold_data.fold}"
    cache_dir.mkdir(parents=True, exist_ok=True)
    train_path = cache_dir / "historical_train.pkl"
    test_path = cache_dir / "historical_test.pkl"
    meta_path = cache_dir / "manifest.json"
    signature = _cache_signature(fold_data, config)

    if train_path.exists() and test_path.exists() and meta_path.exists():
        try:
            cached = json.loads(meta_path.read_text(encoding="utf-8"))
            if cached == signature:
                return pd.read_pickle(train_path), pd.read_pickle(test_path)
        except (OSError, ValueError, json.JSONDecodeError):
            pass

    hist_train, hist_test = build_leak_safe_historical_features(
        fold_data.train, fold_data.test, config
    )
    hist_train.to_pickle(train_path)
    hist_test.to_pickle(test_path)
    meta_path.write_text(json.dumps(signature, indent=2), encoding="utf-8")
    return hist_train, hist_test


def _generate_record(
    faculty: str,
    experiment_name: str,
    fold: int,
    splits_dir: Path,
    output_root: Path,
    config: SuiteConfig,
) -> Tuple[Dict[str, object], Dict[str, object], Dict[str, float]]:
    spec = EXPERIMENTS[experiment_name]
    fold_data = load_raw_fold(faculty, fold, splits_dir, config)
    hist_train, hist_test = _load_or_build_historical_cache(
        fold_data, faculty, output_root, config
    )
    prepared = prepare_experiment_matrices(
        fold_data.train,
        fold_data.test,
        hist_train,
        hist_test,
        spec,
        config,
    )
    prediction = train_predict(
        prepared.X_train,
        prepared.y_train,
        prepared.X_test,
        config,
        desc=f"{experiment_name}/fold{fold}",
    )
    frame = _fold_prediction_frame(
        fold,
        prepared,
        prediction.blended_grade,
        prediction.classification_score,
        config,
    )
    record: Dict[str, object] = {
        "fold": fold,
        "y_bin": frame["true_failure"].to_numpy(dtype=bool),
        "blended": frame["blended_risk"].to_numpy(dtype=float),
        "classification": frame["classification_score"].to_numpy(dtype=float),
        "frame": frame,
    }
    manifest = {
        "fold": fold,
        "feature_count": len(prepared.feature_names),
        "features": prepared.feature_names,
        "dropped_columns": prepared.dropped_columns,
    }
    diagnostics = {"fold": float(fold), **prediction.diagnostics}
    return record, manifest, diagnostics


def tune_policies(records: Sequence[Mapping[str, object]]) -> Dict[str, Dict[str, float]]:
    """Tune thresholds and warning aggressiveness from development records only."""
    if not records:
        raise ValueError("At least one tuning record is required")
    y = np.concatenate([np.asarray(r["y_bin"], dtype=bool) for r in records])
    prevalence = float(y.mean())
    policies: Dict[str, Dict[str, float]] = {}
    for signal in SIGNALS:
        score = np.concatenate([np.asarray(r[signal], dtype=float) for r in records])
        threshold, fixed = best_f1_threshold(y, score)
        policies[signal] = {
            "ap": average_precision(y, score),
            "auc": roc_auc(y, score),
            "threshold": threshold,
            "flag_rate": float(fixed["flag_rate"]),
            "ratio": float(fixed["flag_rate"] / max(prevalence, 1e-12)),
            "precision": float(fixed["precision"]),
            "recall": float(fixed["recall"]),
            "f1": float(fixed["f1"]),
            "f2": float(fixed["f2"]),
            "prevalence": prevalence,
        }
    return policies


def _evaluate_records(
    records: Sequence[MutableMapping[str, object]],
    policies: Mapping[str, Mapping[str, float]],
    tuning_records: Sequence[Mapping[str, object]],
) -> Tuple[List[Dict[str, float]], Dict[str, Dict[str, float]], List[Dict[str, float]]]:
    previous_prevalence: Dict[int, float] = {
        int(r["fold"]): float(np.asarray(r["y_bin"], dtype=bool).mean())
        for r in tuning_records
    }
    fallback_prevalence = float(
        np.concatenate(
            [np.asarray(r["y_bin"], dtype=bool) for r in tuning_records]
        ).mean()
    )
    fold_rows: List[Dict[str, float]] = []
    pooled_data: Dict[str, Dict[str, List[np.ndarray]]] = {
        signal: {"y": [], "fixed": [], "drift": [], "score": []}
        for signal in SIGNALS
    }

    for record in sorted(records, key=lambda x: int(x["fold"])):
        fold = int(record["fold"])
        y = np.asarray(record["y_bin"], dtype=bool)
        for signal in SIGNALS:
            score = np.asarray(record[signal], dtype=float)
            policy = policies[signal]
            fixed_warn = score >= float(policy["threshold"])
            reference_prev = previous_prevalence.get(fold - 1, fallback_prevalence)
            drift_rate = float(
                np.clip(float(policy["ratio"]) * reference_prev, 1e-4, 0.99)
            )
            drift_warn = warn_by_rate(score, drift_rate)
            fixed = confusion_metrics(y, fixed_warn)
            drift = confusion_metrics(y, drift_warn)
            row: Dict[str, float] = {
                "fold": fold,
                "signal": signal,
                "n": len(y),
                "prevalence": float(y.mean()),
                "ap": average_precision(y, score),
                "auc": roc_auc(y, score),
                "threshold": float(policy["threshold"]),
                "drift_rate": drift_rate,
            }
            row.update({f"fixed_{k}": float(v) for k, v in fixed.items()})
            row.update({f"drift_{k}": float(v) for k, v in drift.items()})
            fold_rows.append(row)

            pooled_data[signal]["y"].append(y)
            pooled_data[signal]["score"].append(score)
            pooled_data[signal]["fixed"].append(fixed_warn)
            pooled_data[signal]["drift"].append(drift_warn)
            if signal == "classification":
                frame = record["frame"]
                assert isinstance(frame, pd.DataFrame)
                frame["warn_fixed"] = fixed_warn.astype(int)
                frame["warn_drift"] = drift_warn.astype(int)
                frame["drift_warning_rate"] = drift_rate
        previous_prevalence[fold] = float(y.mean())

    pooled: Dict[str, Dict[str, float]] = {}
    budget_rows: List[Dict[str, float]] = []
    for signal in SIGNALS:
        y = np.concatenate(pooled_data[signal]["y"])
        score = np.concatenate(pooled_data[signal]["score"])
        fixed = confusion_metrics(y, np.concatenate(pooled_data[signal]["fixed"]))
        drift = confusion_metrics(y, np.concatenate(pooled_data[signal]["drift"]))
        pooled[signal] = {
            "ap": average_precision(y, score),
            "auc": roc_auc(y, score),
            "threshold": float(policies[signal]["threshold"]),
            **{f"fixed_{k}": float(v) for k, v in fixed.items()},
            **{f"drift_{k}": float(v) for k, v in drift.items()},
        }
        budget_records = [
            {
                "fold": int(r["fold"]),
                "y_bin": np.asarray(r["y_bin"], dtype=bool),
                "score": np.asarray(r[signal], dtype=float),
            }
            for r in records
        ]
        for rate in EQUAL_BUDGET_RATES:
            metrics = equal_budget_metrics(budget_records, rate)
            budget_rows.append(
                {
                    "signal": signal,
                    "budget": rate,
                    **{k: float(v) for k, v in metrics.items()},
                }
            )
            if signal == "classification":
                for record in records:
                    frame = record["frame"]
                    assert isinstance(frame, pd.DataFrame)
                    frame[f"warn_at_{int(rate * 100)}pct"] = warn_by_rate(
                        np.asarray(record[signal], dtype=float), rate
                    ).astype(int)
    return fold_rows, pooled, budget_rows


def run_experiment(
    faculty: str,
    experiment_name: str,
    folds: Sequence[int],
    splits_dir: Path,
    output_root: Path,
    config: SuiteConfig,
    phase: str,
    save_predictions: bool = True,
    tuning_folds: Sequence[int] | None = None,
) -> Dict[str, object]:
    if experiment_name not in EXPERIMENTS:
        raise KeyError(f"Unknown experiment: {experiment_name}")
    spec = EXPERIMENTS[experiment_name]
    experiment_dir = Path(output_root) / faculty / f"{spec.code}_{spec.slug}"
    experiment_dir.mkdir(parents=True, exist_ok=True)

    tuning_folds = list(tuning_folds) if tuning_folds is not None else list(folds)
    all_folds = sorted(set(folds) | set(tuning_folds))
    record_map: Dict[int, MutableMapping[str, object]] = {}
    manifests: Dict[int, Dict[str, object]] = {}
    diagnostics: Dict[int, Dict[str, float]] = {}
    for fold in all_folds:
        record, manifest, diagnostic = _generate_record(
            faculty,
            experiment_name,
            fold,
            Path(splits_dir),
            Path(output_root),
            config,
        )
        record_map[fold] = record
        manifests[fold] = manifest
        diagnostics[fold] = diagnostic

    tuning_records = [record_map[f] for f in tuning_folds]
    evaluation_records = [record_map[f] for f in folds]
    policies = tune_policies(tuning_records)
    fold_rows, pooled, budget_rows = _evaluate_records(
        evaluation_records, policies, tuning_records
    )

    if save_predictions:
        for record in evaluation_records:
            frame = record["frame"]
            assert isinstance(frame, pd.DataFrame)
            frame.to_csv(
                experiment_dir / f"predictions_fold_{int(record['fold'])}.csv",
                index=False,
            )

    pd.DataFrame(fold_rows).to_csv(experiment_dir / f"fold_metrics_{phase}.csv", index=False)
    # Stable alias for single-phase directories and compatibility with earlier scripts.
    pd.DataFrame(fold_rows).to_csv(experiment_dir / "fold_metrics.csv", index=False)
    pd.DataFrame(budget_rows).to_csv(
        experiment_dir / f"equal_budget_metrics_{phase}.csv", index=False
    )
    pd.DataFrame(budget_rows).to_csv(
        experiment_dir / "equal_budget_metrics.csv", index=False
    )
    diagnostics_frame = pd.DataFrame([diagnostics[f] for f in all_folds])
    diagnostics_frame.to_csv(
        experiment_dir / f"training_diagnostics_{phase}.csv", index=False
    )
    diagnostics_frame.to_csv(experiment_dir / "training_diagnostics.csv", index=False)
    manifest_payload = [manifests[f] for f in all_folds]
    with (experiment_dir / f"feature_manifest_{phase}.json").open(
        "w", encoding="utf-8"
    ) as handle:
        json.dump(manifest_payload, handle, ensure_ascii=False, indent=2)
    with (experiment_dir / "feature_manifest.json").open(
        "w", encoding="utf-8"
    ) as handle:
        json.dump(manifest_payload, handle, ensure_ascii=False, indent=2)

    selected = max(
        SIGNALS,
        key=lambda signal: policies[signal]["ap"]
        if np.isfinite(policies[signal]["ap"])
        else -np.inf,
    )
    summary: Dict[str, object] = {
        "experiment": experiment_name,
        "slug": spec.slug,
        "phase": phase,
        "evaluation_folds": list(folds),
        "tuning_folds": list(tuning_folds),
        "selected_signal": selected,
        "tuning_policies": policies,
        "pooled": pooled,
        "feature_count_min": min(manifests[f]["feature_count"] for f in all_folds),
        "feature_count_max": max(manifests[f]["feature_count"] for f in all_folds),
    }
    with (experiment_dir / f"summary_{phase}.json").open("w", encoding="utf-8") as handle:
        json.dump(summary, handle, ensure_ascii=False, indent=2, allow_nan=True)
    with (experiment_dir / "summary.json").open("w", encoding="utf-8") as handle:
        json.dump(summary, handle, ensure_ascii=False, indent=2, allow_nan=True)
    return summary


def run_many_experiments(
    faculty: str,
    experiment_names: Sequence[str],
    folds: Sequence[int],
    splits_dir: Path,
    output_root: Path,
    config: SuiteConfig,
    phase: str,
    save_predictions: bool = True,
    tuning_folds: Sequence[int] | None = None,
) -> pd.DataFrame:
    summaries = []
    for name in experiment_names:
        result = run_experiment(
            faculty=faculty,
            experiment_name=name,
            folds=folds,
            splits_dir=splits_dir,
            output_root=output_root,
            config=config,
            phase=phase,
            save_predictions=save_predictions,
            tuning_folds=tuning_folds,
        )
        row: Dict[str, object] = {
            "experiment": name,
            "slug": result["slug"],
            "selected_signal": result["selected_signal"],
            "feature_count_min": result["feature_count_min"],
            "feature_count_max": result["feature_count_max"],
        }
        experiment_dir = Path(output_root) / faculty / f"{name}_{result['slug']}"
        fold_metrics = pd.read_csv(experiment_dir / f"fold_metrics_{phase}.csv")
        for signal in SIGNALS:
            pooled_signal = result["pooled"][signal]
            signal_folds = fold_metrics[fold_metrics["signal"] == signal]
            row[f"{signal}_ap"] = pooled_signal["ap"]
            row[f"{signal}_auc"] = pooled_signal["auc"]
            row[f"{signal}_fixed_f1"] = pooled_signal["fixed_f1"]
            row[f"{signal}_fixed_recall"] = pooled_signal["fixed_recall"]
            row[f"{signal}_fixed_precision"] = pooled_signal["fixed_precision"]
            row[f"{signal}_drift_f1"] = pooled_signal["drift_f1"]
            row[f"{signal}_drift_recall"] = pooled_signal["drift_recall"]
            row[f"{signal}_mean_fold_ap"] = float(signal_folds["ap"].mean())
            row[f"{signal}_worst_fold_ap"] = float(signal_folds["ap"].min())
        summaries.append(row)

    comparison = pd.DataFrame(summaries).sort_values(
        ["classification_ap", "classification_mean_fold_ap", "classification_fixed_f1"],
        ascending=False,
    )
    faculty_dir = Path(output_root) / faculty
    faculty_dir.mkdir(parents=True, exist_ok=True)
    comparison.to_csv(faculty_dir / f"experiment_comparison_{phase}.csv", index=False)
    return comparison
