# -*- coding: utf-8 -*-
from __future__ import annotations
from pathlib import Path
import numpy as np
import pandas as pd
from suite26_common import *

REFERENCE_ALIASES={
 'suite14':['suite14_authority_safe_final','suite14','final'],
 'suite16':['suite16_hgb_final','suite16','final'],
 'suite17':['suite17_hgb_final','suite17_final','final'],
 'suite18':['suite18_hgb_final','suite18_final','final'],
 'suite20':['suite20_transformer','suite20_semester_target_transformer','transformer','final'],
 'suite21':['suite21_guarded_temporal_residual','suite21_guarded','final'],
 'suite22':['suite22_safe_blend','safe_blend','final'],
 'suite22_primary':['suite22_sftr_tabm','sftr_tabm','primary'],
 'suite22_relational':['suite22_relational_ft_tabm','relational_ft_tabm','relational'],
 'suite23':['suite23_faculty_safe_fusion','faculty_safe_fusion','final'],
 'suite23_graph':['suite23_cs_hagt','cs_hagt'],
 'suite24':['suite24_evidence_residual','evidence_residual','primary'],
 'suite24_posthoc':['suite24_posthoc_blend','posthoc_blend'],
 'suite25':['suite25_sacr','sacr','primary'],
 'suite25_blend':['suite25_committee_blend','committee_blend'],
 'council':['suite4_competence_council','competence_council','council','final'],
 'v2':['failure_probability_lt5','failure_probability','stacked_probability','final'],
}

def _candidates(root,fold,faculty):
    r=Path(root);return [r/f'fold_{fold}'/f'{faculty}_scores.csv',r/f'{faculty}_fold_{fold}_scores.csv',r/f'fold_{fold}'/f'{faculty}_target_scores.csv',r/f'{faculty}_fold_{fold}_target_scores.csv',r/f'{faculty}_fold_{fold}_test_results.csv']

def load_reference_score(root,fold,faculty,y,aliases):
    if not root:return None,None,None
    p=next((x for x in _candidates(root,fold,faculty) if x.exists()),None)
    if p is None:return None,None,None
    df=pd.read_csv(p); actual=next((c for c in ['actual','actual_failure_lt5','y','label','target'] if c in df.columns),None)
    if actual:
        yy=pd.to_numeric(df[actual],errors='coerce').to_numpy()
        if len(yy)!=len(y) or not np.array_equal(yy.astype(int),np.asarray(y,int)):raise ValueError(f'reference alignment mismatch {p}')
    col=next((c for c in aliases if c in df.columns),None)
    if col is None:return None,p,None
    s=pd.to_numeric(df[col],errors='coerce').to_numpy(float)
    if len(s)!=len(y) or not np.isfinite(s).all():raise ValueError(f'invalid reference {p}:{col}')
    return s,p,col

def load_references(reference_dirs,fold,bundle):
    scores={};meta=[]
    for name,root in reference_dirs.items():
        full=np.full(len(bundle.y),np.nan,float);loaded=0
        for fi,fac in enumerate(bundle.faculty_names):
            m=bundle.faculty_idx==fi
            try:s,p,col=load_reference_score(root,fold,fac,bundle.y[m],REFERENCE_ALIASES.get(name,[name,'final']))
            except Exception as e:meta.append({'reference':name,'fold':fold,'faculty':fac,'status':'error','detail':str(e)});continue
            if s is None:meta.append({'reference':name,'fold':fold,'faculty':fac,'status':'missing','path':str(p) if p else ''});continue
            full[m]=s;loaded+=1;meta.append({'reference':name,'fold':fold,'faculty':fac,'status':'loaded','path':str(p),'column':col})
        if loaded:scores[name]=full
    return scores,meta

def component_uplift(metrics,folds=(6,7)):
    d=pd.DataFrame(metrics);d=d[d.fold.isin(list(folds))];out=[]
    for a,b,label in [('suite26_tree','suite26_tree_memory','student_memory_gain'),('suite26_tree_memory','suite26_tair','latent_ability_requirement_gain')]:
        aa=d[d.model==a][['fold','faculty','ap','fbeta_at_20']].rename(columns={'ap':'a','fbeta_at_20':'af2'});bb=d[d.model==b][['fold','faculty','ap','fbeta_at_20']].rename(columns={'ap':'b','fbeta_at_20':'bf2'});z=aa.merge(bb,on=['fold','faculty'])
        if len(z):z['component']=label;z['delta_ap']=z.b-z.a;z['delta_f2_20']=z.bf2-z.af2;out.append(z[['fold','faculty','component','delta_ap','delta_f2_20']])
    return pd.concat(out,ignore_index=True) if out else pd.DataFrame()

