import sys
from pathlib import Path
import numpy as np
import pandas as pd
import torch

ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT))


def test_e0_starts_exactly_at_committee_probability():
    from suite13_models import CommitteePreservingGlobalExpert
    torch.manual_seed(0)
    m=CommitteePreservingGlobalExpert(flat_dim=6,context_dim=3,n_faculties=2,d_model=16,residual_bound=2.5,dropout=0.0)
    m.eval()
    probs=torch.tensor([[.1,.2,.3],[.7,.8,.9],[.05,.05,.05]],dtype=torch.float32)
    with torch.no_grad():
        _,logit,_,delta=m(torch.randn(3,6),torch.randn(3,3),torch.tensor([0,1,0]),probs)
    expected=probs.mean(1)
    assert torch.allclose(torch.sigmoid(logit),expected,atol=1e-6)
    assert torch.allclose(delta,torch.zeros_like(delta),atol=1e-7)


def test_gate_context_adds_semester_but_fusion_context_remains_support_only():
    from suite13_pipeline import build_context_views
    names=['hist_warning_rate','prev_conduct_score','target_semester_registered_credits']
    raw=np.array([[.8,70.,24.],[.1,90.,15.]],dtype=np.float32)
    support=pd.DataFrame({'history_semesters':[2,4],'history_rows':[9,25],'course_prior_rows':[3,80],'new_student':[0,0],'new_course':[1,0]})
    sem=np.array([20221,20222])
    v=build_context_views(raw,names,support,sem)
    assert v['fusion'].shape==(2,5)
    assert v['gate'].shape==(2,6)
    assert not np.array_equal(v['gate'][:,-1],np.zeros(2))
    raw2=raw.copy();raw2[:,0]=1-raw2[:,0];raw2[:,1]+=100;raw2[:,2]+=50
    v2=build_context_views(raw2,names,support,sem)
    assert np.array_equal(v['fusion'],v2['fusion'])
    assert np.array_equal(v['gate'],v2['gate'])


def test_mechanism_targets_are_structured_evidence_specific():
    from suite13_pipeline import build_mechanism_targets
    y=np.array([1,0],dtype=np.int8);seq=np.zeros((2,1,14),np.float32);sm=np.ones((2,1),bool)
    seq[:,0,0]=[4.,8.];seq[:,0,2]=20.;seq[:,0,3]=[10.,20.];seq[:,0,4]=[10.,0.];seq[:,0,12]=1.
    course=np.zeros((2,17),np.float32);course[:,-5]=[50.,20.];course[:,-2]=[.30,.05]
    comp=np.zeros((2,1,6),np.float32);cm=np.zeros((2,1),bool)
    target=build_mechanism_targets(y,seq,sm,course,comp,cm)
    assert target[0,0]>target[1,0]
    assert np.allclose(target[:,1],[.30,.05])
    assert np.array_equal(target[:,2],y.astype(np.float32))


def test_mastery_target_uses_only_masked_history_and_rewards_low_recent_related_components():
    from suite13_pipeline import mastery_deficiency_target
    # features: grade10, weight, passed, recency, same_course, course_related
    x=np.array([
        [[2.,50.,0.,1.,1.,1.],[9.,50.,1.,5.,0.,0.],[0.,0.,0.,0.,0.,0.]],
        [[9.,50.,1.,1.,1.,1.],[8.,50.,1.,2.,0.,1.],[1.,99.,0.,1.,1.,1.]],
    ],dtype=np.float32)
    mask=np.array([[1,1,0],[1,1,0]],dtype=bool)
    t=mastery_deficiency_target(x,mask)
    assert 0 <= t[0] <= 1 and 0 <= t[1] <= 1
    assert t[0] > t[1]
    # Changing padded garbage must not matter.
    x2=x.copy();x2[:,2,:]=999
    assert np.allclose(t,mastery_deficiency_target(x2,mask))


def test_robust_selection_score_is_faculty_balanced_not_row_pooled():
    from suite13_training import robust_committee_selection_stats
    # Faculty 0 is huge and slightly better; faculty 1 is small and much worse.
    y_big=np.tile([0,1,0,1],50); q_big=np.tile([.1,.6,.9,.8],50); c_big=np.tile([.1,.8,.2,.9],50)
    y_small=np.tile([0,1],5); q_small=np.tile([.1,.8],5); c_small=np.tile([.9,.1],5)
    y=np.r_[y_big,y_small]
    faculty=np.r_[np.zeros(len(y_big),int),np.ones(len(y_small),int)]
    committee=np.r_[q_big,q_small]
    cand=np.r_[c_big,c_small]
    s=robust_committee_selection_stats(y,cand,committee,faculty,['big','small'],f2_weight=.5,mean_weight=.25)
    assert s['faculties']==2
    assert s['ap_wins']==1
    assert s['median_delta_ap'] < 0.1  # small faculty cannot be washed out by row count
    assert 'worst_quartile_delta_ap' in s


