from pathlib import Path
import json
import subprocess
import sys

import joblib
import numpy as np
import pandas as pd


def _support(n, rng):
    hs=rng.integers(0,5,size=n)
    return pd.DataFrame({
        'history_semesters':hs,
        'history_rows':hs+rng.integers(0,4,size=n),
        'new_student':(hs==0).astype(float),
        'course_prior_rows':rng.integers(0,8,size=n),
        'new_course':rng.integers(0,2,size=n).astype(float),
    })


def _make_cache(path:Path, faculty:str, fold:int, seed:int):
    rng=np.random.default_rng(seed)
    ntr,nte,d=72,24,3
    Xtr=rng.normal(size=(ntr,d));Xte=rng.normal(size=(nte,d))
    ytr=np.zeros(ntr,dtype=int);ytr[rng.choice(ntr,18,replace=False)]=1
    yte=np.zeros(nte,dtype=int);yte[rng.choice(nte,6,replace=False)]=1
    base_tr=np.clip(.12+.55*ytr+rng.normal(0,.12,ntr),.01,.99)
    cf=np.column_stack([np.clip(base_tr+rng.normal(0,.04,ntr),.01,.99) for _ in range(3)])
    base_te=np.clip(.12+.55*yte+rng.normal(0,.12,nte),.01,.99)
    pte=np.column_stack([np.clip(base_te+rng.normal(0,.04,nte),.01,.99) for _ in range(3)])
    blocks=np.repeat([1.,2.,3.],24)
    cache={
        'feature_names':['f1','f2','f3'],
        'Xtr':Xtr,'Xte':Xte,'ytr':ytr,'yte':yte,
        'cf_covered_mask':np.ones(ntr,dtype=bool),
        'cf_probabilities':cf,'cf_block_ids':blocks,
        'support_hist':_support(ntr,rng),'support_target':_support(nte,rng),
        'pte':pte,'qte':pte.mean(axis=1),
        'test_frame':pd.DataFrame({'dummy':np.arange(nte)}),
    }
    joblib.dump(cache,path/f'{faculty}_fold_{fold}.joblib')
    return cache


def _score_frame(y,q,score,model):
    n=len(y)
    d={
        'actual':y,'committee':q,model:score,
        'correction_logit':np.zeros(n),
        'weight_null':np.full(n,.2),
    }
    for k in range(1,5):
        d[f'weight_expert_{k}']=np.full(n,.2)
        d[f'residual_expert_{k}']=np.zeros(n)
    return pd.DataFrame(d)


def test_suite8_end_to_end_small(tmp_path:Path):
    cache_dir=tmp_path/'cache';shared_dir=tmp_path/'shared';suite7_dir=tmp_path/'suite7';out=tmp_path/'out'
    cache_dir.mkdir();shared_dir.mkdir();suite7_dir.mkdir()
    faculties=['fac_a','fac_b']
    for fold in [1,2,3]:
        sfd=shared_dir/f'fold_{fold}';afd=suite7_dir/f'fold_{fold}';sfd.mkdir();afd.mkdir()
        schema={'fold':fold,'faculties':faculties,'common_raw_features':['f1','f2','f3'],'shared_feature_names':[]}
        (sfd/'feature_schema.json').write_text(json.dumps(schema))
        (afd/'feature_schema.json').write_text(json.dumps(schema))
        for i,f in enumerate(faculties):
            c=_make_cache(cache_dir,f,fold,seed=1000+fold*10+i)
            q=c['qte'];y=c['yte']
            shared=np.clip(q + (0.01 if f=='fac_a' else -0.01),.001,.999)
            adapter=np.clip(shared + (0.005 if f=='fac_a' else -0.005),.001,.999)
            _score_frame(y,q,shared,'residual_moe_shared').to_csv(sfd/f'{f}_scores.csv',index=False)
            af=_score_frame(y,q,shared,'residual_moe_shared')
            af['residual_moe_shared_private']=adapter
            af.to_csv(afd/f'{f}_scores.csv',index=False)

    script=Path(__file__).resolve().parents[1]/'train_suite8_true_specialist.py'
    cmd=[
        sys.executable,str(script),
        '--cache_dir',str(cache_dir),'--shared_results_dir',str(shared_dir),'--suite7_results_dir',str(suite7_dir),'--models_dir',str(out),
        '--faculties','all','--dev_folds','1,2','--holdout_folds','3','--device','cpu',
        '--epochs','2','--min_epochs','1','--patience','1','--fallback_epochs','1','--val_blocks','1',
        '--min_train_rows','10','--min_val_rows','5','--batch_size','16','--predict_batch_size','64',
        '--encoder_hidden','16','--representation_dim','12','--expert_hidden','8','--faculty_dim','3',
        '--adapter_dim','4','--dropout','0','--adapter_dropout','0','--print_every','1',
        '--view_strength','0.5','--view_entropy_lambda','0.02','--view_balance_lambda','0.02',
        '--min_precision','0','--max_warning_ratio','10','--min_flags','1',
    ]
    cp=subprocess.run(cmd,cwd=str(script.parent),text=True,capture_output=True,timeout=120)
    assert cp.returncode==0,cp.stdout+'\n'+cp.stderr
    for name in [
        'suite8_true_specialist_comparison.csv','suite8_specialist_diagnostics.csv',
        'suite8_expert_ownership.csv','suite8_counterfactual_utility.csv',
        'suite8_collapse_summary.csv','suite8_warning_metrics.csv',
    ]:
        assert (out/name).exists(),name
    assert (out/'fold_3'/'true_specialist_model.pt').exists()
    cmp=pd.read_csv(out/'suite8_true_specialist_comparison.csv')
    assert set(cmp.faculty)==set(faculties)
    assert {'specialist_minus_adapter_ap','specialist_minus_adapter_drift_f2','evidence_pattern'} <= set(cmp.columns)
    own=pd.read_csv(out/'suite8_expert_ownership.csv')
    assert {'expert','latent_dim','ownership','scale'} <= set(own.columns)