def complementarity_rows(fold,bundle,committee,primary,blend,rate=.20):
    rows=[]
    for fi,fac in enumerate(bundle.faculty_names):
        m=bundle.faculty_idx==fi;y=bundle.y[m];q=committee[m];g=primary[m];f=blend[m];pq=warn_by_rate(q,rate).astype(bool);pg=warn_by_rate(g,rate).astype(bool);pf=warn_by_rate(f,rate).astype(bool);fn=(y==1)&(~pq);fa=(y==0)&pq
        rows.append({'fold':fold,'faculty':fac,'rows':int(m.sum()),'committee_false_negatives_at20':int(fn.sum()),'memory_rescued_fn_at20':int(np.sum(fn&pg)),'blend_rescued_fn_at20':int(np.sum(fn&pf)),'committee_false_alarms_at20':int(fa.sum()),'memory_corrected_fa_at20':int(np.sum(fa&(~pg))),'blend_corrected_fa_at20':int(np.sum(fa&(~pf))),'committee_only_correct':int(np.sum((pq==(y==1))&(pg!=(y==1)))),'memory_only_correct':int(np.sum((pq!=(y==1))&(pg==(y==1))))})
    return rows

def cluster_bootstrap_ap_delta(y,a,b,cluster,n_boot=500,seed=42):
    y=np.asarray(y,int);a=np.asarray(a,float);b=np.asarray(b,float);cluster=np.asarray(cluster);uniq=np.unique(cluster)
    if np.unique(y).size<2 or len(uniq)<2:return np.nan,np.nan,np.nan
    loc={u:np.where(cluster==u)[0] for u in uniq};rng=np.random.default_rng(seed);vals=[]
    for _ in range(int(n_boot)):
        sampled=rng.choice(uniq,size=len(uniq),replace=True);idx=np.concatenate([loc[u] for u in sampled]);yy=y[idx]
        if np.unique(yy).size<2:continue
        vals.append(safe_ap(yy,a[idx])-safe_ap(yy,b[idx]))
    if not vals:return np.nan,np.nan,np.nan
    return float(safe_ap(y,a)-safe_ap(y,b)),float(np.quantile(vals,.025)),float(np.quantile(vals,.975))

def history_strata_rows(fold,bundle,store,scores):
    val=np.asarray(store.support); rows=[];bins=[(-.5,.5,'0'),(.5,2.5,'1-2'),(2.5,5.5,'3-5'),(5.5,10.5,'6-10'),(10.5,20.5,'11-20'),(20.5,999,'21+')]
    for fi,fac in enumerate(bundle.faculty_names):
        fm=bundle.faculty_idx==fi
        for lo,hi,label in bins:
            m=fm&(val>lo)&(val<=hi)
            if m.sum()<20:continue
            for model,s in scores.items():rows.append({'fold':fold,'faculty':fac,'history_bin':label,'model':model,'rows':int(m.sum()),'failures':int(bundle.y[m].sum()),'ap':safe_ap(bundle.y[m],np.asarray(s)[m]),'auc':safe_auc(bundle.y[m],np.asarray(s)[m])})
    return rows

def novelty_rows(fold,bundle,store,scores):
    new_s=np.asarray(store.support)==0;new_c=np.asarray(store.course_seen_count)==0;labels=np.where(new_s&new_c,'new_student+new_course',np.where(new_s,'new_student+seen_course',np.where(new_c,'returning_student+new_course','returning_student+seen_course')));rows=[]
    for fi,fac in enumerate(bundle.faculty_names):
        fm=bundle.faculty_idx==fi
        for regime in np.unique(labels[fm]):
            m=fm&(labels==regime)
            for model,s in scores.items():rows.append({'fold':fold,'faculty':fac,'regime':regime,'model':model,'rows':int(m.sum()),'failures':int(bundle.y[m].sum()),'prevalence':float(bundle.y[m].mean()),'ap':safe_ap(bundle.y[m],np.asarray(s)[m]),'auc':safe_auc(bundle.y[m],np.asarray(s)[m])})
    return rows

def _pearson(x,y):
    x=np.asarray(x,float);y=np.asarray(y,float);m=np.isfinite(x)&np.isfinite(y)
    return float(np.corrcoef(x[m],y[m])[0,1]) if m.sum()>=3 and np.std(x[m])>1e-12 and np.std(y[m])>1e-12 else np.nan

def support_gain_rows(fold,bundle,store,tree,primary):
    rows=[]
    for fi,fac in enumerate(bundle.faculty_names):
        m=bundle.faculty_idx==fi
        rows.append({'fold':fold,'faculty':fac,'rows':int(m.sum()),'failures':int(bundle.y[m].sum()),'history_mean':float(store.support[m].mean()),'history_failure_mean':float(store.failure_support[m].mean()),'same_course_mass_proxy':np.nan,'course_seen_mean':float(store.course_seen_count[m].mean()),'primary_delta_ap_vs_tree':safe_ap(bundle.y[m],primary[m])-safe_ap(bundle.y[m],tree[m])})
    return rows

