# -*- coding: utf-8 -*-
from __future__ import annotations
import argparse, copy, gc, os, time
from pathlib import Path
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader, WeightedRandomSampler

from suite23_common import *
from suite23_graph_index import GraphEvidenceIndex, materialize_graphs, SL_HISTORY, SL_COHORT, MAX_HISTORY_COURSES, MAX_COHORT_STUDENTS
from suite23_data import GraphDataset, PerturbedGraphDataset, same_faculty_permutation, same_faculty_course_permutation, fit_node_stats, graph_support_frame
from suite23_model import CSHAGT
from suite23_reporting import *

VARIANTS=[('bipartite','suite23_bipartite_graph'),('hetero','suite23_heterogeneous_graph'),('full','suite23_cs_hagt')]


def move_batch(batch,device):
    return {k:(v.to(device,non_blocking=True) if torch.is_tensor(v) else v) for k,v in batch.items()}


def pairwise_rank_loss(logits,y,faculty,max_pairs=2048):
    loss=[]
    for f in torch.unique(faculty):
        m=faculty==f;pos=torch.where(m&(y>0.5))[0];neg=torch.where(m&(y<=0.5))[0]
        if not len(pos) or not len(neg):continue
        n=min(max_pairs,max(len(pos),len(neg))*4)
        ip=pos[torch.randint(len(pos),(n,),device=logits.device)];ineg=neg[torch.randint(len(neg),(n,),device=logits.device)]
        loss.append(F.softplus(-(logits[ip]-logits[ineg])).mean())
    return torch.stack(loss).mean() if loss else logits.sum()*0


def train_epoch(model,loader,opt,device,rank_weight=.15):
    model.train();tot=0.;n=0
    for batch in loader:
        b=move_batch(batch,device);opt.zero_grad(set_to_none=True);z=model(b);bce=F.binary_cross_entropy_with_logits(z,b['y']);rank=pairwise_rank_loss(z,b['y'],b['faculty_idx']);loss=bce+float(rank_weight)*rank;loss.backward();torch.nn.utils.clip_grad_norm_(model.parameters(),5.0);opt.step();tot+=float(loss.detach())*len(z);n+=len(z)
    return tot/max(n,1)

@torch.no_grad()
def predict_model(model,dataset,device,batch_size=1024,ablate=None,return_aux=False):
    model.eval();loader=DataLoader(dataset,batch_size=batch_size,shuffle=False,num_workers=0,pin_memory=True);scores=[];aux_rows=[];indices=[]
    for batch in loader:
        b=move_batch(batch,device)
        if return_aux:
            z,aux=model(b,return_aux=True,ablate=ablate);aux_rows.append({k:v.detach().cpu() for k,v in aux.items() if torch.is_tensor(v)})
        else:z=model(b,ablate=ablate)
        scores.append(torch.sigmoid(z).cpu().numpy());indices.append(batch['bundle_index'].numpy())
    s=np.concatenate(scores) if scores else np.zeros(0);idx=np.concatenate(indices) if indices else np.zeros(0,int)
    if not return_aux:return s,idx
    merged={k:torch.cat([a[k] for a in aux_rows],0).numpy() for k in aux_rows[0]} if aux_rows else {};return s,idx,merged


def validation_metrics(bundle,mask,score):
    aps=[];f2=[];br=[]
    for fi,_ in enumerate(bundle.faculty_names):
        m=np.asarray(mask)&(bundle.faculty_idx==fi)
        if not m.any():continue
        aps.append(safe_ap(bundle.y[m],score[m]));md=metric_dict(bundle.y[m],score[m]);f2.append(md['fbeta_at_20']);br.append(md['brier'])
    return float(np.nanmean(aps)),float(np.nanmean(f2)),float(np.nanmean(br))


