"""Suite31 stage-aware, fully logged experimental CLI."""
from __future__ import annotations
import argparse, hashlib, json, os, platform, shutil, sys, time, traceback
from pathlib import Path
import importlib.metadata as md
import numpy as np
import pandas as pd
from .core import (load_aq,audit_target,global_historical_split,support_select,scoring,cell_rows,macro_table,paired_comparison,gpu_peak,reset_gpu_peak,note,dump_json,stable_hash)
from .models import Estimator,prior

DEFAULT_ROOT='/root/disk_3tb/earlywarning/30072026'
ALL_MODELS=['committee','hgb_matched','hgb_full','tabicl_frozen','tabpfn_35_frozen','tabicl_ordinary','tabicl_chronological']

def csv_list(x,convert=str):return [convert(v.strip()) for v in str(x).split(',') if v.strip()]
def parse():
    p=argparse.ArgumentParser(description='Suite31 CAPT — external tabular pretraining and chronological adaptation')
    p.add_argument('--stage',choices=['preflight','synthetic','resource','pilot','development','ordinary_finetune','chronological_adapt','retrospective','report','replay'],default='preflight')
    p.add_argument('--root',default=DEFAULT_ROOT);p.add_argument('--cache-dir');p.add_argument('--structured-dir');p.add_argument('--output-dir');p.add_argument('--faculties',default='all');p.add_argument('--folds',default='1');p.add_argument('--seeds',default='29');p.add_argument('--support-budgets',default='4096,16384');p.add_argument('--models',default='auto');p.add_argument('--device',default='cuda');p.add_argument('--query-batch-size',type=int,default=256);p.add_argument('--n-estimators',type=int,default=8);p.add_argument('--n-estimators-train',type=int,default=2)
    p.add_argument('--tabicl-checkpoint',default=None);p.add_argument('--tabpfn-checkpoint',default=None)
    p.add_argument('--epochs',type=int,default=4);p.add_argument('--patience',type=int,default=2);p.add_argument('--learning-rate',type=float,default=1e-5);p.add_argument('--weight-decay',type=float,default=.01);p.add_argument('--grad-clip',type=float,default=1.);p.add_argument('--episode-query-size',type=int,default=128);p.add_argument('--episode-support-size',type=int,default=1024);p.add_argument('--max-episodes',type=int,default=64);p.add_argument('--validation-blocks',type=int,default=3);p.add_argument('--release-gap',type=int,default=0,help='Extra whole-semester embargo (proxy, NOT verified grade-release timestamps)')
    p.add_argument('--strict-audit',action='store_true');p.add_argument('--continue-on-error',action='store_true');p.add_argument('--max-query-rows',type=int,default=None,help='Diagnostic-only query truncation (NOT full experiment)');p.add_argument('--protocol-freeze-file');p.add_argument('--checkpoint');p.add_argument('--scores');p.add_argument('--resume',action='store_true');p.add_argument('--dry-run',action='store_true');p.add_argument('--test-synthetic',action='store_true',help='Use generated synthetic caches; never label as real results')
    return p.parse_args()

def env_report(a,out):
    version={}
    for m in ['numpy','scikit-learn','torch','pandas','joblib','tabicl','tabpfn']:
        try:version[m]=md.version(m)
        except Exception:version[m]='NOT_INSTALLED'
    try:
        import torch
        gpu={'cuda_available':torch.cuda.is_available(),'gpu':torch.cuda.get_device_name(0) if torch.cuda.is_available() else None,'gpu_memory_gb':round(torch.cuda.get_device_properties(0).total_memory/1024**3,3) if torch.cuda.is_available() else None}
    except Exception:gpu={}
    env=dict(python=sys.version,platform=platform.platform(),libraries=version,hardware=gpu,root=str(a.root));dump_json(out/'environment.json',env);note('ENV',python=platform.python_version(),versions=version,gpu=gpu);return env

