#!/usr/bin/env python3
"""Suite33 OVOCL: audited origin-context model comparison, development freeze, retrospective report.

The scientific default is strict. There is deliberately no flag to substitute an outcome-filtered final roster.
"""
from __future__ import annotations
import argparse,json,sys,time,traceback
from pathlib import Path
import numpy as np
import pandas as pd
import joblib
from s33_common import ARMS,log,main_env,save_csv,save_json,seed,metrics,train_predict,sha
from s33_data import faculties,load,common_features,load_target_keys,semord
from s33_context import ContextStore,RosterGateError,example_inputs
from s33_raw import ReconstructedStore
from s33_report import make_macro,freeze,final,pair

def options():
    p=argparse.ArgumentParser(description='AQ Suite33 full dev+retrospective; strict origin-time roster contract')
    p.add_argument('--root',default='/root/disk_3tb/earlywarning/30072026')
    p.add_argument('--roster-manifest',default=None,help='Only used in --mode certified; independently attested institutionally archived roster.')
    p.add_argument('--mode',choices=['reconstructed','certified'],default='reconstructed',help='reconstructed is exploratory and is the default for existing AQ database. certified uses separately attested as-of snapshots.')
    p.add_argument('--database-dir',default=None,help='Raw 28-table CSV directory. Default: <root>/../database')
    p.add_argument('--target-keys',default=None,help='Only for canonical caches missing IDToHoc; immutable row-key->section crosswalk.')
    p.add_argument('--max-iter',type=int,default=100)
    p.add_argument('--bootstrap',type=int,default=100,help='Paired cluster-resampled descriptive CI draws; 0 disables')
    p.add_argument('--min-roster-coverage',type=float,default=None,help='Default 0.0 for reconstructed (keep canonical targets), 0.98 for certified')
    p.add_argument('--folds',default='1,2,3,4,5,6,7',help='Development/test debugging subset; complete publication requires all 1..7')
    p.add_argument('--max-faculties',type=int,default=14,help='For synthetic smoke-test only, not a comparable full AQ run')
    p.add_argument('--smoke-test',action='store_true',help='Accept subsets and skip historical promotion validity requirements; no scientific claim')
    p.add_argument('--create-input-examples',action='store_true')
    return p.parse_args()

def ref_reports(root,scores):
    refpath=Path(__file__).parent/'reference'/'historical_model_ap_cells.csv'
    if not refpath.exists():return
    ref=pd.read_csv(refpath);ref=ref[ref.fold.isin([6,7])]
    candidates=scores[(scores.fold>=6)&(scores.arm.isin(['S0','S4']))][['fold','faculty','arm','ap']]
    rows=[]
    for model in ('suite25_committee_blend','suite22','committee','suite25_sacr','suite27_mcgb','suite17'):
        a=ref[ref.model==model][['fold','faculty','ap']].drop_duplicates(['fold','faculty'])
        for arm in ('S0','S4'):
            b=candidates[candidates.arm==arm]
            z=b.merge(a,on=['fold','faculty'],suffixes=('_candidate','_historical'))
            if z.empty:continue
            delta=z.ap_candidate-z.ap_historical
            z=z.assign(model=model,delta_ap=delta)
            rows.extend(z.to_dict('records'))
            log('RETROSPECTIVE_HISTORICAL_COMPARISON',candidate=arm,reference=model,cells=len(z),candidate_mean=z.ap_candidate.mean(),reference_mean=z.ap_historical.mean(),delta_mean=delta.mean(),wins=int((delta>0).sum()),note='METRICS_ONLY_NOT_ROW_PAIR')
    save_csv(Path(root)/'reports'/'archived_cell_head_to_head.csv',rows)


def audit_cache_coverage(cache_dir,fac,fold,out):
    rows=[]
    for name in fac:
        d=joblib.load(Path(cache_dir)/f'{name}_fold_{fold}.joblib')
        y=np.asarray(d['ytr']);mask=np.asarray(d['cf_covered_mask'],bool)&np.isfinite(np.asarray(d['cf_probabilities'],float)).all(axis=1)
        row=dict(fold=fold,faculty=name,cache_rows=len(y),cache_failures=int(y.sum()),retained_rows=int(mask.sum()),retained_failures=int(y[mask].sum()),excluded_rows=int((~mask).sum()),excluded_failures=int(y[~mask].sum()),cause='NO_VALID_CF_COVERAGE; full original split-to-cache reconciliation separately required')
        rows.append(row);log('ROW_COVERAGE',**row)
    save_csv(Path(out)/'audits'/f'cache_coverage_fold_{fold}.csv',rows)
    log('DATA_GATE',fold=fold,coverage_reconciliation='CACHE_MASK_EXACT',source_split_grade_release='PROVIDER_ATTESTED',source_split_imputation_2x2='NOT_EXECUTED_NEEDS_ORIGINAL_SPLIT_SOURCE')