def selection_train(variant,store,bundle,tr_mask,va_mask,args,device,seed):
    tr_idx=np.where(tr_mask)[0];va_idx=np.where(va_mask)[0];mean,std,_=fit_node_stats(store,tr_idx,args.norm_rows,seed)
    set_seed(seed);model=CSHAGT(variant,args.d_model,args.heads,args.layers,args.dropout,len(bundle.faculty_names),mean,std).to(device);opt=torch.optim.AdamW(model.parameters(),lr=args.lr,weight_decay=args.weight_decay)
    trds=GraphDataset(store,bundle,tr_idx);weights=faculty_sampling_weights(bundle.faculty_idx[tr_idx],len(bundle.faculty_names));sampler=WeightedRandomSampler(torch.as_tensor(weights,dtype=torch.double),num_samples=min(int(args.samples_per_epoch),max(len(tr_idx),1)) if args.samples_per_epoch>0 else len(tr_idx),replacement=True);loader=DataLoader(trds,batch_size=args.batch_size,sampler=sampler,num_workers=0,pin_memory=True,drop_last=False)
    vads=GraphDataset(store,bundle,va_idx);best=None;bad=0;history=[]
    for ep in range(1,args.max_epochs+1):
        loss=train_epoch(model,loader,opt,device,args.rank_weight);pv,idx=predict_model(model,vads,device,args.eval_batch_size);full=np.full(len(bundle.y),np.nan);full[idx]=pv;ap,f2,br=validation_metrics(bundle,va_mask,full);key=(ap,f2,-br)
        history.append({'epoch':ep,'loss':loss,'macro_ap':ap,'macro_f2_at20':f2,'brier':br});isbest=best is None or key>best['key']
        if isbest:best={'key':key,'epoch':ep,'state':copy.deepcopy({k:v.detach().cpu() for k,v in model.state_dict().items()}),'val_score':full.copy(),'history':None};bad=0
        elif ep>=args.min_epochs:bad+=1
        print(f'[SELECT {variant.upper()}] epoch={ep:02d}/{args.max_epochs} loss={loss:.5f} macroAP={ap:.4f} F2@20={f2:.4f} Brier={br:.4f} best={best["epoch"]:02d}')
        if ep>=args.min_epochs and bad>=args.patience:break
    best['history']=history;model.load_state_dict(best['state']);return best,mean,std


def refit_predict(variant,store_hist,bundle_hist,store_target,bundle_target,epochs,args,device,seed):
    idx=np.arange(len(bundle_hist.y));mean,std,_=fit_node_stats(store_hist,idx,args.norm_rows,seed);set_seed(seed);model=CSHAGT(variant,args.d_model,args.heads,args.layers,args.dropout,len(bundle_hist.faculty_names),mean,std).to(device);opt=torch.optim.AdamW(model.parameters(),lr=args.lr,weight_decay=args.weight_decay)
    ds=GraphDataset(store_hist,bundle_hist,idx);weights=faculty_sampling_weights(bundle_hist.faculty_idx,len(bundle_hist.faculty_names));sampler=WeightedRandomSampler(torch.as_tensor(weights,dtype=torch.double),num_samples=min(int(args.samples_per_epoch),max(len(idx),1)) if args.samples_per_epoch>0 else len(idx),replacement=True);loader=DataLoader(ds,batch_size=args.batch_size,sampler=sampler,num_workers=0,pin_memory=True)
    for ep in range(1,int(epochs)+1):
        loss=train_epoch(model,loader,opt,device,args.rank_weight)
        if ep==1 or ep==epochs:print(f'[REFIT {variant.upper()}] epoch={ep:02d}/{epochs} loss={loss:.5f}')
    p,ii=predict_model(model,GraphDataset(store_target,bundle_target),device,args.eval_batch_size);full=np.full(len(bundle_target.y),np.nan);full[ii]=p;return model,full


def policy_masks(bundle,val_mask):
    cal=np.zeros(len(bundle.y),bool);pol=np.zeros(len(bundle.y),bool)
    for fi in range(len(bundle.faculty_names)):
        m=val_mask&(bundle.faculty_idx==fi);blocks=sorted(pd.unique(bundle.blocks[m]))
        if not blocks:continue
        if len(blocks)==1:cal[m]=True;pol[m]=True
        else:
            pol[m&(bundle.blocks==blocks[-1])]=True;cal[m&np.isin(bundle.blocks,blocks[:-1])]=True
    return cal,pol


def per_faculty_scores(bundle,scores):
    out={}
    for fi,fac in enumerate(bundle.faculty_names):
        m=bundle.faculty_idx==fi;out[fac]={'y':bundle.y[m],'scores':{k:np.asarray(v)[m] for k,v in scores.items() if np.isfinite(np.asarray(v)[m]).all()}}
    return out