def models_for(a):
    if a.models!='auto':
        result=csv_list(a.models)
        bad=set(result)-set(ALL_MODELS)
        if bad:raise ValueError(f'Unknown models: {bad}')
        return result
    if a.stage in ('ordinary_finetune',):return ['committee','hgb_full','tabicl_frozen','tabicl_ordinary']
    if a.stage in ('chronological_adapt',):return ['committee','hgb_full','tabicl_frozen','tabicl_chronological']
    if a.stage in ('development','retrospective'):return ALL_MODELS
    return ['committee','hgb_matched','hgb_full','tabicl_frozen','tabpfn_35_frozen']

def save_report(out):
    paths=sorted((out/'fold_scores').glob('*.csv'))
    if not paths:note('REPORT',status='NO_SCORE_FILES',path=out);return
    metrics=pd.concat([pd.read_csv(p) for p in paths],ignore_index=True)
    metrics.to_csv(out/'all_metrics.csv',index=False)
    macro=macro_table(metrics);macro.to_csv(out/'all_macro_by_model.csv',index=False)
    byfold=metrics.groupby(['fold','model','support_budget','stage'],dropna=False)['ap'].mean().reset_index(name='macro_ap');byfold.to_csv(out/'all_macro_by_fold.csv',index=False)
    paired=paired_comparison(metrics);paired.to_csv(out/'all_paired_deltas.csv',index=False)
    faculty=metrics.groupby(['faculty','model','support_budget'],dropna=False).agg(macro_ap=('ap','mean'),f2_at_20=('fbeta_at_20','mean'),brier=('brier','mean'),cells=('ap','count'),registrations=('rows','sum'),failures=('failures','sum')).reset_index()
    faculty.to_csv(out/'all_faculty_stability.csv',index=False)
    support=metrics.groupby(['model','support_budget','stage'],dropna=False).agg(macro_ap=('ap','mean'),brier=('brier','mean'),cells=('ap','count')).reset_index()
    support.to_csv(out/'all_support_budget_effects.csv',index=False)
    seed_variability=metrics.groupby(['model','support_budget','faculty','fold'],dropna=False).agg(mean_ap=('ap','mean'),sd_ap=('ap','std'),seeds=('support_seed','nunique')).reset_index()
    seed_variability.to_csv(out/'all_support_seed_variability.csv',index=False)
    metrics[['fold','faculty','origin','model','support_budget','support_seed','prevalence','mean_score','brier','ece10','logloss']].to_csv(out/'all_calibration.csv',index=False)
    curves=list((out/'checkpoints').glob('*/selection_curve.csv'))
    if curves:
        all_curves=[]
        for fp in curves:
            z=pd.read_csv(fp);z['checkpoint_group']=fp.parent.name;all_curves.append(z)
        pd.concat(all_curves,ignore_index=True).to_csv(out/'all_training_curves.csv',index=False)
    print('\n'+'='*105+'\nSUITE31 ALL-CELL MACRO RESULTS\n'+'='*105,flush=True);print(macro.sort_values('macro_ap',ascending=False).to_string(index=False),flush=True)
    print('\nSUITE31 MACRO AP BY FOLD\n'+byfold.to_string(index=False),flush=True)
    print('\nSUITE31 PAIRED MODEL DIFFERENCES\n'+paired.to_string(index=False),flush=True)
    print('\nSUITE31 FACULTY AP STABILITY\n'+faculty.sort_values(['faculty','model','support_budget']).to_string(index=False),flush=True)
    print('\nSUITE31 SUPPORT-BUDGET RESPONSE\n'+support.to_string(index=False),flush=True)
    if not paired.empty:
        print('\nSUITE31 DESCRIPTIVE TRANSFER/ADAPTATION INSIGHTS (NOT CAUSAL PROOF)')
        for row in paired.itertuples():
            if (row.model,row.reference) in [('tabicl_frozen','hgb_matched'),('tabpfn_35_frozen','hgb_matched'),('tabicl_chronological','tabicl_ordinary'),('tabicl_ordinary','tabicl_frozen'),('tabicl_chronological','tabicl_frozen')]:
                note('INSIGHT',comparison=f'{row.model} - {row.reference}',mean_delta_ap=row.mean_delta_ap,wins=row.wins,losses=row.losses,paired_cells=row.paired_cells,interpretation='descriptive retrospective difference')
    if metrics.fold.nunique()>1:
        byorigin=metrics.groupby(['fold','model'],dropna=False)['ap'].mean().reset_index()
        print('\nSUITE31 TEMPORAL MACRO AP BY FOLD\n'+byorigin.to_string(index=False),flush=True)
    expected=pd.read_csv(out/'coverage_manifest.csv') if (out/'coverage_manifest.csv').exists() else pd.DataFrame()
    if not expected.empty:note('COVERAGE',attempted=len(expected),success=int((expected.status=='SUCCESS').sum()),failure=int((expected.status!='SUCCESS').sum()))
    note('REPORT',metrics_rows=len(metrics),configurations=len(macro),output=out)

