#!/usr/bin/env python3
"""Suite30: JSPR complete chronological experiments, per-faculty metrics and controls."""
from __future__ import annotations
import argparse,copy,json,math,os,random,time
from pathlib import Path
import numpy as np
import pandas as pd
import torch
from torch.utils.data import DataLoader,WeightedRandomSampler
from suite25_common import metric_dict, print_table, set_seed
from suite25_data import (discover_faculties,parse_faculties,discover_common_feature_names,
    load_fold_bundle,COURSE_FEATURES)
from suite30_data import (EnrollmentDataset,collate_groups,group_summary,
    global_chronological_split,matched_peer_placebo)
from suite30_model import JointPortfolioRisk,objective,TRAIN_MODES,joint_log_prob
from suite30_utils import (Standardizer,subset_rows,count_binary_failures,
    audit_history_group_completeness,target_profiles,rows_for_metrics,bootstrap_delta,
    load_suite29_references,group_diagnostics,save_csv)

def parse_args(argv=None):
    p=argparse.ArgumentParser(description=__doc__)
    root="/root/disk_3tb/earlywarning/30072026"
    p.add_argument("--cache-dir",default=root+"/results/cross_faculty_residual_moe/cache")
    p.add_argument("--structured-dir",default=root+"/results/suite12_structured_cache")
    p.add_argument("--output-dir",default=root+"/results/suite30_joint_portfolio")
    p.add_argument("--suite29-results",default=root+"/results/suite29_enrollment_set",
                   help="Existing Suite29 score CSVs; exact key alignment enforced. Empty disables.")
    p.add_argument("--folds",default="1,2,3,4,5,6,7")
    p.add_argument("--faculties",default="all")
    p.add_argument("--modes",default="course_only,portfolio_independent,joint_no_attention,joint")
    p.add_argument("--seeds",default="29,129,229")
    p.add_argument("--epochs",type=int,default=18)
    p.add_argument("--patience",type=int,default=5)
    p.add_argument("--min-epochs",type=int,default=3)
    p.add_argument("--validation-blocks",type=int,default=3)
    p.add_argument("--group-batch-size",type=int,default=64)
    p.add_argument("--lr",type=float,default=3e-4)
    p.add_argument("--weight-decay",type=float,default=3e-4)
    p.add_argument("--width",type=int,default=128)
    p.add_argument("--heads",type=int,default=4)
    p.add_argument("--dropout",type=float,default=.20)
    p.add_argument("--course-dim",type=int,default=17)
    p.add_argument("--joint-weight",type=float,default=.20)
    p.add_argument("--any-weight",type=float,default=.03)
    p.add_argument("--multi-weight",type=float,default=.03)
    p.add_argument("--count-weight",type=float,default=.03)
    p.add_argument("--ranking-weight",type=float,default=.05)
    p.add_argument("--warmup-epochs",type=int,default=0)
    p.add_argument("--ramp-epochs",type=int,default=2)
    p.add_argument("--recency-half-life",type=float,default=6.)
    p.add_argument("--recency-floor",type=float,default=.35)
    p.add_argument("--faculty-sampling-power",type=float,default=.5)
    p.add_argument("--calibration",choices=["none","historical_intercept"],
                   default="historical_intercept")
    p.add_argument("--calibration-prior-rows",type=float,default=1000.,
                   help="Shrink faculty intercept toward global historical intercept.")
    p.add_argument("--max-courses",type=int,default=0,
                   help="0 means no limit. Nonzero fails loudly if a group is too large.")
    p.add_argument("--device",default="auto")
    p.add_argument("--num-workers",type=int,default=0)
    p.add_argument("--save-checkpoints",action="store_true")
    p.add_argument("--bootstrap",type=int,default=100)
    p.add_argument("--disable-placebo",action="store_true")
    p.add_argument("--strict-complete-history-groups",action="store_true")
    a=p.parse_args(argv)
    if len(COURSE_FEATURES)!=a.course_dim:raise ValueError(
        f"Structured cache has {len(COURSE_FEATURES)} features; --course-dim={a.course_dim}")
    if a.width%a.heads:raise ValueError("width must be divisible by heads")
    if a.epochs<1 or a.patience<1 or a.min_epochs<1:raise ValueError("Invalid epochs/patience")
    if a.group_batch_size<1:raise ValueError("Invalid --group-batch-size")
    if not(0<=a.faculty_sampling_power<=1):raise ValueError("faculty_sampling_power outside [0,1]")
    if not(0<a.recency_floor<=1):raise ValueError("recency_floor outside (0,1]")
    if any(getattr(a,k)<0 for k in ("joint_weight","any_weight","multi_weight",
                                   "count_weight","ranking_weight")):raise ValueError("Negative loss weight")
    folds=[int(v) for v in a.folds.split(",") if v.strip()]
    seeds=[int(v) for v in a.seeds.split(",") if v.strip()]
    modes=[v.strip() for v in a.modes.split(",") if v.strip()]
    if not folds or len(set(folds))!=len(folds) or any(i<1 or i>7 for i in folds):
        raise ValueError("Expected unique folds 1..7")
    if not seeds or not modes or len(set(modes))!=len(modes) or any(m not in TRAIN_MODES for m in modes):
        raise ValueError("Expected modes drawn from "+str(TRAIN_MODES))
    return a,folds,seeds,modes