def support_gain_correlations(rows,folds=(6,7)):
    d=pd.DataFrame(rows);d=d[d.fold.isin(list(folds))];out=[]
    for s in ['history_mean','history_failure_mean','course_seen_mean']:out.append({'support':s,'cells':len(d),'pearson_r':_pearson(d[s],d.primary_delta_ap_vs_tree)})
    return pd.DataFrame(out)

def premise_audit(metrics,ablation_rows,shuffle_rows,holdout=(6,7),it_faculty='khoa_cong_nghe_thong_tin'):
    d=pd.DataFrame(metrics);h=d[d.fold.isin(list(holdout))];rows=[]
    def add(label,a,b,strong=.003):
        z=paired_summary(h,a,b);v=z.get('mean_delta_ap',np.nan);w=z.get('wins',0);c=z.get('cells',0);ver='SUPPORTED' if np.isfinite(v) and v>=strong and c and w/c>=.60 else ('WEAK_SUPPORT' if np.isfinite(v) and v>0 else 'NOT_SUPPORTED');rows.append({'premise':label,'mean_delta_ap':v,'wins':w,'cells':c,'verdict':ver})
    add('student_memory_B_minus_tree','suite26_tree_memory','suite26_tree',.005)
    add('latent_ability_C_minus_memory','suite26_tair','suite26_tree_memory',.003)
    add('primary_beats_committee','suite26_tair','committee',.010)
    add('primary_beats_suite22','suite26_tair','suite22',.003)
    add('primary_beats_suite25','suite26_tair','suite25',.003)
    add('joint_head_beats_tree','suite26_joint_head','suite26_tree',.003)
    a=pd.DataFrame(ablation_rows)
    if len(a):
        a=a[a.fold.isin(list(holdout))]
        for mode in ['no_memory','no_labels','no_grades','no_recency','no_tree_leaf','no_ability','uniform_attention','tree_only']:
            z=a[a.ablation==mode];v=float(z.delta_ap.mean()) if len(z) else np.nan;loss=int((z.delta_ap<0).sum()) if len(z) else 0;c=len(z);ver='SUPPORTED' if np.isfinite(v) and v<=-.003 and c and loss/c>=.60 else ('WEAK_SUPPORT' if np.isfinite(v) and v<0 else 'NOT_SUPPORTED');rows.append({'premise':f'ablation_{mode}_should_hurt','mean_delta_ap':v,'wins':loss,'cells':c,'verdict':ver})
    s=pd.DataFrame(shuffle_rows)
    if len(s):
        s=s[s.fold.isin(list(holdout))];v=float(s.delta_ap.mean());loss=int((s.delta_ap<0).sum());c=len(s);ver='SUPPORTED' if v<=-.003 and loss/max(c,1)>=.60 else ('WEAK_SUPPORT' if v<0 else 'NOT_SUPPORTED');rows.append({'premise':'shuffled_student_history_should_hurt','mean_delta_ap':v,'wins':loss,'cells':c,'verdict':ver})
    if 'council' in set(h.model):
        aa=h[h.model=='suite26_tair'][['fold','faculty','ap','fbeta_at_20']].rename(columns={'ap':'a','fbeta_at_20':'af2'});bb=h[h.model=='council'][['fold','faculty','ap','fbeta_at_20']].rename(columns={'ap':'b','fbeta_at_20':'bf2'});z=aa.merge(bb,on=['fold','faculty'])
        if len(z):
            fac=z.groupby('faculty').agg(a=('a','mean'),b=('b','mean'));fac['delta']=fac.a-fac.b;macro=float(fac.delta.mean());loss=int((fac.delta<-.003).sum());it=float(fac.loc[it_faculty,'delta']) if it_faculty in fac.index else np.nan;fd=z.assign(delta=z.a-z.b).groupby('fold').delta.mean().to_dict();f2=float((z.af2-z.bf2).mean());success=bool(macro>=.010 and loss==0 and np.isfinite(it) and it>=0 and all(fd.get(f,np.nan)>0 for f in holdout) and f2>=-.005);rows.append({'premise':'PROJECT_COUNCIL_BREAKTHROUGH','mean_delta_ap':macro,'wins':int((fac.delta>.003).sum()),'cells':len(fac),'verdict':'SUCCESS' if success else 'NOT_SUCCESSFUL','meaningful_faculty_losses':loss,'it_delta_ap':it,'mean_delta_f2':f2,**{f'fold_{k}_delta_ap':v for k,v in fd.items()}})
    return pd.DataFrame(rows)

def print_confusion(title,op,faculty,model,policy='fixed'):
    z=op[(op.faculty==faculty)&(op.model==model)&(op.fold.astype(str)=='POOLED')&(op.policy==policy)]
    if not len(z):return
    r=z.iloc[0];print(f"\n{title} | {faculty} | {model} | {policy}\nActual\\Pred      Normal      Failure\nNormal        {int(r.tn):10d} {int(r.fp):10d}\nFailure       {int(r.fn):10d} {int(r.tp):10d}\nP={r.precision:.4f} R={r.recall:.4f} F2={r.fbeta:.4f} flag={r.flag_rate:.4f}")