def print_support(fold,bundle,store,label):
    s=np.asarray(store.data['support']);rows=[]
    for fi,fac in enumerate(bundle.faculty_names):
        m=bundle.faculty_idx==fi;rows.append({'faculty':fac,'rows':int(m.sum()),'prevalence':float(bundle.y[m].mean()),'history_courses_mean':float(s[m,0].mean()),'cohort_students_mean':float(s[m,1].mean()),'structural_courses_mean':float(s[m,2].mean()),'curriculum_groups_mean':float(s[m,3].mean()),'history_zero_rate':float(np.mean(s[m,0]==0)),'cohort_zero_rate':float(np.mean(s[m,1]==0))})
    print_table(f'FOLD {fold} GRAPH SUPPORT — {label}',rows)


def perturbation_metrics(model,store,bundle,base_score,a,b,device,args,fold):
    rows=[];base_ds=GraphDataset(store,bundle);perm_hist=same_faculty_course_permutation(bundle,args.seed+fold*917);perm_cohort=same_faculty_permutation(bundle,args.seed+fold*991)
    for mode in ['zero_history','shuffle_history','zero_cohort','shuffle_cohort']:
        src=None
        if mode.startswith('shuffle'):
            # dataset-local source positions are global target indices because base dataset is unsubsetted
            src=perm_hist if mode=='shuffle_history' else perm_cohort
        ds=PerturbedGraphDataset(base_ds,mode,src);p,idx=predict_model(model,ds,device,args.eval_batch_size);full=np.full(len(bundle.y),np.nan);full[idx]=apply_platt(p,a,b)
        for fi,fac in enumerate(bundle.faculty_names):
            m=bundle.faculty_idx==fi;rows.append({'fold':fold,'faculty':fac,'perturbation':mode,'rows':int(m.sum()),'base_ap':safe_ap(bundle.y[m],base_score[m]),'perturbed_ap':safe_ap(bundle.y[m],full[m]),'delta_ap':safe_ap(bundle.y[m],full[m])-safe_ap(bundle.y[m],base_score[m]),'mean_abs_probability_change':float(np.mean(np.abs(full[m]-base_score[m])))})
    return rows


def ablation_metrics(model,store,bundle,base_score,a,b,device,args,fold):
    rows=[];ds=GraphDataset(store,bundle)
    for name,abl in [('no_structural',['structural']),('no_groups',['groups']),('no_faculty_edges',['faculty_edges']),('no_time',['time']),('no_student_history_readout',['student_history_readout']),('no_course_cohort_readout',['course_cohort_readout'])]:
        p,idx=predict_model(model,ds,device,args.eval_batch_size,ablate=abl);full=np.full(len(bundle.y),np.nan);full[idx]=apply_platt(p,a,b)
        for fi,fac in enumerate(bundle.faculty_names):
            m=bundle.faculty_idx==fi;rows.append({'fold':fold,'faculty':fac,'ablation':name,'rows':int(m.sum()),'base_ap':safe_ap(bundle.y[m],base_score[m]),'ablated_ap':safe_ap(bundle.y[m],full[m]),'delta_ap':safe_ap(bundle.y[m],full[m])-safe_ap(bundle.y[m],base_score[m])})
    return rows