def make_loader(b,indices,a,shuffle,seed=0,override=None):
    ds=EnrollmentDataset(b,indices,max_courses=a.max_courses,context_override=override)
    if not len(ds):raise ValueError("Empty enrollment dataset")
    sampler=None
    if shuffle and a.faculty_sampling_power>0:
        group_fac=np.asarray([int(b.faculty_idx[g[0]]) for g in ds.groups])
        _, counts=np.unique(group_fac,return_counts=True)
        faculty_counts=np.bincount(group_fac,minlength=len(b.faculty_names))
        weights=np.power(np.maximum(faculty_counts[group_fac],1),-a.faculty_sampling_power)
        sampler=WeightedRandomSampler(weights=torch.tensor(weights,dtype=torch.double),
                  num_samples=len(ds),replacement=True,
                  generator=torch.Generator().manual_seed(seed))
    return ds,DataLoader(ds,batch_size=a.group_batch_size,
         sampler=sampler,shuffle=shuffle and sampler is None,
         collate_fn=collate_groups,num_workers=a.num_workers)

def move(b,device):
    return {k:v.to(device) for k,v in b.items()}

def network(b,a,device):
    return JointPortfolioRisk(b.current.shape[1],len(b.faculty_names),a.width,
                a.heads,a.dropout,a.course_dim).to(device)

def calc_train_loss(pred,bt,a,epoch,mode,max_hist):
    joint_on=mode in ("joint","joint_no_attention")
    return objective(pred,bt["y"],bt["valid"],bt["semester_ordinal"],max_hist,
        epoch=epoch,warmup_epochs=a.warmup_epochs,ramp_epochs=a.ramp_epochs,
        joint_weight=a.joint_weight if joint_on else 0.,
        any_weight=a.any_weight if joint_on else 0.,
        multi_weight=a.multi_weight if joint_on else 0.,
        count_weight=a.count_weight if joint_on else 0.,
        ranking_weight=a.ranking_weight,
        recency_half_life=a.recency_half_life,recency_floor=a.recency_floor)

def train_epoch(net,ld,opt,mode,device,a,epoch,max_hist):
    net.train()
    sums={k:0. for k in ("total","marginal","joint_nll","any","multi","count","rank")}
    rows=0;batches=0
    for bt in ld:
        b=move(bt,device);opt.zero_grad(set_to_none=True)
        pred=net(b["x"],b["valid"],b["faculty"],b["semester_z"],mode=mode)
        d=calc_train_loss(pred,b,a,epoch,mode,max_hist)
        if not torch.isfinite(d["total"]):
            raise FloatingPointError(f"Non-finite train loss mode={mode}, epoch={epoch}")
        d["total"].backward()
        torch.nn.utils.clip_grad_norm_(net.parameters(),1.)
        opt.step()
        nr=int(b["valid"].sum().item());rows+=nr;batches+=1
        for k in sums:sums[k]+=float(d[k].detach())*nr
    for k in sums:sums[k]/=max(rows,1)
    sums.update(rows=rows,batches=batches)
    return sums