def append_csv(path,row):
    path=Path(path);path.parent.mkdir(parents=True,exist_ok=True)
    pd.DataFrame([row]).to_csv(path,mode='a',index=False,header=not path.exists())

def sample_scores(a,out,h,q,fold,model,seed,budget,feature_hash,ckpt=None):
    scores=np.full(len(q),np.nan,float);all_manifests=[];prof=[]
    for fi,name in enumerate(q.faculty_names):
        for origin in np.sort(np.unique(q.semester_ordinal[q.faculty_idx==fi])):
            mask=(q.faculty_idx==fi)&(q.semester_ordinal==origin)
            if not mask.any():continue
            sp,man=support_select(h,fi,origin,budget,seed,a.release_gap)
            man.update({'fold':fold,'faculty':name,'model':model,'feature_hash':feature_hash,'availability_assumption':'semester_proxy_not_grade_release_verified'})
            # Reuse exact sampled supports across HGB and pretrained candidates.
            all_manifests.append(man)
            start=time.monotonic();reset_gpu_peak()
            if model=='committee':pr=np.asarray(q.committee[mask],float)
            else:
                selector=model
                source_ckpt=(str(ckpt) if model in ('tabicl_ordinary','tabicl_chronological') else a.tabicl_checkpoint) if model!='tabpfn_35_frozen' else a.tabpfn_checkpoint
                if model=='hgb_full':sp=np.flatnonzero((h.faculty_idx==fi)&(h.semester_ordinal<float(origin)-a.release_gap))
                est=Estimator(selector,device=a.device,seed=seed,n_estimators=a.n_estimators,batch_size=a.query_batch_size,model_path=source_ckpt)
                est.fit(h.current[sp],h.y[sp]);pr=est.predict(q.current[mask])
                del est
            elapsed=time.monotonic()-start;peak=gpu_peak()
            if not np.isfinite(pr).all():raise ValueError('Non-finite predictions')
            scores[mask]=pr
            mm=scoring(q.y[mask],pr)
            note('FACULTY RESULTS',fold=fold,faculty=name,origin=origin,model=model,seed=seed,budget=budget,rows=int(mask.sum()),positives=mm['failures'],support=len(sp),support_positives=int(h.y[sp].sum()),AP=mm['ap'],AUC=mm['auc'],F2_20=mm['fbeta_at_20'],Brier=mm['brier'],ECE=mm['ece10'],seconds=elapsed,peak_gb=peak)
            prof.append(dict(fold=fold,faculty=name,origin=float(origin),model=model,seed=seed,budget=budget,support_rows=len(sp),support_positives=int(h.y[sp].sum()),elapsed_seconds=elapsed,peak_vram_gb=peak,rows=int(mask.sum())))
    assert np.isfinite(scores).all(),'Unscored rows remain'
    return scores,all_manifests,prof

def record_predictions(out,q,p,fold,model,seed,budget,stage):
    # IDs are intentionally kept in secured per-run local CSV, not console logs.
    df=pd.DataFrame({'fold':fold,'faculty':[q.faculty_names[int(i)] for i in q.faculty_idx],'student_id':q.student_id,'course_id':q.course_id,'semester':q.target_semester,'row_index':q.row_index,'actual':q.y,'model':model,'support_seed':seed,'support_budget':budget,'stage':stage,'predicted_probability':p})
    dest=out/'predictions'/f'F{fold}_{model}_B{budget}_S{seed}.csv';dest.parent.mkdir(parents=True,exist_ok=True);df.to_csv(dest,index=False)
    return dest