def path_inspection(model,store,bundle,score,index,device,args,fold,out_dir):
    rows=[]
    for fi,fac in enumerate(bundle.faculty_names):
        idx=np.where(bundle.faculty_idx==fi)[0]
        if not len(idx):continue
        top=idx[np.argsort(-score[idx])[:min(args.inspect_rows,len(idx))]]
        ds=GraphDataset(store,bundle,top);p,ii,aux=predict_model(model,ds,device,min(args.eval_batch_size,256),return_aux=True)
        for k,global_i in enumerate(ii):
            g=store[int(global_i)];wh=aux['history_attention'][k];wc=aux['cohort_attention'][k];eh=aux['edge_attention'][k]
            hs=int(np.argmax(wh)) if np.any(wh>0) else -1;cs=int(np.argmax(wc)) if np.any(wc>0) else -1;he=SL_HISTORY.start+hs if hs>=0 else -1;ce=SL_COHORT.start+cs if cs>=0 else -1;ee=int(np.argmax(eh)) if np.any(eh>0) else -1
            hcourse=index.idx_to_course[int(g['node_entity'][he])] if he>=0 and int(g['node_entity'][he])>=0 and int(g['node_entity'][he])<len(index.idx_to_course) else ''
            cstud=index.idx_to_student[int(g['node_entity'][ce])] if ce>=0 and int(g['node_entity'][ce])>=0 and int(g['node_entity'][ce])<len(index.idx_to_student) else ''
            rows.append({'fold':fold,'faculty':fac,'bundle_index':int(global_i),'student_id':bundle.student_id[global_i],'target_course_id':bundle.course_id[global_i],'actual':int(bundle.y[global_i]),'graph_score':float(score[global_i]),'top_history_course':hcourse,'top_history_attention':float(wh[hs]) if hs>=0 else 0.,'top_cohort_student':cstud,'top_cohort_attention':float(wc[cs]) if cs>=0 else 0.,'top_edge_type':int(g['edge_type'][ee]) if ee>=0 else 0,'top_edge_attention':float(eh[ee]) if ee>=0 else 0.})
    pd.DataFrame(rows).to_csv(Path(out_dir)/f'fold_{fold}'/'path_inspection.csv',index=False)


