#!/usr/bin/env python3
"""Suite30 comprehensive retrospective research report; does not retrain models."""
from __future__ import annotations
import argparse
from pathlib import Path
import numpy as np
import pandas as pd
from suite25_common import print_table

def short(title,df,max_rows=30):
    if df is None or df.empty:
        print(f"[REPORT EMPTY] {title}",flush=True);return
    print_table(title,df.head(max_rows))
    if len(df)>max_rows:print(f"[REPORT NOTE] Showing {max_rows}/{len(df)} rows",flush=True)

def paired_table(d,model1,model2):
    a=d[d.model==model1]
    b=d[d.model==model2]
    if a.empty or b.empty:return pd.DataFrame()
    a=a[["fold","seed","faculty","ap","auc","fbeta_at_20","brier","ece10"]]
    b=b[["fold","seed","faculty","ap","auc","fbeta_at_20","brier","ece10"]]
    p=a.merge(b,on=["fold","seed","faculty"],suffixes=("_new","_control"),validate="one_to_one")
    for metric in ("ap","auc","fbeta_at_20","brier","ece10"):
        p["delta_"+metric]=p[metric+"_new"]-p[metric+"_control"]
    return p

def render_report(directory):
    out=Path(directory)
    df=pd.read_csv(out/"all_metrics.csv")
    if df.empty:raise ValueError("No Suite30 metrics")
    group=pd.read_csv(out/"all_joint_group_summary.csv") if (out/"all_joint_group_summary.csv").exists() else pd.DataFrame()
    selections=pd.read_csv(out/"all_selected_epochs.csv") if (out/"all_selected_epochs.csv").exists() else pd.DataFrame()
    audit=pd.read_csv(out/"all_history_coverage_audit.csv") if (out/"all_history_coverage_audit.csv").exists() else pd.DataFrame()
    portfolio=pd.read_csv(out/"all_portfolio_diagnostics.csv") if (out/"all_portfolio_diagnostics.csv").exists() else pd.DataFrame()
    epochs=pd.read_csv(out/"all_selection_curves.csv") if (out/"all_selection_curves.csv").exists() else pd.DataFrame()
    target_models=["suite30_course_only","suite30_portfolio_independent",
      "suite30_joint_no_attention","suite30_joint",
      "suite30_joint_no_attention_intervention",
      "suite30_joint_no_context_intervention",
      "suite30_joint_matched_placebo",
      "suite29_set","suite29_self_only","committee"]
    print("\n"+"="*110+"\n[SUITE30 FINAL RESEARCH REPORT]"
          "\n[RESEARCH] Portfolio information beyond row features, history and cohort risk"
          "\n[RESULT ROOT] "+str(out)+"\n"+"="*110,flush=True)
    agg=df.groupby(["model","seed"],as_index=False).agg(
        cells=("faculty","size"),faculties=("faculty","nunique"),
        ap=("ap","mean"),auc=("auc","mean"),f2=("fbeta_at_20","mean"),
        brier=("brier","mean"),ece10=("ece10","mean"))
    agg.to_csv(out/"summary_macro_all_folds.csv",index=False)
    short("OVERALL 7-FOLD MACRO — THREE SEEDS",agg.sort_values(
        ["model","seed"]),60)
    byfold=df.groupby(["fold","model"],as_index=False).agg(
       ap=("ap","mean"),auc=("auc","mean"),f2=("fbeta_at_20","mean"),
       brier=("brier","mean"))
    byfold.to_csv(out/"summary_macro_by_fold.csv",index=False)
    short("PER-FOLD MACRO AP/AUC/F2/BRIER",byfold.sort_values(
        ["fold","model"]),100)
    f67=df[df.fold.isin([6,7])]
    if len(f67):
        held=f67.groupby(["model","seed"],as_index=False).agg(
           cells=("faculty","size"),ap=("ap","mean"),auc=("auc","mean"),
           f2=("fbeta_at_20","mean"),brier=("brier","mean"))
        held.to_csv(out/"summary_f6_f7_retrospective.csv",index=False)
        short("F6/F7 RETROSPECTIVE — REPEATEDLY CONSULTED, NOT UNTOUCHED",held,65)
    folds=sorted(df.fold.unique());faculties=sorted(df.faculty.unique())
    expect=len(folds)*len(faculties)
    coverage=df.groupby(["model","seed"],as_index=False).agg(
          reported_cells=("faculty","size"),unique_faculties=("faculty","nunique"),
          unique_folds=("fold","nunique"))
    coverage["expected_cells"]=expect
    coverage["complete"]=coverage.reported_cells==expect
    coverage.to_csv(out/"summary_coverage.csv",index=False)
    short("MODEL COVERAGE — ONLY COMPLETE CELLS ARE EQUALLY COMPARABLE",coverage,65)
    comparisons=[]
    if "suite30_joint" in df.model.values:
        for baseline in target_models:
            if baseline=="suite30_joint":continue
            matched=paired_table(df,"suite30_joint",baseline)
            if matched.empty:continue
            matched["baseline"]=baseline
            comparisons.append(matched)
    if comparisons:
        paired=pd.concat(comparisons,ignore_index=True)
        paired.to_csv(out/"summary_all_paired.csv",index=False)
        paired_agg=paired.groupby("baseline",as_index=False).agg(
            cells=("faculty","size"),mean_delta_ap=("delta_ap","mean"),
            mean_delta_auc=("delta_auc","mean"),
            mean_delta_f2=("delta_fbeta_at_20","mean"),
            mean_delta_brier=("delta_brier","mean"),
            wins=("delta_ap",lambda x:int((x>0).sum())),
            losses=("delta_ap",lambda x:int((x<0).sum())))
        paired_agg["win_fraction"]=paired_agg.wins/paired_agg.cells
        paired_agg.to_csv(out/"summary_paired_comparisons.csv",index=False)
        short("PRIMARY JOINT MODEL MINUS EACH BASELINE — PAIRED AP",paired_agg,40)
        paired_fac=paired.groupby(["baseline","faculty"],as_index=False).agg(
            cells=("fold","size"),mean_delta_ap=("delta_ap","mean"),
            mean_delta_f2=("delta_fbeta_at_20","mean"),
            wins=("delta_ap",lambda x:int((x>0).sum())))
        paired_fac["win_fraction"]=paired_fac.wins/paired_fac.cells
        paired_fac.to_csv(out/"summary_paired_faculty.csv",index=False)
        for baseline in ("suite30_portfolio_independent","suite30_joint_no_attention",
                         "suite29_set","committee","suite30_joint_matched_placebo"):
            tmp=paired_fac[paired_fac.baseline==baseline].sort_values("mean_delta_ap",ascending=False)
            if not tmp.empty:short(f"FACULTY STABILITY — JOINT MINUS {baseline}",tmp,30)
        trends=paired.groupby(["baseline","fold"],as_index=False).agg(
            cells=("faculty","size"),mean_delta_ap=("delta_ap","mean"),
            wins=("delta_ap",lambda x:int((x>0).sum())))
        trends.to_csv(out/"summary_paired_by_fold.csv",index=False)
        short("PAIRED BENEFIT BY TARGET FOLD",trends,90)
    if not group.empty:
        gm=group.groupby("model",as_index=False).agg(
            observed_count=("observed_count","mean"),expected_count=("expected_count","mean"),
            count_mae=("count_mae","mean"),
            observed_any=("observed_any","mean"),predicted_any=("predicted_any","mean"),
            observed_multiple=("observed_multiple","mean"),
            predicted_multiple=("predicted_multiple","mean"),
            any_brier=("any_brier","mean"),multi_brier=("multi_brier","mean"),
            mean_joint_nll=("mean_joint_nll","mean"),
            mean_excess_count_variance=("mean_excess_count_variance","mean"),
            mean_multi_dependence_lift=("multi_joint_minus_independent","mean"))
        gm.to_csv(out/"summary_joint_risk_all_folds.csv",index=False)
        short("COLLECTIVE FAILURE — COUNT/ANY/MULTIPLE/NLL BY MODEL",gm,30)
        gh=group.groupby(["fold","model"],as_index=False).agg(
            observed_count=("observed_count","mean"),
            expected_count=("expected_count","mean"),
            observed_multiple=("observed_multiple","mean"),
            predicted_multiple=("predicted_multiple","mean"),
            count_mae=("count_mae","mean"))
        gh.to_csv(out/"summary_joint_risk_by_fold.csv",index=False)
        short("JOINT-RISK TEMPORAL CALIBRATION",gh,100)
    size_path=out/"all_course_size_metrics.csv"
    if size_path.exists():
        size=pd.read_csv(size_path)
        sb=size.groupby(["model","size_bin"],as_index=False).agg(
             faculty_cells=("faculty","size"),macro_ap=("ap","mean"),
             macro_f2=("fbeta_at_20","mean"),mean_prevalence=("prevalence","mean"))
        sb.to_csv(out/"summary_by_enrollment_set_size.csv",index=False)
        short("COURSE-RISK AP/F2 BY OBSERVED ENROLLMENT-SET SIZE",sb,65)
        fs=size.groupby(["model","faculty","size_bin"],as_index=False).agg(
             macro_ap=("ap","mean"),macro_f2=("fbeta_at_20","mean"),
             rows=("rows","sum"),failures=("failures","sum"))
        fs.to_csv(out/"summary_faculty_set_size.csv",index=False)
    group_detail_path=out/"all_group_diagnostics.csv"
    if group_detail_path.exists():
        gd=pd.read_csv(group_detail_path,usecols=["model","faculty","size",
            "observed_failures","expected_failures","multi_observed","multi_predicted",
            "peer_gate","mean_abs_logit_delta"])
        gd["size_bin"]=np.where(gd["size"]==1,"1",np.where(gd["size"]<=3,"2-3",
                             np.where(gd["size"]<=6,"4-6","7+")))
        cg=gd.groupby(["model","faculty","size_bin"],as_index=False).agg(
             groups=("size","size"),actual_count=("observed_failures","mean"),
             predicted_count=("expected_failures","mean"),
             actual_multiple=("multi_observed","mean"),
             predicted_multiple=("multi_predicted","mean"),
             mean_gate=("peer_gate","mean"),
             mean_abs_logit_delta=("mean_abs_logit_delta","mean"))
        cg.to_csv(out/"summary_collective_faculty_set_size.csv",index=False)
        sm=gd.groupby(["model","size_bin"],as_index=False).agg(
             groups=("size","size"),actual_count=("observed_failures","mean"),
             predicted_count=("expected_failures","mean"),
             actual_multiple=("multi_observed","mean"),
             predicted_multiple=("multi_predicted","mean"),
             mean_gate=("peer_gate","mean"),
             mean_abs_logit_delta=("mean_abs_logit_delta","mean"))
        sm.to_csv(out/"summary_collective_by_set_size.csv",index=False)
        short("COLLECTIVE RISK AND PEER EFFECT BY ENROLLMENT SET SIZE",sm,65)
    if not selections.empty:
        sel=selections.groupby("mode",as_index=False).agg(
             runs=("epoch","size"),mean_epoch=("epoch","mean"),
             min_epoch=("epoch","min"),max_epoch=("epoch","max"),
             mean_validation_AP=("val_macro_ap","mean"))
        sel.to_csv(out/"summary_selected_epochs.csv",index=False)
        short("HISTORICAL EPOCH SELECTION",sel,25)
    if not epochs.empty:
        per=epochs.groupby(["model","fold"],as_index=False).agg(
           peak_val_ap=("macro_ap","max"),final_val_ap=("macro_ap","last"),
           start_loss=("train_total","first"),last_loss=("train_total","last"))
        per["peak_minus_final"]=per.peak_val_ap-per.final_val_ap
        per.to_csv(out/"summary_overfit_audit.csv",index=False)
        short("OVERFITTING AUDIT — BEST VS LAST HISTORICAL VALIDATION",per,90)
    if not portfolio.empty:
        pa=portfolio.groupby(["fold","mode"],as_index=False).agg(
            mixture=("mean_mixture","mean"),
            separation=("mean_state_separation","mean"),
            peer_gate=("mean_peer_gate","mean"),
            peer_mass=("mean_peer_mass","mean"),
            peer_top=("mean_peer_top","mean"),
            entropy=("mean_peer_entropy","mean"),
            abs_logit_delta=("mean_abs_peer_logit_delta","mean"))
        pa.to_csv(out/"summary_latent_portfolio_by_fold.csv",index=False)
        short("LEARNED SHARED RISK AND PEER-GATE DIAGNOSTICS",pa,45)
        print("[INTERPRETATION] Attention/gates quantify usage, NOT causal importance. "
              "Use matched placebo and separately trained controls.",flush=True)
    calibration_path=out/"all_historical_calibration.csv"
    if calibration_path.exists():
        cal=pd.read_csv(calibration_path)
        if not cal.empty:
            ca=cal.groupby(["mode","fold"],as_index=False).agg(
              mean_bias=("final_bias","mean"),
              val_prevalence=("historical_val_prevalence","mean"),
              raw_bce=("raw_bce","mean"),
              historical_calibrated_bce=("calibrated_bce","mean"))
            ca.to_csv(out/"summary_calibration_by_fold.csv",index=False)
            short("HISTORICAL-VALIDATION INTERCEPT CALIBRATION",ca,80)
    matched_path=out/"all_matched_peer_deltas.csv"
    if matched_path.exists():
        mat=pd.read_csv(matched_path)
        if not mat.empty:
            t=mat.groupby("faculty",as_index=False).agg(
              cells=("fold","size"),mean_matched_delta_ap=("matched_delta_ap","mean"),
              wins=("matched_delta_ap",lambda x:int((x>0).sum())),
              mean_matched_rows=("matched_rows","mean"))
            t.to_csv(out/"summary_matched_peer_only.csv",index=False)
            short("TRUE PEER PORTFOLIO VS EXACT-MATCH PLACEBO: MATCHED SUBSET ONLY",t,30)
    if not audit.empty:
        problems=audit[audit.partial_groups>0]
        print(f"[HISTORY AUDIT] faculty-fold cells={len(audit)}"
              f" with_partial_groups={len(problems)}",flush=True)
        audit.to_csv(out/"summary_history_audit.csv",index=False)
        if len(problems):short("INCOMPLETE HISTORICAL SETS",problems,40)
    coverage_paths=sorted(out.glob("fold_*_seed_*_placebo_coverage.csv"))
    if coverage_paths:
        cv=pd.concat([pd.read_csv(p) for p in coverage_paths],ignore_index=True)
        cv["group_match_fraction"]=cv.matched_groups/cv.groups.clip(lower=1)
        cv["row_match_fraction"]=cv.matched_rows/cv.rows.clip(lower=1)
        cv.to_csv(out/"summary_placebo_coverage.csv",index=False)
        short("MATCHED PEER PLACEBO — EXACT FACULTY/SEMESTER/SET SIZE",cv.head(60),60)
    bs=out/"all_bootstrap.csv"
    if bs.is_file():
        boot=pd.read_csv(bs)
        boot.to_csv(out/"summary_bootstrap.csv",index=False)
        print(f"[UNCERTAINTY] {len(boot)} student-cluster descriptive intervals; "
              "folds and repeated seeds are not statistically independent.",flush=True)
    print("[SCIENTIFIC CAUTION] F6/F7 were used in earlier research; "
          "these are retrospective model comparisons, not pristine holdout claims.",flush=True)
    print("[REPORT COMPLETE]",out,flush=True)

if __name__=="__main__":
    parser=argparse.ArgumentParser()
    parser.add_argument("--output-dir",required=True)
    render_report(parser.parse_args().output_dir)