def make_strata(fold,fac,cell,feature_frame,model_preds,output):
    """Descriptive, target-only post-hoc diagnostics. Do NOT use for arm choice."""
    if 'S0' not in model_preds or 'S4' not in model_preds:return
    values=feature_frame
    y=cell.y
    g=values['s4_gpa_gap'].to_numpy(float)
    peers=values['s1_sec_peers'].fillna(0).to_numpy(float)
    faccount=values['s1_sec_faculties'].fillna(0).to_numpy(float)
    match=values['is_placebo_matchable'].fillna(0).to_numpy(float)>0
    rows=[]
    for name,which in [('small_section',peers<10),('medium_section',(peers>=10)&(peers<40)),('large_section',peers>=40),('cross_faculty',faccount>1),('single_faculty',faccount<=1),('matched_placebo',match),('unmatched_placebo',~match),('below_peer_mean',np.isfinite(g)&(g<0)),('above_peer_mean',np.isfinite(g)&(g>=0)),('no_valid_gap',~np.isfinite(g))]:
        if which.sum()<20 or len(np.unique(y[which]))<2:continue
        b=metrics(y[which],model_preds['S0'][which]);a=metrics(y[which],model_preds['S4'][which])
        rows.append(dict(fold=fold,faculty=fac,stratum=name,rows=int(which.sum()),failures=int(y[which].sum()),S0_ap=b['ap'],S4_ap=a['ap'],delta_ap=a['ap']-b['ap']))
    if rows:
        save_csv(Path(output)/'insights'/f'F{fold}_{fac}_strata.csv',rows)
        for r in rows:log('RELATIVE_GAP',**r)
    if match.sum()>=20 and len(np.unique(y[match]))==2:
        a=metrics(y[match],model_preds['S4'][match]);b=metrics(y[match],model_preds['P1'][match])
        log('PLACEBO_MATCH',fold=fold,faculty=fac,matched_rows=int(match.sum()),matched_failures=int(y[match].sum()),S4_ap=a['ap'],P1_ap=b['ap'],delta_ap=a['ap']-b['ap'],note='course_semester_faculty_stratified_section_permutation')
    return int(match.sum())


def retrospective_origin_subset(preds,out):
    """Sensitivity for target rows whose source keys existed at cutoff proxy.
    Note: the fitted peer context itself remains reconstructed from all records;
    this is a *target-subset* sensitivity, NOT a verified origin-roster test.
    """
    from sklearn.metrics import average_precision_score
    z=preds[(preds.fold>=6)&(preds.origin_candidate>0)&preds.arm.isin(['S0','S4','P1'])]
    rows=[]
    for (fold,fac),grp in z.groupby(['fold','faculty']):
        g={a:x.sort_values('row_index') for a,x in grp.groupby('arm')}
        if set(g)!={'S0','S4','P1'}:continue
        a=g['S0'];b=g['S4'];p=g['P1']
        if not (np.array_equal(a.row_index,b.row_index) and np.array_equal(a.row_index,p.row_index)):
            raise ValueError('Source-quality sensitivity rows misaligned')
        y=a.y.to_numpy(int)
        if len(a)<30 or len(np.unique(y))<2 or y.sum()<5:continue
        r=dict(fold=fold,faculty=fac,rows=len(y),failures=int(y.sum()),S0_ap=float(average_precision_score(y,a.p)),S4_ap=float(average_precision_score(y,b.p)),P1_ap=float(average_precision_score(y,p.p)))
        r['S4_minus_S0']=r['S4_ap']-r['S0_ap'];rows.append(r)
        log('ORIGIN_CANDIDATE_SUBSET',**r,note='TARGET_SUBSET_ONLY_FULL_RECONSTRUCTED_PEER_CONTEXT_NOT_VERIFIED')
    save_csv(Path(out)/'reports'/'retrospective_origin_candidate_subset.csv',rows)
    log('ORIGIN_CANDIDATE_SUBSET',qualified_cells=len(rows),note='NO_CLAIM_OF_ORIGIN_VERIFICATION')