def run(args):
    t0=time.time();out=Path(args.out_dir);out.mkdir(parents=True,exist_ok=True);device=torch.device(args.device if args.device!='auto' else ('cuda' if torch.cuda.is_available() else 'cpu'))
    faculties=parse_faculties(args.faculties,discover_faculties(args.cache_dir,1));folds=[int(x) for x in args.folds.split(',')];dev=[int(x) for x in args.dev_folds.split(',')];hold=[int(x) for x in args.holdout_folds.split(',')]
    print('='*170);print('SUITE23 — CHRONOLOGY-SAFE HETEROGENEOUS ACADEMIC GRAPH TRANSFORMER (CS-HAGT)');print('student-course graph | structural course groups/prerequisites | edge-aware hetero attention | dual relational readout | Committee-preserving faculty-safe fusion');print('='*170)
    print(f'[ENV] device={device} torch={torch.__version__} faculties={len(faculties)} folds={folds} dev={dev} holdout={hold}')
    print('[SCIENTIFIC CONTRACT] A=bipartite graph; B=heterogeneous curriculum/structural graph; C=full CS-HAGT; primary deployable output=faculty-safe Committee fusion; no neural OOF; F6/F7 never select architecture/epochs/calibration/fusion.')
    raw_index=Path(args.structured_cache_dir)/'raw_index';index=GraphEvidenceIndex(raw_index,args.database_dir);print(f'[GRAPH INDEX] interactions={len(index.hp):,} students={len(index.student_to_idx):,} courses={len(index.course_to_idx):,} groups={len(index.group_to_idx):,}')
    all_metrics=[];records=[];reference_meta=[];support_rows=[];novelty_rows=[];support_gain=[];pert_rows=[];abl_rows=[];comp_rows=[];fusion_rows=[];boot_rows=[]
    reference_dirs={'suite14':args.suite14_dir,'suite16':args.suite16_dir,'suite17':args.suite17_dir,'suite18':args.suite18_dir,'suite20':args.suite20_dir,'suite21':args.suite21_dir,'suite22':args.suite22_dir,'suite22_primary':args.suite22_dir,'suite22_relational':args.suite22_dir,'council':args.council_dir,'v2':args.v2_dir}
    for fold in folds:
        fd=out/f'fold_{fold}';fd.mkdir(exist_ok=True);print('\n'+'='*170+f'\nSUITE23 TARGET FOLD {fold}\n'+'='*170)
        hist,target=load_query_bundles(args.cache_dir,faculties,fold);tr,va=latest_block_split(hist.faculty_idx,hist.blocks,args.val_blocks);print(f'[DATA] history={len(hist.y):,} fail={hist.y.sum():,} ({hist.y.mean():.2%}) train={tr.sum():,} validation={va.sum():,} target={len(target.y):,} fail={target.y.sum():,} ({target.y.mean():.2%})')
        print('[GRAPH CACHE] materializing chronology-safe sampled neighborhoods for historical rows...');gh=materialize_graphs(index,hist,args.graph_progress_every);print('[GRAPH CACHE] target...');gt=materialize_graphs(index,target,args.graph_progress_every);print_support(fold,target,gt,'TARGET')
        viol=int(gh.data['chronology_violation'].sum()+gt.data['chronology_violation'].sum());print(f'[CHRONOLOGY AUDIT] violations={viol} / {len(hist.y)+len(target.y):,} | strict max interaction semester < query semester');assert viol==0
        variant_target={};variant_models={};variant_platt={};variant_select={}
        for vi,(variant,name) in enumerate(VARIANTS):
            print('\n'+'-'*130+f'\n[VARIANT {name}]')
            best,_,_=selection_train(variant,gh,hist,tr,va,args,device,args.seed+fold*1000+vi*100);variant_select[name]=best
            # chronological calibration split inside validation
            cal,pol=policy_masks(hist,va);raw_val=best['val_score'];a,b=fit_platt(hist.y[cal],raw_val[cal]);variant_platt[name]=(a,b);cal_val=np.full(len(hist.y),np.nan);cal_val[va]=apply_platt(raw_val[va],a,b)
            pd.DataFrame(best['history']).to_csv(fd/f'{name}_selection_history.csv',index=False);save_json({'selected_epoch':best['epoch'],'platt_a':a,'platt_b':b},fd/f'{name}_selection.json')
            model,praw=refit_predict(variant,gh,hist,gt,target,best['epoch'],args,device,args.seed+fold*1000+vi*100);p=apply_platt(praw,a,b);variant_target[name]=p;variant_models[name]=model
            print(f'[VARIANT {name}] selected_epochs={best["epoch"]} platt=({a:.4f},{b:.4f}) target macroAP={np.mean([safe_ap(target.y[target.faculty_idx==j],p[target.faculty_idx==j]) for j in range(len(faculties))]):.4f}')
        # Fusion policy uses the latest validation block(s) only; calibration used earlier validation blocks.
        full_name='suite23_cs_hagt';best=variant_select[full_name];a,b=variant_platt[full_name];cal,pol=policy_masks(hist,va);gval=np.full(len(hist.y),np.nan);gval[va]=apply_platt(best['val_score'][va],a,b)
        alphas,global_alpha,fr=select_safe_fusion(hist.y[pol],hist.committee[pol],gval[pol],hist.faculty_idx[pol],faculties,tuple(float(x) for x in args.alpha_grid.split(',')),args.alpha_shrink_k,args.alpha_ap_tolerance,args.alpha_f2_tolerance);fr['fold']=fold;fusion_rows.extend(fr.to_dict('records'));print_table(f'FOLD {fold} FACULTY-SAFE FUSION POLICY',fr)
        graph=variant_target[full_name];global_blend=blend_logits(target.committee,graph,global_alpha);safe=apply_faculty_fusion(target.committee,graph,target.faculty_idx,alphas)
        scores={'committee':target.committee,**variant_target,'suite23_global_blend':global_blend,'suite23_faculty_safe_fusion':safe}
        # external references
        refs,meta=load_references(reference_dirs,fold,target);reference_meta.extend(meta)
        for n,s in refs.items():scores[n]=s
        # Metrics only where score exists for full faculty slice.
        rows=faculty_fold_metric_rows(fold,target,{k:v for k,v in scores.items() if np.isfinite(v).all()})
        # replace rows for partial refs (e.g. 13-fac Council)
        rows=[r for r in rows if not (r['model'] in refs and not np.isfinite(refs[r['model']]).all())]
        for rn,rs in refs.items():
            if np.isfinite(rs).all():continue
            for fi,fac in enumerate(faculties):
                m=target.faculty_idx==fi;z=rs[m]
                if np.isfinite(z).all():rows.append({'fold':fold,'faculty':fac,'model':rn,'rows':int(m.sum()),'failures':int(target.y[m].sum()),'prevalence':float(target.y[m].mean()),**metric_dict(target.y[m],z)})
        all_metrics.extend(rows);print_table(f'SUITE23 TARGET FOLD {fold} — NEW + AVAILABLE REFERENCE RESULTS',pd.DataFrame(rows).sort_values(['faculty','model']))
        # Save scores per faculty with aligned metadata
        for fi,fac in enumerate(faculties):
            m=target.faculty_idx==fi;d={'actual':target.y[m],'committee':target.committee[m],'student_id':target.student_id[m],'course_id':target.course_id[m],'target_semester':target.target_semester[m]}
            for n,s in scores.items():
                z=np.asarray(s)[m]
                if np.isfinite(z).all():d[n]=z
            pd.DataFrame(d).to_csv(fd/f'{fac}_scores.csv',index=False);records.append({'fold':fold,'faculty':fac,'y':target.y[m],'scores':{k:np.asarray(v)[m] for k,v in scores.items() if np.isfinite(np.asarray(v)[m]).all()}})
        # Insight diagnostics from primary graph
        full_model=variant_models[full_name];pert=perturbation_metrics(full_model,gt,target,graph,a,b,device,args,fold);pert_rows.extend(pert);pd.DataFrame(pert).to_csv(fd/'relational_perturbations.csv',index=False)
        abl=ablation_metrics(full_model,gt,target,graph,a,b,device,args,fold);abl_rows.extend(abl);pd.DataFrame(abl).to_csv(fd/'graph_ablations.csv',index=False)
        diag_scores={'committee':target.committee,'suite23_cs_hagt':graph,'suite23_faculty_safe_fusion':safe}
        sr=support_strata_rows(fold,target,gt,diag_scores);support_rows.extend(sr);pd.DataFrame(sr).to_csv(fd/'support_strata.csv',index=False)
        nr=novelty_regime_rows(fold,target,gt,diag_scores);novelty_rows.extend(nr);pd.DataFrame(nr).to_csv(fd/'novelty_regimes.csv',index=False)
        sgr=support_gain_rows(fold,target,gt,target.committee,graph,safe);support_gain.extend(sgr);pd.DataFrame(sgr).to_csv(fd/'support_gain.csv',index=False)
        cr=complementarity_rows(fold,target,target.committee,graph,safe);comp_rows.extend(cr);pd.DataFrame(cr).to_csv(fd/'committee_complementarity.csv',index=False)
        path_inspection(full_model,gt,target,graph,index,device,args,fold,out)
        # component uplift this fold
        md=pd.DataFrame(rows);cu=component_uplift(md,['suite23_bipartite_graph','suite23_heterogeneous_graph','suite23_cs_hagt'],folds=(fold,));pd.DataFrame(cu).to_csv(fd/'component_uplift.csv',index=False)
        # target support/chronology raw table
        sf=pd.DataFrame(graph_support_frame(gt,target));sf['faculty']=[faculties[i] for i in target.faculty_idx];sf.to_csv(fd/'graph_support_and_chronology.csv',index=False)
        # free fold graph stores/models before next fold
        del gh,gt,variant_models;gc.collect();
        if torch.cuda.is_available():torch.cuda.empty_cache()
    # Aggregate ----------------------------------------------------------------
    metrics=pd.DataFrame(all_metrics);metrics.to_csv(out/'all_fold_metrics.csv',index=False);pd.DataFrame(reference_meta).to_csv(out/'reference_loading.csv',index=False);pd.DataFrame(fusion_rows).to_csv(out/'fusion_policies.csv',index=False);pd.DataFrame(pert_rows).to_csv(out/'relational_perturbations_all.csv',index=False);pd.DataFrame(abl_rows).to_csv(out/'graph_ablations_all.csv',index=False);pd.DataFrame(support_rows).to_csv(out/'support_strata_all.csv',index=False);pd.DataFrame(novelty_rows).to_csv(out/'novelty_regimes_all.csv',index=False);pd.DataFrame(support_gain).to_csv(out/'support_gain_all.csv',index=False);pd.DataFrame(comp_rows).to_csv(out/'committee_complementarity_all.csv',index=False)
    print_table('SUITE23 ALL-FOLD MACRO SUMMARY — F1..F7',macro_summary(metrics,folds));print_table('SUITE23 HOLDOUT MACRO SUMMARY — F6/F7',macro_summary(metrics,hold))
    prim='suite23_faculty_safe_fusion';comparators=['committee','suite17','suite18','suite21','suite22','suite22_relational','council']
    ps=[]
    for bname in comparators:
        z=paired_summary(metrics,prim,bname,hold)
        if z.get('cells',0):ps.append(z)
    print_table('SUITE23 PRIMARY PAIRED HOLDOUT COMPARISON',ps);pd.DataFrame(ps).to_csv(out/'paired_holdout_summary.csv',index=False)
    # Faculty mean head-to-head on holdout
    hh=[]
    for fac in faculties:
        d=metrics[(metrics.faculty==fac)&metrics.fold.isin(hold)];a=d[d.model==prim].ap.mean()
        row={'faculty':fac,'suite23_ap':a}
        for bname in comparators:
            z=d[d.model==bname]
            if len(z):row[f'{bname}_ap']=z.ap.mean();row[f'delta_vs_{bname}']=a-z.ap.mean()
        hh.append(row)
    hh=pd.DataFrame(hh);hh.to_csv(out/'holdout_faculty_head_to_head.csv',index=False);print_table('SUITE23 HOLDOUT FACULTY HEAD-TO-HEAD',hh.sort_values('delta_vs_committee',ascending=False))
    # operational evaluation and confusion matrices
    op=operational_metrics(records,dev,hold);op.to_csv(out/'operational_metrics.csv',index=False);piv=op[(op.fold.astype(str)=='POOLED')].groupby(['model','policy'],as_index=False).agg(precision=('precision','mean'),recall=('recall','mean'),f2=('fbeta','mean'),flag_rate=('flag_rate','mean'));print_table('SUITE23 OPERATIONAL HOLDOUT SUMMARY',piv.sort_values(['policy','f2'],ascending=[True,False]))
    op_primary=op[(op.fold.astype(str)=='POOLED')&(op.model.isin([prim,'suite23_cs_hagt','committee','suite18','suite22','council']))][['faculty','model','policy','tp','fp','fn','tn','precision','recall','fbeta','flag_rate','ap','auc']].sort_values(['faculty','policy','model'])
    op_primary.to_csv(out/'operational_faculty_head_to_head.csv',index=False);print_table('SUITE23 PER-FACULTY OPERATIONAL HEAD-TO-HEAD — POOLED F6/F7',op_primary,max_rows=200)
    for model in [prim,'suite23_cs_hagt','committee','suite18','suite22','council','v2']:
        for pol in ['fixed','drift']:print_confusion('POOLED F6/F7 CONFUSION MATRIX',op,'khoa_cong_nghe_thong_tin',model,pol)
    # bootstrap on holdout candidate vs references from records pooled by faculty
    br=[]
    for fac in faculties:
        rr=[r for r in records if r['faculty']==fac and int(r['fold']) in hold];
        if not rr:continue
        y=np.concatenate([r['y'] for r in rr]);cand=np.concatenate([r['scores'][prim] for r in rr])
        for ref in ['committee','suite18','suite21','suite22','council']:
            if not all(ref in r['scores'] for r in rr):continue
            s=np.concatenate([r['scores'][ref] for r in rr]);d,lo,hi=bootstrap_ap_delta(y,cand,s,args.bootstrap,args.seed+len(br)*31);br.append({'faculty':fac,'reference':ref,'delta_ap':d,'ci_low':lo,'ci_high':hi,'positive_ci':lo>0,'negative_ci':hi<0})
    br=pd.DataFrame(br);br.to_csv(out/'paired_bootstrap_holdout.csv',index=False);print_table('SUITE23 PAIRED BOOTSTRAP — HOLDOUT',br)
    # Diagnostic summaries
    if pert_rows:
        p=pd.DataFrame(pert_rows);print_table('SUITE23 RELATIONAL PERTURBATION SUMMARY — HOLDOUT',p[p.fold.isin(hold)].groupby('perturbation',as_index=False).agg(cells=('delta_ap','size'),mean_delta_ap=('delta_ap','mean'),median_delta_ap=('delta_ap','median'),loss_cells=('delta_ap',lambda x:int((x<0).sum())),mean_abs_prob_change=('mean_abs_probability_change','mean')))
    if abl_rows:
        a=pd.DataFrame(abl_rows);print_table('SUITE23 GRAPH ABLATION SUMMARY — HOLDOUT',a[a.fold.isin(hold)].groupby('ablation',as_index=False).agg(cells=('delta_ap','size'),mean_delta_ap=('delta_ap','mean'),median_delta_ap=('delta_ap','median'),loss_cells=('delta_ap',lambda x:int((x<0).sum()))))
    if comp_rows:print_table('SUITE23 COMMITTEE COMPLEMENTARITY — HOLDOUT',pd.DataFrame(comp_rows)[pd.DataFrame(comp_rows).fold.isin(hold)])
    if novelty_rows:print_table('SUITE23 COLD/NEW GRAPH REGIMES — HOLDOUT',pd.DataFrame(novelty_rows)[pd.DataFrame(novelty_rows).fold.isin(hold)])
    if support_gain:
        sgc=support_gain_correlations(support_gain,hold);sgc.to_csv(out/'support_gain_correlations.csv',index=False);print_table('SUITE23 GRAPH-SUPPORT vs GAIN CORRELATIONS — HOLDOUT',sgc)
    audit=premise_audit(metrics,pert_rows,abl_rows,support_gain,primary=prim,holdout=hold);audit.to_csv(out/'premise_audit.csv',index=False);print_table('SUITE23 PREDECLARED PREMISE / SUCCESS AUDIT',audit)
    elapsed=(time.time()-t0)/3600.;save_json({'elapsed_hours':elapsed,'folds':folds,'faculties':faculties,'primary':'suite23_faculty_safe_fusion'},out/'run_summary.json');print(f'\n[DONE] Suite23 complete in {elapsed:.2f} hours | output={out}')