@torch.no_grad()
def predict(net,ld,mode,device,nrows,intervention="none",with_targets=True,
            calibration=None):
    net.eval()
    scores=np.full(nrows,np.nan,np.float64)
    aux_keys=("p_low","p_high","peer_gate","peer_mass","peer_top","peer_entropy",
              "peer_logit_delta","mixture","low_shift","high_shift",
              "state_separation","prob_any","prob_multi","expected_count","joint_nll")
    aux={k:np.full(nrows,np.nan,np.float32) for k in aux_keys}
    for bt in ld:
        b=move(bt,device)
        cal_tensor=(None if calibration is None else
             torch.as_tensor(calibration,dtype=b["x"].dtype,device=device)[b["faculty"]])
        pred=net(b["x"],b["valid"],b["faculty"],b["semester_z"],
              mode=mode,intervention=intervention,
              context_override=b["context_course"] if intervention=="placebo" else None,
              calibration_bias=cal_tensor)
        keys=bt["index"][bt["valid"]].numpy()
        if np.isfinite(scores[keys]).any():raise AssertionError("Predicted a row twice")
        p=pred["prob"][b["valid"]].cpu().numpy()
        scores[keys]=p
        for k in ("p_low","p_high","peer_gate","peer_mass","peer_top","peer_entropy","peer_logit_delta"):
            aux[k][keys]=pred[k][b["valid"]].cpu().numpy()
        group_fields={
            "mixture":pred["mixture"],
            "low_shift":pred["low_shift"],
            "high_shift":pred["high_shift"],
            "state_separation":pred["low_shift"]+pred["high_shift"],
            "prob_any":pred["prob_any"],
            "prob_multi":pred["prob_multi"],
            "expected_count":pred["expected_count"],
        }
        if with_targets:
            group_fields["joint_nll"]=-joint_log_prob(
                b["y"],b["valid"],pred["p_low"],pred["p_high"],pred["mixture"])
        else:
            group_fields["joint_nll"]=b["y"].new_zeros(len(b["y"]))
        for k,values in group_fields.items():
            expanded=values[:,None].expand_as(b["valid"])
            aux[k][keys]=expanded[b["valid"]].cpu().numpy()
    return scores,aux

def macro(b,scores,mask):
    aps=[];f2=[];brier=[]
    for fi in range(len(b.faculty_names)):
        z=mask&(b.faculty_idx==fi)&np.isfinite(scores)
        if not z.any():continue
        md=metric_dict(b.y[z],scores[z])
        aps.append(md["ap"]);f2.append(md["fbeta_at_20"]);brier.append(md["brier"])
    if not aps:raise ValueError("No faculty validation predictions")
    return float(np.nanmean(aps)),float(np.nanmean(f2)),float(np.nanmean(brier))

def validation_group_metrics(b,valid_indices,p,aux):
    groups=EnrollmentDataset(b,valid_indices).groups
    ys=[];anyp=[];multip=[];mae=[];nl=[]
    for g in groups:
        gt=int(b.y[g].sum());i=int(g[0])
        ys.append(gt);anyp.append(float(aux["prob_any"][i]))
        multip.append(float(aux["prob_multi"][i]))
        mae.append(abs(gt-float(p[g].sum())))
        nl.append(float(aux["joint_nll"][i])/len(g))
    obs=np.asarray(ys)
    return dict(groups=len(groups),obs_any=float(np.mean(obs>0)),
       pred_any=float(np.mean(anyp)),obs_multi=float(np.mean(obs>1)),
       pred_multi=float(np.mean(multip)),count_mae=float(np.mean(mae)),
       joint_nll_row=float(np.mean(nl)))


def fit_historical_calibration(hist,raw_scores,aux,validation_mask,fold,seed,mode,prior_rows):
    """Fit a shared-logit intercept to BOTH conditional states, preserving
    coherent joint probabilities. Labels are HISTORICAL validation only.
    Per-faculty biases shrink toward a global intercept; no target outcomes.
    """
    from scipy.special import expit
    from scipy.optimize import minimize_scalar
    eps=1e-6
    p0=np.clip(aux["p_low"],eps,1-eps)
    p1=np.clip(aux["p_high"],eps,1-eps)
    mix=np.clip(aux["mixture"],eps,1-eps)
    z0=np.log(p0/(1-p0))
    z1=np.log(p1/(1-p1))
    y=np.asarray(hist.y,float)
    eligible=validation_mask&np.isfinite(z0)&np.isfinite(z1)&np.isfinite(mix)
    if not eligible.any():raise ValueError("Historical calibration lacks held-out rows")
    def calibrated(idx,shift):
        q0=expit(z0[idx]+shift)
        q1=expit(z1[idx]+shift)
        return np.clip((1-mix[idx])*q0+mix[idx]*q1,eps,1-eps)
    def bce(idx,bias):
        p=calibrated(idx,bias)
        return float(-np.mean(y[idx]*np.log(p)+(1-y[idx])*np.log1p(-p)))
    def optimize(idx):
        if int(idx.sum())<10 or len(np.unique(y[idx]))<2:return 0.
        result=minimize_scalar(lambda shift:bce(idx,shift),bounds=(-3.,3.),
                               method="bounded",options={"xatol":1e-4})
        if not result.success:raise RuntimeError("Calibration optimizer did not converge")
        return float(result.x)
    global_bias=optimize(eligible)
    bias_array=np.zeros(len(hist.faculty_names),np.float32)
    report=[]
    for fi,fac in enumerate(hist.faculty_names):
        idx=eligible&(hist.faculty_idx==fi)
        count=int(idx.sum())
        if not count:continue
        local=optimize(idx)
        alpha=count/(count+max(float(prior_rows),1))
        if count_binary_failures(hist.y[idx])<8:alpha=0.
        value=float(np.clip(global_bias*(1-alpha)+local*alpha,-3.,3.))
        bias_array[fi]=value
        report.append(dict(fold=fold,seed=seed,mode=mode,faculty=fac,
             historical_val_rows=count,historical_val_failures=int(y[idx].sum()),
             historical_val_prevalence=float(y[idx].mean()),
             global_bias=global_bias,local_bias=local,shrinkage=alpha,
             final_bias=value,raw_bce=bce(idx,0.),calibrated_bce=bce(idx,value),
             raw_mean_probability=float(np.mean(raw_scores[idx])),
             calibrated_mean_probability=float(np.mean(calibrated(idx,value)))))
    return bias_array,report

