#!/usr/bin/env python3
"""Suite29: single shared, masked enrollment-set model with matched self-only control."""
from __future__ import annotations
import argparse,copy,json,os,random,time,hashlib
from pathlib import Path
import numpy as np
import pandas as pd
import torch
from torch.utils.data import DataLoader
from suite25_data import discover_faculties,parse_faculties,discover_common_feature_names,load_fold_bundle,RetrievalBundle
from suite25_common import metric_dict,safe_ap,print_table,set_seed
from suite29_data import EnrollmentDataset,collate_groups,group_summary,global_chronological_split
from suite29_model import EnrollmentSetNet,masked_bce
from suite25_reporting import load_reference_score,REFERENCE_ALIASES

def args_parser():
    p=argparse.ArgumentParser()
    p.add_argument("--cache-dir",default="/root/disk_3tb/earlywarning/30072026/results/cross_faculty_residual_moe/cache")
    p.add_argument("--structured-dir",default="/root/disk_3tb/earlywarning/30072026/results/suite12_structured_cache")
    p.add_argument("--output-dir",default="/root/disk_3tb/earlywarning/30072026/results/suite29_enrollment_set")
    p.add_argument("--folds",default="1,2,3,4,5,6,7")
    p.add_argument("--faculties",default="all")
    p.add_argument("--modes",default="self_only,set")
    p.add_argument("--seeds",default="29")
    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=5e-4)
    p.add_argument("--weight-decay",type=float,default=1e-4)
    p.add_argument("--width",type=int,default=128)
    p.add_argument("--heads",type=int,default=4)
    p.add_argument("--dropout",type=float,default=.10)
    p.add_argument("--max-courses",type=int,default=0,help="0=no limit; otherwise fail loudly rather than truncate")
    p.add_argument("--device",default="auto")
    p.add_argument("--num-workers",type=int,default=0)
    p.add_argument("--reference",action="append",default=[],help="Reference result name=/absolute/path, repeatable; verifies target count and labels")
    p.add_argument("--save-checkpoints",action="store_true")
    p.add_argument("--bootstrap",type=int,default=100,help="student-cluster bootstrap replicates, 0=disabled")
    p.add_argument("--strict-complete-history-groups",action="store_true",
        help="Fail if CF-history selection removes registrations from any retained semester group")
    return p.parse_args()

def count_binary_failures(values):
    """Count binary events without NumPy int8 scalar accumulation overflow."""
    labels = np.asarray(values)
    if labels.ndim != 1 or not np.all((labels == 0) | (labels == 1)):
        raise ValueError("Failure labels must be a one-dimensional binary array")
    return int(np.count_nonzero(labels))


def subset_rows(bundle,indices):
    indices=np.asarray(indices)
    return RetrievalBundle(**{k:getattr(bundle,k)[indices] for k in
        ("current","y","committee","faculty_idx","blocks","semester_ordinal",
         "semester_z","target_semester","student_id","course_id","row_index")},
        faculty_names=bundle.faculty_names)

class Standardizer:
    def __init__(self,raw):
        x=np.nan_to_num(np.asarray(raw.current,np.float32),nan=0.,posinf=0.,neginf=0.)
        self.mean=x.mean(0);self.scale=x.std(0);self.scale=np.where(self.scale>1e-6,self.scale,1.)
        self.smean=float(np.mean(raw.semester_ordinal))
        self.sscale=max(float(np.std(raw.semester_ordinal)),1.)
    def apply(self,b):
        x=np.nan_to_num(np.asarray(b.current,np.float32),nan=0.,posinf=0.,neginf=0.)
        return RetrievalBundle((x-self.mean)/self.scale,b.y,b.committee,b.faculty_idx,b.blocks,
            b.semester_ordinal,((b.semester_ordinal-self.smean)/self.sscale).astype(np.float32),
            b.target_semester,b.student_id,b.course_id,b.row_index,b.faculty_names)