def main():
    a=options();root=Path(a.root);out=root/'results'/'suite33_offering_context';out.mkdir(parents=True,exist_ok=True)
    for d in ('audits','context','development','retrospective','predictions','insights','reports','examples'):(out/d).mkdir(exist_ok=True)
    example_inputs(Path(__file__).parent/'examples')
    log('START',name='Suite33_EXPLORATORY_RECONSTRUCTED_OR_CERTIFIED',root=str(root),output=str(out),arguments=vars(a),environment=main_env())
    log('SCIENTIFIC_CONTRACT',label='grade10_lt5',primary='macro_AP_equal_faculty_fold_cells',train='faculty_local_labels',context='other_prior_semester_GPA_from_current_database_roster',thresholds='prior_development_folds_only',holdout='F6_F7_RETROSPECTIVE_NOT_UNTOUCHED',gpu='HGB_CPU',roster_policy='RECONSTRUCTED_IS_EXPLORATORY_NOT_ORIGIN_ATTESTED' if a.mode=='reconstructed' else 'CERTIFIED_SOURCE_ATTESTATION_REQUIRED')
    if a.create_input_examples:
        log('DATA_GATE',status='EXAMPLE_CREATED',directory=str(Path(__file__).parent/'examples'));return
    if a.mode=='certified':
        manifest=a.roster_manifest or str(root/'data'/'asof_rosters'/'roster_manifest.json')
        roster=ContextStore(manifest).compute(out/'context')
        coverage=.98 if a.min_roster_coverage is None else a.min_roster_coverage
    else:
        database=Path(a.database_dir) if a.database_dir else root.parent/'database'
        if a.roster_manifest:raise ValueError('--roster-manifest is only meaningful with --mode certified')
        roster=ReconstructedStore(database,out/'audits'/'raw_roster',cutoff='semester_start').compute(out/'context')
        coverage=0.0 if a.min_roster_coverage is None else a.min_roster_coverage
        log('RESEARCH_MODE_WARNING',status='EXPLORATORY_ONLY',certified=False,origin_valid=False,future_section_membership_possible=True,do_not_deploy=True,source=str(database))
    if not (0<=coverage<=1):raise ValueError('--min-roster-coverage must be between 0 and 1')
    cache=root/'results'/'cross_faculty_residual_moe'/'cache';structured=root/'results'/'suite12_structured_cache'
    fs=faculties(cache,1)[:a.max_faculties]
    requested=[int(x) for x in a.folds.split(',')]
    if not a.smoke_test and (a.max_iter!=100 or (a.mode=='certified' and coverage<.95)):
        raise ValueError('Full runs require max_iter=100; certified source also requires >=.95 roster coverage. Use --smoke-test for altered runs')
    if (requested!=list(range(1,8)) or len(fs)!=14) and not a.smoke_test:raise ValueError('Non-full AQ runs must explicitly pass --smoke-test; otherwise exactly 14 faculties and F1-F7 are required.')
    lookup=load_target_keys(a.target_keys)
    common=None;all_scores=[];pred_frames=[];scope=[];fold_details=[]
    for fold in requested:
        stage='development' if fold<=5 else 'retrospective';timefold=time.monotonic()
        log('FOLD_START',fold=fold,stage=stage,faculties=len(fs))
        audit_cache_coverage(cache,fs,fold,out)
        pairs={}
        for fac in fs:
            h,t=load(cache,structured,fac,fold);pairs[fac]=(h,t)
        current=common_features([z[0] for z in pairs.values()])
        if common is None:common=current
        if current!=common:raise ValueError(f'F{fold} feature intersection changed across folds')
        log('DATA_GATE',fold=fold,common_raw=len(common),course_features=17,base_total=len(common)+17,history_rows=sum(len(v[0]) for v in pairs.values()),target_rows=sum(len(v[1]) for v in pairs.values()),target_failures=sum(int(v[1].y.sum()) for v in pairs.values()))
        base_target_origins=[]
        for fac,(h,t) in pairs.items():
            hk=h.keys(lookup);tk=t.keys(lookup);origins=roster.roster[roster.roster.semester.isin(tk.semester.unique())].groupby('semester').origin_at.first()
            if set(tk.semester)-set(origins.index):raise RosterGateError(f'{fac} F{fold}: target semester missing from origin roster {set(tk.semester)-set(origins.index)}')
            target_origin=min(origins)
            roster.verify_label_release(hk.semester.unique(),target_origin)
            if max(map(semord,hk.semester))>=min(map(semord,tk.semester)):raise RosterGateError('Future semester in historical learning labels')
            base_target_origins.append(target_origin)
            histcontext=roster.fetch(hk,min_coverage=coverage)
            testcontext=roster.fetch(tk,min_coverage=coverage)
            pairs[fac]=(h,t,histcontext,testcontext,hk,tk)
            scope.append(dict(fold=fold,faculty=fac,rows=len(t),matchable_rows=int((testcontext.is_placebo_matchable.fillna(0)>0).sum())))
            log('ASOF_ROSTER_AUDIT',fold=fold,faculty=fac,historical=len(h),target=len(t),matching_target_sections=int(testcontext.s1_sec_peers.notna().sum()),cross_faculty_target_fraction=float((testcontext.s1_sec_faculties>1).mean()),mean_peers=float(testcontext.s1_sec_peers.mean()),mode=a.mode)
            if a.mode=='reconstructed':
                log('ORIGIN_MEMBERSHIP_SENSITIVITY',fold=fold,faculty=fac,target_rows=len(t),candidate_preorigin=int((testcontext.origin_member_candidate_DIAGNOSTIC_ONLY>0).sum()),postcutoff_registration=int((testcontext.registration_late_DIAGNOSTIC_ONLY>0).sum()),postcutoff_section=int((testcontext.section_late_DIAGNOSTIC_ONLY>0).sum()),postcutoff_change=int((testcontext.section_changed_late_DIAGNOSTIC_ONLY>0).sum()),note='DIAGNOSTIC_FLAGS_NEVER_USED_IN_PREDICTION')
        if max(base_target_origins)!=min(base_target_origins):log('DATA_WARNING',fold=fold,message='Non-uniform institution origins across faculty target semesters; cutoffs checked per faculty')
        for fac,(h,t,hc,tc,hk,tk) in pairs.items():
            X=h.X(common);T=t.X(common)
            if X.shape[1]!=153 and not a.smoke_test:raise ValueError(f'Expected exactly 153 S0 features, got {X.shape[1]}')
            model_probs={};this_fac_scores=[]
            for arm in ARMS:
                x=np.concatenate([X,roster.matrix(hc,arm)],axis=1)
                z=np.concatenate([T,roster.matrix(tc,arm)],axis=1)
                pred,diag=train_predict(x,h.y,z,seed_value=42,max_iter=a.max_iter)
                d=metrics(t.y,pred);model_probs[arm]=pred
                result=dict(fold=fold,stage=stage,faculty=fac,arm=arm,feature_count=x.shape[1],training_rows=len(h),**d,**diag)
                this_fac_scores.append(result);all_scores.append(result)
                log('TRAINING_DIAGNOSTICS',fold=fold,faculty=fac,arm=arm,seconds=diag['seconds'],iterations=diag['iterations'],train_ap=diag['in_sample_ap'],train_bce=diag['in_sample_bce'],features=x.shape[1])
                log('FACULTY_RESULTS',fold=fold,faculty=fac,arm=arm,**d)
                pred_frames.append(pd.DataFrame(dict(
                    fold=fold,faculty=fac,arm=arm,row_index=t.row_index.astype(int),
                    student_id=tk.student_id.to_numpy(str),course_id=tk.course_id.to_numpy(str),
                    section_id=tk.section_id.to_numpy(str),semester=tk.semester.to_numpy(str),
                    offering_key=(tk.semester+'|'+tk.course_id+'|'+tk.section_id).to_numpy(str),
                    y=t.y.astype(int),p=pred,committee=t.committee.astype(float),
                    placebo_matchable=tc.is_placebo_matchable.fillna(0).to_numpy(float),
                    origin_candidate=tc['origin_member_candidate_DIAGNOSTIC_ONLY'].to_numpy(float) if a.mode=='reconstructed' else np.ones(len(t),float),
                    late_registration=tc['registration_late_DIAGNOSTIC_ONLY'].to_numpy(float) if a.mode=='reconstructed' else np.zeros(len(t),float),
                    late_section=tc['section_late_DIAGNOSTIC_ONLY'].to_numpy(float) if a.mode=='reconstructed' else np.zeros(len(t),float))))
            sc=pd.DataFrame(this_fac_scores)
            for arm_a,arm_b in [('S4','S0'),('S4','S1'),('S3','S2'),('S4','P1')]:
                u=sc.loc[sc.arm==arm_a,'ap'].iloc[0]-sc.loc[sc.arm==arm_b,'ap'].iloc[0]
                log('CONTEXT_INCREMENT',fold=fold,faculty=fac,candidate=arm_a,reference=arm_b,delta_ap=float(u))
            log('COURSE_VS_SECTION',fold=fold,faculty=fac,S2_ap=float(sc.loc[sc.arm=='S2','ap'].iloc[0]),S3_ap=float(sc.loc[sc.arm=='S3','ap'].iloc[0]),S3_minus_S2=float(sc.loc[sc.arm=='S3','ap'].iloc[0]-sc.loc[sc.arm=='S2','ap'].iloc[0]))
            make_strata(fold,fac,t,tc,model_probs,out)
            save_csv(out/'predictions'/f'F{fold}_{fac}_scores.csv',pd.concat(pred_frames[-len(ARMS):],ignore_index=True))
            log('FACULTY_COMPLETE',fold=fold,faculty=fac,rows=len(t),positive=int(t.y.sum()),arms=list(ARMS),seconds=sum(float(d['seconds']) for d in this_fac_scores))
        save_csv(out/'audits'/f'fold_{fold}_schema.csv',[dict(faculty=f,raw_features=len(v[0].raw_names),common_features=len(common),course_features=17,history_rows=len(v[0]),target_rows=len(v[1])) for f,v in pairs.items()])
        save_csv(out/stage/f'F{fold}_scores.csv',[x for x in all_scores if x['fold']==fold])
        log('FOLD_COMPLETE',fold=fold,seconds=time.monotonic()-timefold,stage=stage)
    scores=pd.DataFrame(all_scores);preds=pd.concat(pred_frames,ignore_index=True)
    save_csv(out/'reports'/'all_faculty_fold_metrics.csv',scores);save_csv(out/'reports'/'all_predictions.csv',preds)
    save_csv(out/'development'/'placebo_scope.csv',[x for x in scope if x['fold']<=5])
    save_json(out/'audits'/'runtime_manifest.json',dict(roster=roster.hashes,mode=a.mode,verified_origin=a.mode=='certified',environment=main_env(),folds=requested,faculties=fs,base_features=common,feature_contract=roster.families,model='HistGradientBoostingClassifier_default_100_iter',original_split_imputation_audit='NEEDS_EXPLICIT_SOURCE_TABLES_NOT_CLAIMED_PERFORMED'))
    if not set(range(1,6)).issubset(requested):
        if a.smoke_test:log('DECISION',status='SMOKE_TEST_ONLY_NO_FREEZE');return
        raise ValueError('F1-F5 development required to freeze policy')
    decision=freeze(scores,preds,out)
    if a.mode=='reconstructed':
        decision['scientific_status']='EXPLORATORY_DEVELOPMENT_DIAGNOSTIC_NOT_DEPLOYABLE'
        decision['promoted_for_deployment']=False
        save_json(out/'development'/'frozen_decision.json',decision)
        log('PROMOTION_GUARD',mode='RECONSTRUCTED',development_candidate=decision['selected'],promoted_for_deployment=False,reason='UNVERIFIED_HISTORICAL_ROSTER')
    if {6,7}.issubset(requested):
        if not a.smoke_test:ref_reports(out,scores)
        final(scores,preds,out,decision,bootstrap_draws=(a.bootstrap if not a.smoke_test else 0),include_archived=not a.smoke_test)
        if a.mode=='reconstructed':
            retrospective_origin_subset(preds,out)
            save_json(out/'reports'/'scientific_status.json',dict(status='EXPLORATORY_NOT_DEPLOYABLE',roster='CURRENT_STATE',origin='SEMESTER_START_PROXY',grade_availability='ENTRY_DATE_AND_PRIOR_SEMESTER_PROXY',f6_f7='REUSED_RETROSPECTIVE',promoted_for_deployment=False))
        for fold in [6,7]:
            p=preds[(preds.fold==fold)&(preds.arm=='S0')]
            log('FAILURE_MIX',fold=fold,rows=len(p),failures=int(p.y.sum()),failure_prevalence=float(p.y.mean()),grade_zero_vs_nearpass='NOT_OBSERVED_IN_CANONICAL_BINARY_CACHE; original numeric grades required')
    else:log('DECISION',status='SMOKE_ONLY_NO_RETROSPECTIVE')
    log('INSIGHT',note='Source provenance status depends on --mode. Reconstructed mode uses non-versioned current-state enrollment and unverified grade-entry proxies. The 153 cached own-history features remain semester-proxy; no causal/deployable early-warning conclusion is justified.')
    log('RUN_STATUS',status='SUCCESS',output=str(out),research_mode='SMOKE_ONLY' if a.smoke_test else ('EXPLORATORY_RECONSTRUCTED' if a.mode=='reconstructed' else 'SOURCE_ATTESTED_ORIGIN'),promoted_for_deployment=False if a.mode=='reconstructed' else decision['promoted'],overall_cells=len(scores))

if __name__=='__main__':
    try:main()
    except BaseException as e:
        log('DATA_GATE',status='FAILED',error_class=type(e).__name__,message=str(e))
        traceback.print_exc()
        log('RUN_STATUS',status='FAILED',reason=str(e));sys.exit(1)