def parser():
    root='/root/disk_3tb/earlywarning/30072026';p=argparse.ArgumentParser()
    p.add_argument('--cache_dir',default=f'{root}/results/cross_faculty_residual_moe/cache');p.add_argument('--structured_cache_dir',default=f'{root}/results/suite12_structured_cache');p.add_argument('--database_dir',default='/root/disk_3tb/earlywarning/database');p.add_argument('--out_dir',default=f'{root}/results/suite23_cs_hagt');p.add_argument('--faculties',default='all');p.add_argument('--folds',default='1,2,3,4,5,6,7');p.add_argument('--dev_folds',default='1,2,3,4,5');p.add_argument('--holdout_folds',default='6,7')
    p.add_argument('--seed',type=int,default=20260924);p.add_argument('--device',default='auto');p.add_argument('--d_model',type=int,default=64);p.add_argument('--heads',type=int,default=4);p.add_argument('--layers',type=int,default=2);p.add_argument('--dropout',type=float,default=.10);p.add_argument('--lr',type=float,default=1e-3);p.add_argument('--weight_decay',type=float,default=1e-4);p.add_argument('--rank_weight',type=float,default=.15);p.add_argument('--batch_size',type=int,default=768);p.add_argument('--eval_batch_size',type=int,default=1024);p.add_argument('--max_epochs',type=int,default=12);p.add_argument('--min_epochs',type=int,default=3);p.add_argument('--patience',type=int,default=3);p.add_argument('--samples_per_epoch',type=int,default=240000);p.add_argument('--norm_rows',type=int,default=50000);p.add_argument('--val_blocks',type=int,default=3)
    p.add_argument('--alpha_grid',default='0,.10,.25,.50,.75');p.add_argument('--alpha_shrink_k',type=float,default=50.);p.add_argument('--alpha_ap_tolerance',type=float,default=.002);p.add_argument('--alpha_f2_tolerance',type=float,default=.005);p.add_argument('--bootstrap',type=int,default=500);p.add_argument('--inspect_rows',type=int,default=12);p.add_argument('--graph_progress_every',type=int,default=25000)
    p.add_argument('--suite14_dir',default=f'{root}/results/suite14_authority_safe_collaborative_moe');p.add_argument('--suite16_dir',default=f'{root}/results/suite16_conditional_tree_boosting');p.add_argument('--suite17_dir',default=f'{root}/results/suite17_robust_forward_specialist_boosting');p.add_argument('--suite18_dir',default=f'{root}/results/suite18_fmm_forward_specialist_boosting');p.add_argument('--suite20_dir',default=f'{root}/results/suite20_semester_target_transformer');p.add_argument('--suite21_dir',default=f'{root}/results/suite21_guarded_temporal_residual');p.add_argument('--suite22_dir',default=f'{root}/results/suite22_sftr_tabm');p.add_argument('--council_dir',default=f'{root}/results/suite4/2_competence_council');p.add_argument('--v2_dir',default=f'{root}/results/final_train_v2');return p

if __name__=='__main__':run(parser().parse_args())