def loader(bundle,idx,a,shuffle):
    ds=EnrollmentDataset(bundle,idx,a.max_courses)
    if len(ds)==0:raise ValueError("No complete enrollment sets for this split")
    ld=DataLoader(ds,batch_size=a.group_batch_size,shuffle=shuffle,
        collate_fn=collate_groups,num_workers=a.num_workers)
    return ds,ld

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

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

def train_epoch(net,ld,opt,mode,device):
    net.train();loss_sum=0.;n=0;count=0
    for bt in ld:
        b=move(bt,device);opt.zero_grad(set_to_none=True)
        logits=net(b["x"],b["valid"],b["faculty"],b["semester_z"],mode=mode)
        loss=masked_bce(logits,b["y"],b["valid"])
        loss.backward();torch.nn.utils.clip_grad_norm_(net.parameters(),1.)
        opt.step()
        count+=1;num=int(b["valid"].sum());loss_sum+=float(loss.detach())*num;n+=num
    return loss_sum/max(n,1),n,count

@torch.no_grad()
def predict(net,ld,mode,device,nrows,diag=True,collect_rows=False):
    net.eval();scores=np.full(nrows,np.nan,dtype=float);selfm=[];peerm=[];nullm=[]
    row_att={k:np.full(nrows,np.nan,dtype=np.float32) for k in ('self','peer','null')} if collect_rows else None
    for bt in ld:
        b=move(bt,device)
        if diag:
            logits,aux=net(b["x"],b["valid"],b["faculty"],b["semester_z"],mode,True)
            n=int(b["valid"].sum());selfm.append((float(aux["self_mass"]),n))
            peerm.append((float(aux["peer_mass"]),n));nullm.append((float(aux["null_mass"]),n))
        else: logits=net(b["x"],b["valid"],b["faculty"],b["semester_z"],mode)
        indices=bt["index"][bt["valid"]].numpy()
        if np.isfinite(scores[indices]).any():raise AssertionError("Predicted an enrollment row twice")
        scores[indices]=torch.sigmoid(logits[b["valid"]]).cpu().numpy()
        if collect_rows:
            if not diag:raise ValueError('collect_rows requires diagnostics')
            for key in row_att:
                row_att[key][indices]=aux[f'row_{key}_mass'][b['valid']].detach().cpu().numpy()
    def weighted(rows):
        return float(sum(x*w for x,w in rows)/max(sum(w for x,w in rows),1)) if rows else np.nan
    report=dict(self_mass=weighted(selfm),peer_mass=weighted(peerm),null_mass=weighted(nullm))
    if collect_rows:
        if any(not np.isfinite(v).all() for v in row_att.values()):
            raise AssertionError('Missing per-registration attention diagnostics')
        report['_per_row']=row_att
    return scores,report

def macro(bundle,score,mask):
    aps=[];f2=[];brier=[]
    for i in range(len(bundle.faculty_names)):
        z=mask&(bundle.faculty_idx==i)&np.isfinite(score)
        if not z.any():continue
        mm=metric_dict(bundle.y[z],score[z])
        aps.append(mm["ap"]);f2.append(mm["fbeta_at_20"]);brier.append(mm["brier"])
    return (float(np.nanmean(aps)),float(np.nanmean(f2)),float(np.nanmean(brier)))

def rows_for_metrics(bundle,scores,fold,seed,tag="target"):
    out=[]
    for name,sc in scores.items():
        for i,fac in enumerate(bundle.faculty_names):
            faculty_rows=bundle.faculty_idx==i
            if faculty_rows.any() and not np.isfinite(sc[faculty_rows]).all():
                print(f'[INCOMPLETE MODEL COVERAGE] fold={fold} faculty={fac} model={name}; '
                      'not reporting an incomparable subset metric',flush=True)
                continue
            z=faculty_rows
            if not z.any():continue
            mm=metric_dict(bundle.y[z],sc[z])
            out.append(dict(fold=fold,seed=seed,phase=tag,faculty=fac,model=name,
                rows=int(z.sum()),failures=int(bundle.y[z].sum()),prevalence=float(np.mean(bundle.y[z])),**mm))
    return out