def selection_phase(hist,a,seed,mode,device,fold):
    train_mask,val_mask,split=global_chronological_split(hist,a.validation_blocks)
    for g in EnrollmentDataset(hist).groups:
        if train_mask[g].any() and val_mask[g].any():
            raise ValueError("Historical selection split divides student-semester group")
    prep=Standardizer(subset_rows(hist,np.flatnonzero(train_mask)))
    normalized=prep.apply(hist)
    tr_ds,tr_ld=make_loader(normalized,np.flatnonzero(train_mask),a,True,seed)
    va_ds,va_ld=make_loader(normalized,np.flatnonzero(val_mask),a,False)
    print("[GLOBAL TEMPORAL CUTOFF] "+json.dumps(split),flush=True)
    for fi,fac in enumerate(hist.faculty_names):
        print(f"[SELECTION COVERAGE] faculty={fac} train={int((train_mask&(hist.faculty_idx==fi)).sum())}"
              f" val={int((val_mask&(hist.faculty_idx==fi)).sum())}",flush=True)
    print(f"[SELECT DATA] mode={mode} train_rows={int(train_mask.sum()):,}"
          f" validation_rows={int(val_mask.sum()):,} train_groups={group_summary(tr_ds)}"
          f" val_groups={group_summary(va_ds)}",flush=True)
    set_seed(seed)
    net=network(hist,a,device)
    opt=torch.optim.AdamW(net.parameters(),lr=a.lr,weight_decay=a.weight_decay)
    best=None;patience=0;curves=[];faculty_curves=[]
    max_ord=float(np.max(hist.semester_ordinal[train_mask]))
    for ep in range(1,a.epochs+1):
        train=train_epoch(net,tr_ld,opt,mode,device,a,ep,max_ord)
        scores,aux=predict(net,va_ld,mode,device,len(hist.y))
        ap,f2,br=macro(hist,scores,val_mask)
        gm=validation_group_metrics(hist,np.flatnonzero(val_mask),scores,aux)
        curves.append(dict(fold=fold,seed=seed,model="suite30_"+mode,epoch=ep,
                macro_ap=ap,macro_f2_at20=f2,macro_brier=br,
                train_rows=train["rows"],train_batches=train["batches"],
                **{f"train_{k}":train[k] for k in
                   ("total","marginal","joint_nll","any","multi","count","rank")},
                **{f"val_{k}":v for k,v in gm.items()}))
        for fi,fac in enumerate(hist.faculty_names):
            z=val_mask&(hist.faculty_idx==fi)
            if not z.any():continue
            md=metric_dict(hist.y[z],scores[z])
            faculty_curves.append(dict(fold=fold,seed=seed,mode=mode,
                epoch=ep,faculty=fac,rows=int(z.sum()),
                failures=count_binary_failures(hist.y[z]),
                ap=md["ap"],auc=md["auc"],fbeta_at_20=md["fbeta_at_20"],
                brier=md["brier"]))
        key=(ap,f2,-br)
        if best is None or key>best["key"]:
            best=dict(epoch=ep,key=key,state=copy.deepcopy(
                {k:v.detach().cpu().clone() for k,v in net.state_dict().items()}))
            patience=0
        else: patience+=1
        print(f"[SELECT] F{fold} seed={seed} {mode} ep={ep:02d}/{a.epochs}"
             f" total={train['total']:.5f} BCE={train['marginal']:.5f}"
             f" JNLL={train['joint_nll']:.5f} rank={train['rank']:.5f}"
             f" valAP={ap:.6f} valF2@20={f2:.6f} Brier={br:.6f}"
             f" valAny={gm['pred_any']:.4f}/{gm['obs_any']:.4f}"
             f" valMulti={gm['pred_multi']:.4f}/{gm['obs_multi']:.4f}"
             f" valCountMAE={gm['count_mae']:.4f}"
             f" best={best['epoch']} patience={patience}",flush=True)
        if ep>=a.min_epochs and patience>=a.patience:break
    calibration_details=[]
    if a.calibration=="historical_intercept":
        net.load_state_dict(best["state"])
        best_p,best_aux=predict(net,va_ld,mode,device,len(hist.y))
        biases,calibration_details=fit_historical_calibration(
            hist,best_p,best_aux,val_mask,fold,seed,mode,a.calibration_prior_rows)
        best["calibration"]=biases
        print(f"[CALIBRATION] fold={fold} seed={seed} mode={mode}"
              f" biases={np.round(biases,4).tolist()}",flush=True)
    else:
        best["calibration"]=np.zeros(len(hist.faculty_names),np.float32)
    del best["state"]
    return best,curves,faculty_curves,calibration_details