def check_stage_frozen(a,out):
    if a.stage=='retrospective':
        if not a.protocol_freeze_file:raise ValueError('Retrospective F6/F7 requires --protocol-freeze-file from frozen development')
        lock=json.loads(Path(a.protocol_freeze_file).read_text());note('GATE',stage='retrospective',lock_id=lock.get('config_hash'),status='FROZEN_PROTOCOL_READ')

def run(a):
    root=Path(a.root);out=Path(a.output_dir or (root/'results'/'suite31_CAPT'));out.mkdir(parents=True,exist_ok=True)
    note('START',stage=a.stage,root=root,output=out)
    dump_json(out/'config.json',vars(a));env_report(a,out)
    if a.stage=='report':save_report(out);return
    if a.stage=='replay':
        if not a.scores:raise ValueError('--scores required for checkpoint prediction replay')
        df=pd.read_csv(a.scores)
        if df.empty:raise ValueError('Empty replay score file')
        if len(df[['fold','model','support_seed','support_budget']].drop_duplicates())!=1:raise ValueError('Replay file must have one configuration')
        cfg=df.iloc[0];fold=int(cfg['fold']);model=str(cfg['model']);seed=int(cfg['support_seed']);budget=int(cfg['support_budget'])
        if model in ('tabicl_ordinary','tabicl_chronological') and not a.checkpoint:raise ValueError('Adapted replay requires --checkpoint best.ckpt')
        from suite25_data import discover_faculties,parse_faculties,discover_common_feature_names
        cache=Path(a.cache_dir or root/'results'/'cross_faculty_residual_moe'/'cache');structured=Path(a.structured_dir or root/'results'/'suite12_structured_cache')
        faculties=parse_faculties(a.faculties,discover_faculties(cache,fold));names=discover_common_feature_names(cache,faculties,fold)
        h,q,_,_=load_aq(cache,structured,faculties,fold,names);audit_target(h,q)
        values,_,_=sample_scores(a,out,h,q,fold,model,seed,budget,stable_hash(names),a.checkpoint)
        test=pd.DataFrame({'faculty':[q.faculty_names[int(i)] for i in q.faculty_idx],'student_id':q.student_id.astype(str),'course_id':q.course_id.astype(str),'semester':q.target_semester,'row_index':q.row_index,'replayed':values})
        joined=df.merge(test,on=['faculty','student_id','course_id','semester','row_index'],validate='one_to_one')
        if len(joined)!=len(df):raise ValueError('Replay canonical row identity mismatch')
        error=float(np.max(np.abs(joined.predicted_probability-joined.replayed)))
        note('REPLAY',scores=a.scores,checkpoint=a.checkpoint or 'model_default',rows=len(joined),max_abs_probability_error=error)
        if error>1e-4:raise AssertionError(f'Predictions do not replay within 1e-4: {error}')
        return
    if a.stage=='synthetic' or a.test_synthetic:
        from make_synthetic_caches import create
        cc,ss=create(out/'synthetic_cache');a.cache_dir=str(cc);a.structured_dir=str(ss);a.faculties='all';a.folds='1';note('SYNTHETIC',warning='Generated data only, not AQ performance')
    cdir=Path(a.cache_dir or root/'results'/'cross_faculty_residual_moe'/'cache');sdir=Path(a.structured_dir or root/'results'/'suite12_structured_cache')
    from suite25_data import discover_faculties,parse_faculties,discover_common_feature_names
    folds=csv_list(a.folds,int);seeds=csv_list(a.seeds,int);budgets=csv_list(a.support_budgets,int)
    if a.stage=='retrospective' and not set(folds).issubset({6,7}):raise ValueError('Retrospective mode only supports F6/F7')
    if a.stage=='development' and not set(folds).issubset({1,2,3,4,5}):raise ValueError('Development must not select on F6/F7')
    if any(x<=0 for x in budgets):raise ValueError('Support budgets must be positive')
    if a.release_gap<0:raise ValueError('Negative release_gap creates future leakage')
    faculties=parse_faculties(a.faculties,discover_faculties(cdir,folds[0]));names=discover_common_feature_names(cdir,faculties,folds[0]);feature_hash=stable_hash(names)
    check_stage_frozen(a,out)
    if a.stage=='retrospective':
        lock=json.loads(Path(a.protocol_freeze_file).read_text())
        compared={'faculties':faculties,'models':models_for(a),'budgets':budgets,'release_gap':a.release_gap,'seeds':seeds,'checkpoint_source':a.tabicl_checkpoint,'epochs':a.epochs,'patience':a.patience,'learning_rate':a.learning_rate,'weight_decay':a.weight_decay,'grad_clip':a.grad_clip,'episode_query_size':a.episode_query_size,'episode_support_size':a.episode_support_size,'max_episodes':a.max_episodes,'validation_blocks':a.validation_blocks,'n_estimators':a.n_estimators,'n_estimators_train':a.n_estimators_train}
        for k,v in compared.items():
            if lock.get(k)!=v:raise ValueError(f'Protocol mismatch at {k}: current={v}, development={lock.get(k)}')
        if lock.get('feature_hash')!=feature_hash:raise ValueError('Feature contract differs from development lock')
    if a.dry_run:
        note('DRY RUN',folds=folds,faculties=faculties,models=models_for(a),seeds=seeds,budgets=budgets,planned_cells=len(folds)*len(models_for(a))*len(seeds)*len(budgets))
        return
    if a.strict_audit:note('WARNING',audit='Cache contains semester chronology; exact grade release-time audit not verified. Do NOT call this verified-as-of evaluation.')
    note('FEATURE',raw_features=len(names),structured_features=17,total_features=len(names)+17,feature_hash=feature_hash,faculties=len(faculties),folds=folds,models=models_for(a))
    dump_json(out/'feature_contract.json',{'raw_names':names,'course_names':'see legacy/suite25_data.py COURSE_FEATURES','total_columns':len(names)+17,'hash':feature_hash,'availability':'previous benchmark cache; raw as-of availability NOT audited'})
    if a.stage=='preflight':
        for fold in folds:
            h,q,fn,_=load_aq(cdir,sdir,faculties,fold,names);audit_target(h,q);global_historical_split(h,a.validation_blocks)
        note('RUN STATUS',status='SUCCESS',stage='preflight');return
    if a.stage=='resource':
        note('GPU',stage='resource',message='resource stage performs actual inference over one fold using requested models/budgets');a.models='tabicl_frozen,tabpfn_35_frozen'
    if a.stage in ('development','retrospective') and a.models=='auto':note('GATE',message='All seven models requested; heavyweight adaptation can be expensive. Use --models to stage.')
    requested=models_for(a);coverage=[];required_error=0
    for fold in folds:
        h,q,feature_names,_=load_aq(cdir,sdir,faculties,fold,names);audit_target(h,q)
        if a.max_query_rows:
            q=q.subset(np.arange(len(q))<a.max_query_rows);note('WARNING',type='TRUNCATED_TARGET_DIAGNOSTIC',rows=len(q))
        train,valid,cutoff=global_historical_split(h,a.validation_blocks)
        for seed in seeds:
            ckpt={}
            for model in requested:
                if model not in ('tabicl_ordinary','tabicl_chronological'):continue
                mode='ordinary' if model=='tabicl_ordinary' else 'chronological'
                from .adapt import train_adapted
                destination=out/'checkpoints'/f'F{fold}_S{seed}_{model}'
                try:
                    ckpt[model]=train_adapted(h,train,valid,mode,destination,seed,a.device,epochs=a.epochs,patience=a.patience,lr=a.learning_rate,weight_decay=a.weight_decay,grad_clip=a.grad_clip,episode_support=a.episode_support_size,episode_query=a.episode_query_size,max_episodes=a.max_episodes,validation_support=budgets[0],n_estimators_train=a.n_estimators_train,n_estimators_val=min(2,a.n_estimators),n_estimators_infer=a.n_estimators,model_path=a.tabicl_checkpoint,release_gap=a.release_gap)
                except Exception as exc:
                    note('FAILURE',stage='adaptation',fold=fold,seed=seed,model=model,error=repr(exc));traceback.print_exc();required_error+=1
                    if not a.continue_on_error:raise
            for budget in budgets:
                for model in requested:
                    ident=f'F{fold}_{model}_B{budget}_S{seed}';score_path=out/'fold_scores'/f'{ident}.csv'
                    if a.resume and score_path.exists():note('RESUME',configuration=ident,status='ALREADY_SCORED');continue
                    if model in ('tabicl_ordinary','tabicl_chronological') and model not in ckpt:
                        note('FAILURE',configuration=ident,error='adapted_checkpoint_missing');required_error+=1;continue
                    try:
                        t0=time.monotonic();pred,manifests,resource=sample_scores(a,out,h,q,fold,model,seed,budget,feature_hash,ckpt.get(model))
                        rows=cell_rows(q,pred,model,fold,seed,budget,a.stage,wall=time.monotonic()-t0,vram=gpu_peak())
                        score_path.parent.mkdir(parents=True,exist_ok=True);pd.DataFrame(rows).to_csv(score_path,index=False)
                        record_predictions(out,q,pred,fold,model,seed,budget,a.stage)
                        for m in manifests:
                            with (out/'all_support_manifests.jsonl').open('a') as ff:ff.write(json.dumps(m,default=str)+'\n')
                        for r in resource:append_csv(out/'all_resource_profiles.csv',r)
                        coverage.append(dict(fold=fold,seed=seed,model=model,budget=budget,status='SUCCESS',cells=len(rows),elapsed_seconds=time.monotonic()-t0))
                        note('MACRO RESULTS',configuration=ident,macro_ap=float(np.nanmean([r['ap'] for r in rows])),macro_f2=float(np.mean([r['fbeta_at_20'] for r in rows])),brier=float(np.mean([r['brier'] for r in rows])),cells=len(rows))
                    except Exception as exc:
                        coverage.append(dict(fold=fold,seed=seed,model=model,budget=budget,status='FAILED',reason=str(exc)));required_error+=1;note('FAILURE',configuration=ident,error=repr(exc));traceback.print_exc()
                        if not a.continue_on_error:raise
                    append_csv(out/'coverage_manifest.csv',coverage[-1])
        save_report(out)
    if a.stage=='development' and required_error==0:
        lock={'stage':'development','feature_hash':feature_hash,'faculties':faculties,'models':requested,'budgets':budgets,'release_gap':a.release_gap,'seeds':seeds,'folds':folds,'checkpoint_source':a.tabicl_checkpoint,'epochs':a.epochs,'patience':a.patience,'learning_rate':a.learning_rate,'weight_decay':a.weight_decay,'grad_clip':a.grad_clip,'episode_query_size':a.episode_query_size,'episode_support_size':a.episode_support_size,'max_episodes':a.max_episodes,'validation_blocks':a.validation_blocks,'n_estimators':a.n_estimators,'n_estimators_train':a.n_estimators_train,'created_without_F6_F7_selection':True}
        lock['config_hash']=stable_hash(lock);dump_json(out/'frozen_development_protocol.json',lock);note('GATE',status='FROZEN_DEVELOPMENT_PROTOCOL',path=out/'frozen_development_protocol.json')
    note('RUN STATUS',status='SUCCESS' if required_error==0 else 'PARTIAL',failed_jobs=required_error,output=out)
    if required_error:raise RuntimeError(f'{required_error} jobs failed; see coverage_manifest.csv')

def main():
    a=parse()
    try:run(a)
    except Exception as exc:
        note('RUN STATUS',status='FAILURE',reason=repr(exc));traceback.print_exc();sys.exit(1)
if __name__=='__main__':main()