def per_group_diagnostics(bundle,scores,fold,seed):
    base=EnrollmentDataset(bundle)
    rows=[]
    for group in base.groups:
        n=len(group);y=bundle.y[group];fi=int(bundle.faculty_idx[group[0]])
        for mode,p in scores.items():
            if not np.isfinite(p[group]).all():continue
            rows.append(dict(fold=fold,seed=seed,faculty=bundle.faculty_names[fi],model=mode,
                size=n,student_id=str(bundle.student_id[group[0]]),semester=float(bundle.target_semester[group[0]]),
                observed_failures=int(y.sum()),expected_failures=float(p[group].sum()),
                absolute_count_error=float(abs(y.sum()-p[group].sum())),
                brier=float(np.mean((p[group]-y)**2)),
                any_failure=int(y.sum()>0),predicted_any_independent=float(1-np.prod(1-p[group]))))
    return rows

def selection_phase(hist,a,seed,mode,device,fold):
    tr,va,split_info=global_chronological_split(hist,a.validation_blocks)
    if not va.any():raise ValueError("No chronological selection blocks")
    # Whole group integrity; neither train nor validation may get partial group.
    for grp in EnrollmentDataset(hist).groups:
        if tr[grp].any() and va[grp].any():
            raise ValueError("Temporal selection divides one student-semester set")
    prep=Standardizer(subset_rows(hist,np.where(tr)[0]))
    normalized=prep.apply(hist)
    tr_ds,tr_ld=loader(normalized,np.where(tr)[0],a,True)
    va_ds,va_ld=loader(normalized,np.where(va)[0],a,False)
    print(f'[GLOBAL TEMPORAL CUTOFF] {json.dumps(split_info)}',flush=True)
    for fi,name in enumerate(hist.faculty_names):
        print(f'[SELECTION COVERAGE] faculty={name} '
              f'train={int(np.sum(tr & (hist.faculty_idx==fi)))} '
              f'validation={int(np.sum(va & (hist.faculty_idx==fi)))}',flush=True)
    print(f"[SELECT DATA] mode={mode} rows train={tr.sum():,} val={va.sum():,} "
          f"train_groups={group_summary(tr_ds)} 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;bad=0;curves=[];faculty_curves=[]
    for epoch in range(1,a.epochs+1):
        loss,n,nb=train_epoch(net,tr_ld,opt,mode,device)
        score,att=predict(net,va_ld,mode,device,len(hist.y))
        ap,f2,br=macro(hist,score,va)
        curves.append(dict(epoch=epoch,loss=loss,macro_ap=ap,macro_f2_at20=f2,brier=br))
        for fi,fac in enumerate(hist.faculty_names):
            z=va&(hist.faculty_idx==fi)
            if not np.any(z):continue
            md=metric_dict(hist.y[z],score[z])
            faculty_curves.append(dict(fold=fold,seed=seed,model=f'suite29_{mode}',
                epoch=epoch,faculty=fac,rows=int(z.sum()),failures=int(hist.y[z].sum()),
                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={"key":key,"epoch":epoch,"state":copy.deepcopy(
                {k:v.detach().cpu().clone() for k,v in net.state_dict().items()})}
            bad=0
        else:bad+=1
        print(f"[SELECT] {mode} epoch={epoch:02d}/{a.epochs} loss={loss:.5f} macroAP={ap:.6f} "
              f"F2@20={f2:.6f} Brier={br:.6f} best={best['epoch']} patience={bad} attention={att}",flush=True)
        if epoch>=a.min_epochs and bad>=a.patience:break
    return best,curves,faculty_curves

def refit(hist,target,a,seed,mode,epochs,device):
    prep=Standardizer(hist);h=prep.apply(hist);t=prep.apply(target)
    tr_ds,tr_ld=loader(h,None,a,True)
    te_ds,te_ld=loader(t,None,a,False)
    print(f"[REFIT DATA] {mode} history={len(h.y):,} target={len(t.y):,} "
          f"history_groups={group_summary(tr_ds)} target_groups={group_summary(te_ds)}",flush=True)
    set_seed(seed);net=network(h,a,device)
    opt=torch.optim.AdamW(net.parameters(),lr=a.lr,weight_decay=a.weight_decay)
    for ep in range(1,epochs+1):
        loss,n,nb=train_epoch(net,tr_ld,opt,mode,device)
        if ep==1 or ep==epochs or ep%5==0:
            print(f"[REFIT] {mode} epoch={ep:02d}/{epochs} BCE={loss:.5f} rows={n:,} batches={nb}",flush=True)
    p,att=predict(net,te_ld,mode,device,len(t.y),collect_rows=True)
    if not np.isfinite(p).all():raise AssertionError("Missing or duplicate target predictions")
    if mode=="set":
        without_peer,_=predict(net,te_ld,"self_only",device,len(t.y),False)
    else:without_peer=None
    return net,prep,p,att,without_peer


def audit_history_group_completeness(cache_dir,faculties,fold,strict=False):
    """Compare retained OOF rows to all canonical historical registrations.

    The Suite25 loader consumes CF-covered historical rows; if OOF coverage
    removes only *part* of a student-semester, attention sees a partial set.
    These counts are observable from the original canonical cache itself.
    """
    import joblib
    results=[]
    for faculty in faculties:
        d=joblib.load(Path(cache_dir)/f'{faculty}_fold_{fold}.joblib')
        fr=d['train_frame']
        valid=np.asarray(d['cf_covered_mask'],bool) & np.isfinite(np.asarray(d['cf_probabilities'])).all(1)
        if len(valid)!=len(fr):raise ValueError(f'CF mask/frame length mismatch: {faculty} F{fold}')
        ids=fr['IDSV'].astype(str).to_numpy()
        sem=fr['target_NHHK'].astype(str).to_numpy()
        full={}
        kept={}
        for i in range(len(fr)):
            key=(ids[i],sem[i]);full[key]=full.get(key,0)+1
            if valid[i]:kept[key]=kept.get(key,0)+1
        partial=[(key,n) for key,n in kept.items() if n<full[key]]
        affected=int(sum(n for _,n in partial))
        item=dict(fold=fold,faculty=faculty,full_history_rows=len(fr),
            covered_history_rows=int(valid.sum()),full_groups=len(full),
            retained_groups=len(kept),partial_groups=len(partial),
            rows_in_partial_groups=affected,
            fraction_retained_rows_in_partial_groups=affected/max(int(valid.sum()),1))
        results.append(item)
        print('[HISTORY ENROLLMENT COVERAGE]',json.dumps(item),flush=True)
        if strict and partial:
            raise ValueError(f'Incomplete history enrollment sets: {faculty} F{fold}; {item}')
    return results


def attention_strata(bundle,att,fold,seed,model):
    """True-outcome and observed-set-size diagnostics for each faculty."""
    row=att.get('_per_row')
    if row is None:return []
    group_size=np.zeros(len(bundle.y),dtype=int)
    for grp in EnrollmentDataset(bundle).groups:
        group_size[grp]=len(grp)
    bins=np.where(group_size==1,'1',np.where(group_size<=3,'2-3',
          np.where(group_size<=6,'4-6','7+')))
    out=[]
    for fi,faculty in enumerate(bundle.faculty_names):
        faculty_mask=bundle.faculty_idx==fi
        for size_bin in ('1','2-3','4-6','7+'):
            for outcome in (0,1):
                z=faculty_mask&(bins==size_bin)&(bundle.y==outcome)
                if not np.any(z):continue
                out.append(dict(fold=fold,seed=seed,model=model,faculty=faculty,
                    size_bin=size_bin,outcome=outcome,rows=int(z.sum()),
                    mean_self_mass=float(row['self'][z].mean()),
                    mean_peer_mass=float(row['peer'][z].mean()),
                    mean_null_mass=float(row['null'][z].mean())))
    return out

def bootstrap_delta(bundle,p_set,p_base,reps,seed):
    rng=np.random.default_rng(seed);result=[]
    if reps<1:return result
    for i,fac in enumerate(bundle.faculty_names):
        z=np.where(bundle.faculty_idx==i)[0]
        if not len(z):continue
        ids=np.asarray(bundle.student_id[z]).astype(str);uniq,inv=np.unique(ids,return_inverse=True)
        if len(uniq)<3:continue
        loc=[z[np.where(inv==j)[0]] for j in range(len(uniq))]
        deltas=[]
        for _ in range(reps):
            sampled=rng.integers(0,len(uniq),len(uniq))
            ix=np.concatenate([loc[j] for j in sampled])
            if len(np.unique(bundle.y[ix]))<2:continue
            deltas.append(safe_ap(bundle.y[ix],p_set[ix])-safe_ap(bundle.y[ix],p_base[ix]))
        if deltas:result.append(dict(faculty=fac,bootstrap_reps=len(deltas),
            observed_delta_ap=safe_ap(bundle.y[z],p_set[z])-safe_ap(bundle.y[z],p_base[z]),
            ci_lo=float(np.quantile(deltas,.025)),ci_hi=float(np.quantile(deltas,.975))))
    return result

def save_csv(rows,path):
    if rows:pd.DataFrame(rows).to_csv(path,index=False)

def target_group_profile(bundle,fold):
    """Descriptive, target-only enrollment composition (never input new labels)."""
    ds=EnrollmentDataset(bundle)
    rows=[]
    for fi,faculty in enumerate(bundle.faculty_names):
        grps=[g for g in ds.groups if int(bundle.faculty_idx[g[0]])==fi]
        if not grps:continue
        sizes=np.asarray([len(g) for g in grps],dtype=int)
        failures=np.asarray([int(np.sum(bundle.y[g])) for g in grps],dtype=int)
        rows.append(dict(fold=fold,faculty=faculty,registrations=int(sizes.sum()),
            groups=len(grps),singletons=int(np.sum(sizes==1)),
            singleton_fraction=float(np.mean(sizes==1)),
            avg_courses=float(np.mean(sizes)),median_courses=float(np.median(sizes)),
            max_courses=int(sizes.max()),groups_with_any_failure=int(np.sum(failures>0)),
            groups_with_multiple_failures=int(np.sum(failures>=2)),
            total_failures=int(failures.sum()),
            failure_share_in_multi_failure_groups=float(np.sum(failures[failures>=2])/max(np.sum(failures),1))))
    return rows

def main():
    a=args_parser();out=Path(a.output_dir);out.mkdir(parents=True,exist_ok=True)
    folds=[int(v) for v in a.folds.split(",")];seeds=[int(v) for v in a.seeds.split(",")]
    modes=[v.strip() for v in a.modes.split(",") if v.strip()]
    if any(v not in ("set","self_only") for v in modes):raise ValueError(modes)
    if a.device=="auto":device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
    else:device=torch.device(a.device)
    faculties=parse_faculties(a.faculties,discover_faculties(a.cache_dir,folds[0]))
    common=discover_common_feature_names(a.cache_dir,faculties,folds[0])
    if len(common)!=136:print(f"[WARN] common features={len(common)}, not assumed to equal 136")
    print(f"[START] Suite29 device={device} torch={torch.__version__} faculties={len(faculties)} "
          f"folds={folds} seeds={seeds} modes={modes} common_features={len(common)}",flush=True)
    (out/"config.json").write_text(json.dumps(vars(a),indent=2))
    from suite25_data import COURSE_FEATURES
    (out/"features.json").write_text(json.dumps(dict(common=common,course_features=COURSE_FEATURES),indent=2))
    metrics=[];selection=[];selection_faculty=[];attentions=[];attention_strata_rows=[];boostrap=[];times=[];group_size_summaries=[]
    history_audit=[];enrollment_profiles=[]
    gr_path=out/'all_group_diagnostics.csv'
    if gr_path.exists(): gr_path.unlink()
    for fold in folds:
        start=time.time()
        print(f"\n{'='*90}\nSUITE29 TARGET FOLD {fold} - {len(faculties)} FACULTIES\n{'='*90}",flush=True)
        hist,target,names=load_fold_bundle(a.cache_dir,a.structured_dir,faculties,fold,common)
        history_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'[CROSS-FACULTY TARGET CHRONOLOGY] F{fold}: '
              f'max historical semester {np.max(hist.semester_ordinal)} >= '
              f'min target semester {np.min(target.semester_ordinal)}. '
              'A prospective shared model cannot train on outcomes at/after its earliest target. '
              'Investigate cross-faculty chronology before proceeding.')
        print(f'[TARGET TEMPORAL AUDIT] max_history={np.max(hist.semester_ordinal)} '
              f'min_target={np.min(target.semester_ordinal)}',flush=True)
        history_failures = count_binary_failures(hist.y)
        target_failures = count_binary_failures(target.y)
        print(f"[DATA] history={len(hist.y):,} failures={history_failures:,}, "
              f"target={len(target.y):,} failures={target_failures:,}, "
              f"dim={hist.current.shape[1]}", flush=True)
        if len(faculties) < len(discover_faculties(a.cache_dir,fold)):
            print('[SMOKE FEATURE CONTRACT] Only selected faculties contribute to '
                  'the raw-feature intersection. This input dimension may differ '
                  'from the full all-faculty experiment; smoke metrics must not '
                  'be compared as full-run results.', flush=True)
        new_profiles=target_group_profile(target,fold)
        enrollment_profiles.extend(new_profiles)
        print_table(f'SUITE29 FOLD {fold} — ENROLLMENT-SET PROFILE BY FACULTY',
                    pd.DataFrame(new_profiles))
        save_csv(enrollment_profiles,out/'all_target_enrollment_profiles.csv')
        if not a.reference:
            print('[REFERENCE] Committee is always included. Historical Suite22/Suite25 references are opt-in '
                  'via --reference NAME=/path; no external predictions are silently assumed aligned.',flush=True)
        for seed in seeds:
            scores={"committee":target.committee.copy()}
            for spec in a.reference:
                if "=" not in spec: raise ValueError("--reference requires NAME=PATH")
                refname,refroot=spec.split("=",1)
                refscore=np.full(len(target.y),np.nan,float)
                for fi,fac in enumerate(target.faculty_names):
                    rr=np.where(target.faculty_idx==fi)[0]
                    try:
                        rp,path,col=load_reference_score(refroot,fold,fac,target.y[rr],
                           REFERENCE_ALIASES.get(refname,[refname,"final"]))
                        if rp is None:
                            print(f"[REFERENCE MISSING] {refname} fold={fold} faculty={fac} root={refroot}")
                        else:
                            refscore[rr]=rp
                            print(f"[REFERENCE OK] {refname} fold={fold} faculty={fac} file={path} column={col}")
                    except Exception as ex:
                        print(f"[REFERENCE UNAVAILABLE] {refname} fold={fold} faculty={fac}: {ex}")
                if np.isfinite(refscore).any():scores["reference_"+refname]=refscore
            for mode in modes:
                sel,curves,fc=selection_phase(hist,a,seed,mode,device,fold)
                for r in curves:selection.append(dict(fold=fold,seed=seed,model=f"suite29_{mode}",**r))
                selection_faculty.extend(fc)
                print(f"[SELECTION] F{fold} {mode} chosen epoch={sel['epoch']} "
                      f"key={sel['key']}",flush=True)
                net,prep,p,att,ablate=refit(hist,target,a,seed,mode,sel["epoch"],device)
                scores[f"suite29_{mode}"]=p
                attention_strata_rows.extend(attention_strata(
                    target,att,fold,seed,f'suite29_{mode}'))
                scalar_att={key:value for key,value in att.items() if not key.startswith('_')}
                attentions.append(dict(fold=fold,seed=seed,model=f"suite29_{mode}",**scalar_att))
                if ablate is not None:scores["suite29_set_no_peers"]=ablate
                if a.save_checkpoints:
                    torch.save(dict(model=net.state_dict(),args=vars(a),fold=fold,seed=seed,
                        mode=mode,faculties=list(hist.faculty_names),features=list(names),
                        selected_epochs=sel['epoch'],model_config=dict(
                            n_features=int(hist.current.shape[1]),n_faculties=len(hist.faculty_names),
                            width=a.width,heads=a.heads,dropout=a.dropout),
                        preprocessing=dict(mean=prep.mean.astype(np.float32),
                            scale=prep.scale.astype(np.float32),smean=prep.smean,sscale=prep.sscale)),
                        out/f'fold{fold}_{mode}_seed{seed}.pt')
                del net
                if device.type=="cuda":torch.cuda.empty_cache()
            mm=rows_for_metrics(target,scores,fold,seed)
            metrics.extend(mm)
            gr=per_group_diagnostics(target,scores,fold,seed)
            if gr:
                gr_df=pd.DataFrame(gr)
                gr_df.to_csv(gr_path,mode='a',header=not gr_path.exists(),index=False)
                gr_df['size_bin']=pd.cut(gr_df['size'],bins=[0,1,3,6,np.inf],
                    labels=['1','2-3','4-6','7+'])
                agg=gr_df.groupby(['faculty','model','size_bin'],observed=True).agg(
                    groups=('size','size'),registrations=('size','sum'),
                    mean_observed_failures=('observed_failures','mean'),
                    mean_expected_failures=('expected_failures','mean'),
                    mean_absolute_count_error=('absolute_count_error','mean'),
                    mean_registration_brier=('brier','mean')).reset_index()
                group_size_summaries.extend([dict(fold=fold,seed=seed,**row)
                    for row in agg.to_dict('records')])
            print_table(f"SUITE29 FOLD {fold} SEED {seed} - FACULTY RESULTS",
                pd.DataFrame(mm)[["faculty","model","rows","failures","prevalence", "ap", "auc",
                    "precision_at_20","recall_at_20","fbeta_at_20","brier","ece10"]])
            print_table(f"SUITE29 FOLD {fold} SEED {seed} - MACRO",
                pd.DataFrame(mm).groupby("model",as_index=False)[["ap","auc", "precision_at_20",
                    "recall_at_20", "fbeta_at_20", "brier", "ece10"]].mean())
            if gr:
                g_summary=(pd.DataFrame(gr).groupby('model',as_index=False).agg(
                    groups=('size','size'), mean_observed_failures=('observed_failures','mean'),
                    mean_expected_failures=('expected_failures','mean'),
                    mean_absolute_failure_count_error=('absolute_count_error','mean')))
                print_table(f'SUITE29 FOLD {fold} SEED {seed} - STUDENT/SEMESTER FAILURE COUNT INSIGHTS',g_summary)
            current_att=[r for r in attention_strata_rows if r['fold']==fold and r['seed']==seed
                and r['model']=='suite29_set']
            if current_att:
                detail=pd.DataFrame(current_att)
                strat=[]
                for (faculty,size_bin), g in detail.groupby(['faculty','size_bin']):
                    weights=g['rows'].to_numpy()
                    strat.append(dict(faculty=faculty,size_bin=size_bin,rows=int(weights.sum()),
                        self_mass=float(np.average(g.mean_self_mass,weights=weights)),
                        peer_mass=float(np.average(g.mean_peer_mass,weights=weights)),
                        null_mass=float(np.average(g.mean_null_mass,weights=weights))))
                print_table(f'SUITE29 FOLD {fold} SEED {seed} - PEER ATTENTION BY FACULTY/SET SIZE',
                    pd.DataFrame(strat))
            if "suite29_set" in scores and "suite29_self_only" in scores:
                bs=bootstrap_delta(target,scores["suite29_set"],scores["suite29_self_only"],a.bootstrap,seed)
                boostrap.extend([dict(fold=fold,seed=seed,**r) for r in bs])
                d=pd.DataFrame(mm).pivot(index="faculty",columns="model",values="ap")
                d["delta_set_minus_control"]=d["suite29_set"]-d["suite29_self_only"]
                print_table("SUITE29 PAIRED FACULTY AP DELTAS",d.reset_index())
            observed_group_size=np.zeros(len(target.y),dtype=np.int32)
            for grp in EnrollmentDataset(target).groups:observed_group_size[grp]=len(grp)
            score_df=pd.DataFrame(dict(fold=fold,seed=seed,faculty=np.asarray(faculties)[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=observed_group_size,actual=target.y))
            for k,v in scores.items():score_df[k]=v
            if 'suite29_set' in scores and 'suite29_self_only' in scores:
                score_df['delta_set_minus_self_probability']=scores['suite29_set']-scores['suite29_self_only']
            if 'suite29_set_no_peers' in scores:
                score_df['delta_set_peer_intervention']=scores['suite29_set']-scores['suite29_set_no_peers']
            score_df.to_csv(out/f"fold_{fold}_seed_{seed}_scores.csv",index=False)
            save_csv(metrics,out/"all_metrics.csv")
            save_csv(selection,out/"all_selection_curves.csv")
            save_csv(selection_faculty,out/"all_selection_faculty_curves.csv")
            save_csv(attentions,out/"all_attention_diagnostics.csv")
            save_csv(attention_strata_rows,out/"all_attention_stratified.csv")
            save_csv(group_size_summaries,out/"all_group_size_summaries.csv")
            save_csv(history_audit,out/"all_history_coverage_audit.csv")
            save_csv(boostrap,out/"all_bootstrap.csv")
        times.append(dict(fold=fold,seconds=time.time()-start))
        print(f"[TIMING] fold={fold} seconds={times[-1]['seconds']:.1f}",flush=True)
    summary=pd.DataFrame(metrics)
    summary.groupby(["model","seed"],as_index=False)[["ap","auc","fbeta_at_20","brier"]].mean().to_csv(out/"macro_all_folds.csv",index=False)
    held=summary[summary.fold.isin([6,7])]
    if len(held):
        held.groupby(["model","seed"],as_index=False)[["ap","auc","fbeta_at_20","brier"]].mean().to_csv(
            out/"macro_folds_6_7_retrospective.csv",index=False)
    save_csv(times,out/"runtimes.csv")
    full_cells=len(folds)*len(faculties)
    coverage=summary.groupby(['model','seed'],as_index=False).agg(
        reported_cells=('faculty','size'),n_faculties=('faculty','nunique'),
        mean_ap=('ap','mean'),mean_f2_at20=('fbeta_at_20','mean'))
    coverage['expected_cells']=full_cells
    coverage['complete_coverage']=coverage.reported_cells==full_cells
    coverage.to_csv(out/'coverage_manifest.csv',index=False)
    print_table('COVERAGE MANIFEST — ONLY FULL-COVERAGE MODELS ARE FAIRLY COMPARABLE',coverage)
    if {'suite29_set','suite29_self_only'}.issubset(set(summary.model)):
        a_rows=summary[summary.model=='suite29_set'][['fold','seed','faculty','ap','auc','fbeta_at_20']]
        b_rows=summary[summary.model=='suite29_self_only'][['fold','seed','faculty','ap','auc','fbeta_at_20']]
        paired=a_rows.merge(b_rows,on=['fold','seed','faculty'],suffixes=('_set','_self'))
        paired['delta_ap']=paired.ap_set-paired.ap_self
        paired['delta_auc']=paired.auc_set-paired.auc_self
        paired['delta_f2_at20']=paired.fbeta_at_20_set-paired.fbeta_at_20_self
        paired.to_csv(out/'paired_set_vs_self.csv',index=False)
        print_table('SUITE29 PAIRED FOLD/FACULTY AP DELTAS',paired[['fold','seed','faculty','delta_ap','delta_auc','delta_f2_at20']])
        print('[PAIRED SUMMARY] cells=',len(paired),'wins=',int((paired.delta_ap>0).sum()),
              'losses=',int((paired.delta_ap<0).sum()),
              'ties=',int((paired.delta_ap==0).sum()),
              'mean_delta_ap=',float(paired.delta_ap.mean()),flush=True)
    from suite29_report import render_report
    render_report(out)
    print("\n[DONE] Suite29 outputs:",out,flush=True)

if __name__=="__main__":main()