def test_joint_optimizer_scales_only_e0_learning_rate():
    from suite13_models import CollaborativeEvidenceMoE
    from suite13_training import build_optimizer
    m=CollaborativeEvidenceMoE(flat_dim=8,e0_context_dim=3,gate_context_dim=6,fusion_context_dim=5,e1_context_dim=4,e2_context_dim=3,e3_context_dim=4,e4_context_dim=3,
        student_dim=4,course_dim=5,relation_dim=7,component_numeric_dim=6,n_faculties=2,n_component_types=6,d_model=16,dropout=0.,e0_dropout=0.,expert_dropout=0.)
    opt=build_optimizer(m,phase=3,lr=1e-3,weight_decay=1e-4,e0_lr_scale=.2)
    lrs=sorted(set(round(g['lr'],8) for g in opt.param_groups))
    assert lrs==[0.0002,0.001]


def test_gate_receives_faculty_context_but_final_head_does_not_directly_consume_it():
    from suite13_models import CollaborativeEvidenceMoE
    m=CollaborativeEvidenceMoE(flat_dim=8,e0_context_dim=3,gate_context_dim=6,fusion_context_dim=5,e1_context_dim=4,e2_context_dim=3,e3_context_dim=4,e4_context_dim=3,
        student_dim=4,course_dim=5,relation_dim=7,component_numeric_dim=6,n_faculties=3,n_component_types=6,d_model=16,dropout=0.,e0_dropout=0.,expert_dropout=0.)
    assert hasattr(m,'gate_fac')
    # Final first Linear only sees fused d_model + neutral fusion support.
    first=next(x for x in m.final if isinstance(x,torch.nn.Linear))
    assert first.in_features==16+5


def test_selected_oof_blocks_ignores_expected_uncovered_blanks():
    from suite13_pipeline import selected_oof_blocks
    d={'cf_block_ids':np.array(['','',20211,20212],dtype=object),'cf_mode':'temporal'}
    got=selected_oof_blocks(d,np.array([2,3]),4)
    assert np.allclose(got,[20211,20212])


def test_retention_diagnostics_report_perfect_identity():
    from suite13_reporting import e0_retention_diagnostics
    y=np.array([0,1,0,1,1,0])
    q=np.array([.1,.8,.2,.7,.9,.3])
    d=e0_retention_diagnostics(y,q,q,np.zeros(len(q)))
    assert abs(d['e0_probability_mae']) < 1e-12
    assert abs(d['e0_rank_corr']-1.0) < 1e-12
    assert abs(d['e0_top20_overlap']-1.0) < 1e-12
    assert abs(d['e0_delta_ap']) < 1e-12


def test_reference_loader_can_compare_suite12_scores(tmp_path):
    from types import SimpleNamespace
    from suite13_reporting import reference_for_faculty,S12
    root=tmp_path/'suite12';p=root/'fold_6';p.mkdir(parents=True)
    y=np.array([0,1,0]);pd.DataFrame({'actual':y,'suite12_collaborative_evidence_moe':[.1,.8,.2]}).to_csv(p/'fac_scores.csv',index=False)
    args=SimpleNamespace(shared_results_dir=None,suite7_results_dir=None,suite8_results_dir=None,suite9_results_dir=None,suite10_results_dir=None,suite11_results_dir=None,suite12_results_dir=str(root),v2_results_dir=None,council_results_dir=None)
    got=reference_for_faculty(args,6,'fac',y,np.array([.2,.7,.3]))
    assert S12 in got and np.allclose(got[S12],[.1,.8,.2])


def test_e1_e2_mechanism_targets_do_not_depend_on_other_rows_labels():
    from suite13_pipeline import build_mechanism_targets
    # student_seq: 14 dims; positions 0 GPA, 2 registered credits, 3 passed credits, 4 failed/debt credits, 12 recency.
    seq=np.zeros((2,2,14),np.float32);mask=np.ones((2,2),bool)
    seq[:,:,0]=[[7,6],[8,8]];seq[:,:,2]=20;seq[:,:,3]=[[18,16],[20,19]];seq[:,:,4]=[[2,4],[0,1]];seq[:,:,12]=[[2,1],[2,1]]
    course=np.zeros((2,17),np.float32);course[:,12]=[100,50];course[:,-2]=[.25,.10]  # hist_count, hist_fail_rate
    comp=np.zeros((2,1,6),np.float32);cm=np.zeros((2,1),bool)
    a=build_mechanism_targets(np.array([0,0]),seq,mask,course,comp,cm)
    b=build_mechanism_targets(np.array([0,1]),seq,mask,course,comp,cm)
    assert np.allclose(a[:,:2],b[:,:2])
    assert np.allclose(a[:,1],[.25,.10])
    assert np.array_equal(a[:,2],[0,0]) and np.array_equal(b[:,2],[0,1])