def checkpoint_payload(net,prep,hist,a,fold,seed,mode,epochs,names,calibration):
    return dict(model={k:v.detach().cpu() for k,v in net.state_dict().items()},
                fold=fold,seed=seed,mode=mode,epochs=epochs,
                faculties=list(hist.faculty_names),feature_names=list(names),
                calibration=np.asarray(calibration,np.float32),
                course_feature_names=list(COURSE_FEATURES),args=vars(a),
                model_config=dict(n_features=hist.current.shape[1],
                   n_faculties=len(hist.faculty_names),width=a.width,
                   heads=a.heads,dropout=a.dropout,course_dim=a.course_dim),
                preprocessing=dict(mean=prep.mean.astype(np.float32),
                   scale=prep.scale.astype(np.float32),
                   smean=float(prep.smean),sscale=float(prep.sscale)))

def refit(hist,target,a,seed,mode,epochs,device,fold,names,out,calibration):
    prep=Standardizer(hist)
    nh=prep.apply(hist);nt=prep.apply(target)
    train_ds,train_ld=make_loader(nh,None,a,True,seed+100000)
    target_ds,target_ld=make_loader(nt,None,a,False)
    print(f"[REFIT DATA] mode={mode} fold={fold} seed={seed}"
          f" history_rows={len(nh):,} target_rows={len(nt):,}"
          f" history_groups={group_summary(train_ds)}"
          f" target_groups={group_summary(target_ds)}",flush=True)
    set_seed(seed);net=network(nh,a,device)
    opt=torch.optim.AdamW(net.parameters(),lr=a.lr,weight_decay=a.weight_decay)
    max_ord=float(np.max(nh.semester_ordinal))
    refit_losses=[]
    for ep in range(1,epochs+1):
        loss=train_epoch(net,train_ld,opt,mode,device,a,ep,max_ord)
        refit_losses.append(dict(fold=fold,seed=seed,mode=mode,epoch=ep,**loss))
        if ep==1 or ep==epochs or ep%5==0:
            print(f"[REFIT] F{fold} seed={seed} {mode} ep={ep:02d}/{epochs}"
                f" total={loss['total']:.5f} BCE={loss['marginal']:.5f}"
                f" JNLL={loss['joint_nll']:.5f} any={loss['any']:.5f}"
                f" multi={loss['multi']:.5f} count={loss['count']:.5f}"
                f" rank={loss['rank']:.5f} rows={loss['rows']:,}",flush=True)
    p,aux=predict(net,target_ld,mode,device,len(nt.y),calibration=calibration)
    if not np.isfinite(p).all():raise AssertionError("Target predictions have missing or repeated rows")
    extras={}
    if mode=="joint":
        if a.calibration!="none":
            raw_p,raw_aux=predict(net,target_ld,mode,device,len(nt.y))
            extras["joint_raw_uncalibrated"]=(raw_p,raw_aux)
        for intervention,tag in (("no_attention","joint_no_attention_intervention"),
                                  ("no_context","joint_no_context_intervention")):
            q,d=predict(net,target_ld,mode,device,len(nt.y),
                        intervention,calibration=calibration)
            extras[tag]=(q,d)
        if not a.disable_placebo:
            donors,coverage,match_mask=matched_peer_placebo(
                nt,seed+fold*100003,a.course_dim,return_mask=True)
            placebo_ds,placebo_ld=make_loader(nt,None,a,False,override=donors)
            q,d=predict(net,placebo_ld,mode,device,len(nt.y),
                        "placebo",calibration=calibration)
            d["matched_rows"]=match_mask
            extras["joint_matched_placebo"]=(q,d)
            print(f"[PLACEBO COVERAGE] fold={fold} seed={seed} "+
                  json.dumps(coverage,sort_keys=True),flush=True)
            save_csv([dict(fold=fold,seed=seed,faculty=fac,**stats)
                      for fac,stats in coverage["faculty"].items()],
                     out/f"fold_{fold}_seed_{seed}_placebo_coverage.csv")
    if a.save_checkpoints:
        torch.save(checkpoint_payload(net,prep,hist,a,fold,seed,mode,epochs,names,calibration),
                   out/f"fold{fold}_{mode}_seed{seed}.pt")
    del net
    if device.type=="cuda":torch.cuda.empty_cache()
    return p,aux,extras,refit_losses

