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
    # predictive but imperfect committee probabilities
    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 test_suite7_end_to_end_small(tmp_path:Path):
    cache_dir=tmp_path/'cache';shared_dir=tmp_path/'shared';out=tmp_path/'out'
    cache_dir.mkdir();shared_dir.mkdir()
    faculties=['fac_a','fac_b']
    for fold in [1,2,3]:
        fd=shared_dir/f'fold_{fold}';fd.mkdir()
        (fd/'feature_schema.json').write_text(json.dumps({
            'fold':fold,'faculties':faculties,
            'common_raw_features':['f1','f2','f3'],
            'shared_feature_names':[],
        }))
        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)
            frame=pd.DataFrame({
                'actual':y,'committee':q,'residual_moe_shared':shared,
                'correction_logit':np.zeros(len(y)),
                'weight_null':np.full(len(y),.2),
                'weight_expert_1':np.full(len(y),.2),
                'weight_expert_2':np.full(len(y),.2),
                'weight_expert_3':np.full(len(y),.2),
                'weight_expert_4':np.full(len(y),.2),
                'residual_expert_1':np.zeros(len(y)),
                'residual_expert_2':np.zeros(len(y)),
                'residual_expert_3':np.zeros(len(y)),
                'residual_expert_4':np.zeros(len(y)),
            })
            frame.to_csv(fd/f'{f}_scores.csv',index=False)

    script=Path(__file__).resolve().parents[1]/'train_suite7_shared_private_adapter.py'
    cmd=[
        sys.executable,str(script),
        '--cache_dir',str(cache_dir),'--shared_results_dir',str(shared_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',
        '--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
    assert (out/'suite7_shared_private_comparison.csv').exists()
    assert (out/'suite7_adapter_diagnostics.csv').exists()
    assert (out/'suite7_adapter_parameter_norms.csv').exists()
    assert (out/'fold_3'/'shared_private_model.pt').exists()
    cmp=pd.read_csv(out/'suite7_shared_private_comparison.csv')
    assert set(cmp.faculty)==set(faculties)
    assert {'private_minus_shared_ap','private_minus_shared_drift_f2','evidence_pattern'} <= set(cmp.columns)