def test_strict_history_filter_excludes_target_and_future():
    from suite13_structured_cache import prior_indices
    assert np.array_equal(prior_indices([20201,20202,20211],20211),[0,1])


def test_semester_distance_respects_academic_year_rollover():
    from suite13_structured_cache import semester_distance
    assert semester_distance(20211,20202)==2
    assert semester_distance(20212,20211)==1
    assert semester_distance(20213,20212)==1


def test_grade_coalesce_uses_calculated_grade_when_primary_missing():
    from suite13_structured_cache import coalesce_grade
    df=pd.DataFrame({'DiemHP':[np.nan,7.0],'DiemHPCalc':[4.5,8.0]})
    assert np.allclose(coalesce_grade(df).to_numpy(),[4.5,7.0])


def test_curriculum_edge_added_in_same_or_future_term_is_not_history():
    from suite13_structured_cache import edge_is_available
    assert edge_is_available(20211,20212)
    assert not edge_is_available(20212,20212)
    assert not edge_is_available(20221,20212)


def test_relation_code_preserves_prerequisite_kind_and_direction():
    from suite13_structured_cache import encode_relation,relation_is_forward,relation_is_direct
    assert encode_relation(1,1,1)==1 and encode_relation(1,-1,1)==4
    assert relation_is_forward(3) and not relation_is_forward(6)
    assert relation_is_direct(1) and not relation_is_direct(8)


def test_student_semester_dedup_uses_latest_ngaythem():
    from suite13_structured_cache import dedupe_student_semester_latest
    df=pd.DataFrame({'IDSV':[11,11,11],'NHHK':[20212,20212,20211],'DTBHK':[7.18,8.30,6.50],'NgayThem':['2022-03-09','2022-10-17','2022-01-01']})
    got=dedupe_student_semester_latest(df);row=got[(got.IDSV==11)&(got.NHHK==20212)].iloc[0]
    assert row.DTBHK==8.30 and len(got)==2


def test_structured_cache_compatibility_rejects_old_schema(tmp_path):
    from suite13_structured_cache import structured_cache_compatible,SCHEMA_VERSION
    import joblib
    p=tmp_path/'f.joblib';joblib.dump({'config':{'schema_version':SCHEMA_VERSION-1}},p);assert not structured_cache_compatible(p);joblib.dump({'config':{'schema_version':SCHEMA_VERSION}},p);assert structured_cache_compatible(p)


def test_dense_fusion_survives_full_e0_dropout():
    from suite13_models import CollaborativeEvidenceMoE
    b=4;m=CollaborativeEvidenceMoE(flat_dim=8,e0_context_dim=3,gate_context_dim=6,fusion_context_dim=5,e1_context_dim=4,e2_context_dim=3,e3_context_dim=4,e4_context_dim=3,student_dim=4,course_dim=5,relation_dim=7,component_numeric_dim=6,n_faculties=2,n_component_types=6,d_model=16,dropout=0.,e0_dropout=1.,expert_dropout=0.)
    m.train();o=m(flat=torch.randn(b,8),e0_context=torch.randn(b,3),gate_context=torch.randn(b,6),fusion_context=torch.randn(b,5),e1_context=torch.randn(b,4),e2_context=torch.randn(b,3),e3_context=torch.randn(b,4),e4_context=torch.randn(b,3),faculty=torch.zeros(b,dtype=torch.long),committee_probs=torch.rand(b,3)*.8+.1,student_seq=torch.randn(b,2,4),student_mask=torch.ones(b,2,dtype=torch.bool),course_target=torch.randn(b,5),course_neighbors=torch.randn(b,2,5),course_neighbor_mask=torch.ones(b,2,dtype=torch.bool),course_relation_type=torch.ones(b,2,dtype=torch.long),relation_events=torch.randn(b,3,7),relation_mask=torch.ones(b,3,dtype=torch.bool),relation_type=torch.ones(b,3,dtype=torch.long),component_events=torch.randn(b,3,6),component_mask=torch.ones(b,3,dtype=torch.bool),component_type=torch.ones(b,3,dtype=torch.long))
    assert torch.isfinite(o['final_logit']).all() and not o['availability'][:,0].any()