def main(argv=None):
    a,folds,seeds,modes=parse_args(argv)
    out=Path(a.output_dir);out.mkdir(parents=True,exist_ok=True)
    device=torch.device(("cuda" if torch.cuda.is_available() else "cpu")
                        if a.device=="auto" else a.device)
    if device.type=="cuda" and not torch.cuda.is_available():
        raise RuntimeError("CUDA device unavailable")
    faculties=parse_faculties(a.faculties,discover_faculties(a.cache_dir,folds[0]))
    common=discover_common_feature_names(a.cache_dir,faculties,folds[0])
    print(f"[START] Suite30 JSPR device={device} torch={torch.__version__} "
          f"folds={folds} faculties={len(faculties)} seeds={seeds} modes={modes} "
          f"raw_features={len(common)} structured_features={len(COURSE_FEATURES)} "
          f"total_features={len(common)+len(COURSE_FEATURES)}",flush=True)
    print("[DESIGN] One network per trained mode. Joint mixture models correlated "
          "failures; all controls use the same historical chronological contract.",flush=True)
    print("[REFERENCE CONTRACT] The existing Committee is a baseline only. "
          "Suite29 prior scores require exact per-row keys, not label/count matching.",flush=True)
    (out/"config.json").write_text(json.dumps(vars(a),indent=2))
    (out/"features.json").write_text(json.dumps(dict(common=common,
          structured_course_features=COURSE_FEATURES,faculty_names=faculties),indent=2))
    metrics=[];all_group_stats=[];select_rows=[];selection_faculty=[]
    refit_rows=[];audit=[];profiles=[];bootstrap_rows=[];calibration_rows=[]
    runtimes=[];selected_epochs=[];raw_aux=[];matched_only=[];size_metrics=[]
    group_path=out/"all_group_diagnostics.csv"
    if group_path.exists():group_path.unlink()
    for fold in folds:
        start=time.monotonic()
        print("\n"+"="*105+f"\nSUITE30 FOLD {fold} - {len(faculties)}-FACULTY JOINT PORTFOLIO ANALYSIS\n"+"="*105,
              flush=True)
        hist,target,names=load_fold_bundle(a.cache_dir,a.structured_dir,faculties,fold,common)
        audit.extend(audit_history_group_completeness(
            a.cache_dir,faculties,fold,a.strict_complete_history_groups))
        if np.max(hist.semester_ordinal)>=np.min(target.semester_ordinal):
            raise ValueError(f"Global target chronology violated F{fold}: "
                 f"history max {np.max(hist.semester_ordinal)} "
                 f"target min {np.min(target.semester_ordinal)}")
        if not np.isfinite(hist.current).all() or not np.isfinite(target.current).all():
            raise ValueError("Non-finite input features")
        nfail_hist=count_binary_failures(hist.y);nfail_target=count_binary_failures(target.y)
        print(f"[DATA] fold={fold} history_rows={len(hist.y):,}"
              f" history_failures={nfail_hist:,} history_prevalence={nfail_hist/len(hist.y):.6f}"
              f" target_rows={len(target.y):,} target_failures={nfail_target:,}"
              f" target_prevalence={nfail_target/len(target.y):.6f}"
              f" prevalence_ratio={(nfail_target/len(target.y))/(nfail_hist/len(hist.y)):.3f}"
              f" input_dim={hist.current.shape[1]}",flush=True)
        print(f"[TEMPORAL] history_last={np.max(hist.semester_ordinal)}"
              f" target_first={np.min(target.semester_ordinal)}",flush=True)
        if len(faculties)<len(discover_faculties(a.cache_dir,fold)):
            print("[SMOKE SCHEMA] Partial faculties change the common raw feature "
                  "intersection; smoke metrics are not comparable to full-suite scores.",flush=True)
        pro=target_profiles(target,fold);profiles.extend(pro)
        print_table(f"FOLD {fold} ENROLLMENT PROFILE",pd.DataFrame(pro))
        save_csv(profiles,out/"all_target_enrollment_profiles.csv")
        for seed in seeds:
            scores={"committee":target.committee.copy()}
            other=load_suite29_references(a.suite29_results,target,fold,seed)
            scores.update(other)
            aux_models={}
            for mode in modes:
                best,curves,fc,cal_rows=selection_phase(hist,a,seed,mode,device,fold)
                calibration_rows.extend(cal_rows)
                selected_epochs.append(dict(fold=fold,seed=seed,mode=mode,
                                             epoch=best["epoch"],val_macro_ap=best["key"][0]))
                select_rows.extend(curves);selection_faculty.extend(fc)
                print(f"[SELECTION RESULT] fold={fold} seed={seed} mode={mode} "
                      f"epoch={best['epoch']} valAP={best['key'][0]:.6f}",flush=True)
                p,aux,extras,refit_losses=refit(
                    hist,target,a,seed,mode,best["epoch"],device,fold,names,out,best["calibration"])
                model_name="suite30_"+mode
                scores[model_name]=p;aux_models[model_name]=aux
                refit_rows.extend(refit_losses)
                for tag,(q,d) in extras.items():
                    scores["suite30_"+tag]=q
                    aux_models["suite30_"+tag]=d
                raw_aux.append(dict(fold=fold,seed=seed,mode=mode,
                    mean_mixture=float(np.mean(aux["mixture"])),
                    mean_state_separation=float(np.mean(aux["state_separation"])),
                    mean_peer_gate=float(np.mean(aux["peer_gate"])),
                    mean_peer_mass=float(np.mean(aux["peer_mass"])),
                    mean_peer_top=float(np.mean(aux["peer_top"])),
                    mean_peer_entropy=float(np.mean(aux["peer_entropy"])),
                    mean_abs_peer_logit_delta=float(np.mean(np.abs(aux["peer_logit_delta"]))),
                    mean_prob_any_registration_weighted=float(np.mean(aux["prob_any"])),
                    mean_prob_multi_registration_weighted=float(np.mean(aux["prob_multi"]))))
                print(f"[MODEL DIAGNOSTICS] F{fold} seed={seed} {mode}"
                      f" mixture={raw_aux[-1]['mean_mixture']:.4f}"
                      f" separation={raw_aux[-1]['mean_state_separation']:.4f}"
                      f" gate={raw_aux[-1]['mean_peer_gate']:.4f}"
                      f" peer_mass={raw_aux[-1]['mean_peer_mass']:.4f}"
                      f" peer_top={raw_aux[-1]['mean_peer_top']:.4f}"
                      f" entropy={raw_aux[-1]['mean_peer_entropy']:.4f}"
                      f" |logit_delta|={raw_aux[-1]['mean_abs_peer_logit_delta']:.5f}",
                      flush=True)
            if "suite30_joint_matched_placebo" in aux_models:
                matched=aux_models["suite30_joint_matched_placebo"]["matched_rows"]
                for fi,fac in enumerate(faculties):
                    z=matched&(target.faculty_idx==fi)
                    if z.sum()<5 or np.unique(target.y[z]).size<2:continue
                    from suite25_common import safe_ap
                    v=float(safe_ap(target.y[z],scores["suite30_joint"][z])-
                      safe_ap(target.y[z],scores["suite30_joint_matched_placebo"][z]))
                    matched_only.append(dict(fold=fold,seed=seed,faculty=fac,
                         matched_rows=int(z.sum()),matched_failures=int(target.y[z].sum()),
                         matched_delta_ap=v))
                print_table(f"FOLD {fold} SEED {seed} MATCHED-ONLY PEER PLACEBO DELTAS",
                            pd.DataFrame([r for r in matched_only
                                          if r["fold"]==fold and r["seed"]==seed]))
            per_fac=rows_for_metrics(target,scores,fold,seed)
            metrics.extend(per_fac)
            gr=group_diagnostics(target,scores,aux_models,fold,seed)
            if gr:
                df=pd.DataFrame(gr)
                df.to_csv(group_path,mode="a",header=not group_path.exists(),index=False)
                gg=df.groupby("model",as_index=False).agg(groups=("size","size"),
                    observed_count=("observed_failures","mean"),
                    expected_count=("expected_failures","mean"),
                    count_mae=("count_abs_error","mean"),
                    observed_any=("any_observed","mean"),
                    predicted_any=("any_predicted","mean"),
                    observed_multiple=("multi_observed","mean"),
                    predicted_multiple=("multi_predicted","mean"),
                    any_brier=("any_brier","mean"),
                    multi_brier=("multi_brier","mean"),
                    mean_joint_nll=("joint_nll","mean"),
                    mean_peer_gate=("peer_gate","mean"),
                    mean_peer_mass=("peer_mass","mean"),
                    mean_peer_top=("peer_top","mean"),
                    mean_peer_entropy=("peer_entropy","mean"),
                    mean_abs_logit_delta=("mean_abs_logit_delta","mean"),
                    mean_mixture=("mixture","mean"),
                    mean_separation=("state_separation","mean"),
                    mean_excess_count_variance=("excess_correlated_count_variance","mean"),
                    multi_joint_minus_independent=("joint_minus_independent_multi","mean"))
                all_group_stats.extend([dict(fold=fold,seed=seed,**r)
                                         for r in gg.to_dict("records")])
                print_table(f"FOLD {fold} SEED {seed} - JOINT FAILURE ANALYSIS",gg)
            table=pd.DataFrame(per_fac)
            print_table(f"FOLD {fold} SEED {seed} - FACULTY AP/AUC/F2/BRIER",
                table[["faculty","model","rows","failures","prevalence","ap","auc",
                       "precision_at_20","recall_at_20","fbeta_at_20","brier","ece10"]])
            print_table(f"FOLD {fold} SEED {seed} - MACRO COMPARISON",
                table.groupby("model",as_index=False)[["ap","auc","precision_at_20",
                    "recall_at_20","fbeta_at_20","brier","ece10"]].mean())
            if "suite30_joint" in scores and "suite30_portfolio_independent" in scores:
                bs=bootstrap_delta(target,scores["suite30_joint"],
                    scores["suite30_portfolio_independent"],a.bootstrap,seed+fold)
                bootstrap_rows.extend([dict(fold=fold,seed=seed,**r) for r in bs])
                pp=table.pivot(index="faculty",columns="model",values="ap")
                pp["joint_minus_portfolio_independent"]=(
                    pp["suite30_joint"]-pp["suite30_portfolio_independent"])
                if "suite29_set" in pp:pp["joint_minus_suite29_set"]=pp["suite30_joint"]-pp["suite29_set"]
                if "committee" in pp:pp["joint_minus_committee"]=pp["suite30_joint"]-pp["committee"]
                print_table(f"FOLD {fold} SEED {seed} PAIRED AP CHANGES",pp.reset_index())
            group_size=np.zeros(len(target.y),dtype=np.int32)
            for group in EnrollmentDataset(target).groups:group_size[group]=len(group)
            size_bins=np.where(group_size==1,"1",np.where(group_size<=3,"2-3",
                        np.where(group_size<=6,"4-6","7+")))
            for model_name,p in scores.items():
                for fi,fac in enumerate(faculties):
                    for band in ("1","2-3","4-6","7+"):
                        z=(target.faculty_idx==fi)&(size_bins==band)
                        if not z.any() or not np.isfinite(p[z]).all():continue
                        md=metric_dict(target.y[z],p[z])
                        size_metrics.append(dict(fold=fold,seed=seed,faculty=fac,
                           model=model_name,size_bin=band,rows=int(z.sum()),
                           failures=int(target.y[z].sum()),
                           prevalence=float(np.mean(target.y[z])),**md))
            score_rows=dict(fold=fold,seed=seed,
                faculty=np.asarray(faculties,dtype=object)[target.faculty_idx],
                student_id=target.student_id,course_id=target.course_id,
                semester=target.target_semester,row_index=target.row_index,
                observed_set_size=group_size,actual=target.y)
            score_rows.update(scores)
            if "suite30_joint_matched_placebo" in aux_models:
                score_rows["placebo_exact_matched_row"]=aux_models[
                    "suite30_joint_matched_placebo"]["matched_rows"]
            for name,aux in aux_models.items():
                for field in ("peer_gate","peer_mass","peer_top","peer_entropy",
                              "peer_logit_delta","mixture","state_separation",
                              "prob_any","prob_multi","expected_count"):
                    score_rows[name+"__"+field]=aux[field]
            pd.DataFrame(score_rows).to_csv(
                out/f"fold_{fold}_seed_{seed}_scores.csv",index=False)
            save_csv(metrics,out/"all_metrics.csv")
            save_csv(all_group_stats,out/"all_joint_group_summary.csv")
            save_csv(select_rows,out/"all_selection_curves.csv")
            save_csv(selection_faculty,out/"all_selection_faculty_curves.csv")
            save_csv(selected_epochs,out/"all_selected_epochs.csv")
            save_csv(refit_rows,out/"all_refit_losses.csv")
            save_csv(raw_aux,out/"all_portfolio_diagnostics.csv")
            save_csv(calibration_rows,out/"all_historical_calibration.csv")
            save_csv(matched_only,out/"all_matched_peer_deltas.csv")
            save_csv(size_metrics,out/"all_course_size_metrics.csv")
            save_csv(audit,out/"all_history_coverage_audit.csv")
            save_csv(bootstrap_rows,out/"all_bootstrap.csv")
        runtime=time.monotonic()-start
        runtimes.append(dict(fold=fold,seconds=runtime))
        print(f"[TIMING] fold={fold} seconds={runtime:.1f}",flush=True)
    save_csv(runtimes,out/"runtimes.csv")
    from suite30_report import render_report
    render_report(out)
    print(f"\n[DONE] Suite30 results: {out}",flush=True)

if __name__=="__main__":
    main()
