import numpy as np
import torch


def test_strict_history_filter_excludes_target_and_future():
    from suite12_structured_cache import prior_indices
    sem=np.array([20201,20202,20211,20212])
    assert prior_indices(sem,20211).tolist()==[0,1]


def test_masked_standardizer_ignores_padding():
    from suite12_data import MaskedStandardizer
    x=np.array([[[1.,2.],[0.,0.]],[[3.,4.],[5.,6.]]],dtype=np.float32)
    m=np.array([[1,0],[1,1]],dtype=bool)
    s=MaskedStandardizer.fit(x,m)
    y=s.transform(x,m)
    vals=y[m]
    assert np.allclose(vals.mean(0),0,atol=1e-6)
    assert np.allclose(y[~m],0)


def test_model_forward_is_direct_and_all_experts_present():
    from suite12_models import CollaborativeEvidenceMoE
    b=7
    model=CollaborativeEvidenceMoE(
        flat_dim=12,e0_context_dim=4,fusion_context_dim=3,e1_context_dim=4,e2_context_dim=3,e3_context_dim=4,e4_context_dim=3,student_dim=6,course_dim=8,relation_dim=7,component_numeric_dim=6,
        n_faculties=3,n_component_types=11,d_model=24,
    )
    out=model(
        flat=torch.randn(b,12),e0_context=torch.randn(b,4),fusion_context=torch.randn(b,3),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.randint(0,3,(b,)),
        student_seq=torch.randn(b,5,6),student_mask=torch.ones(b,5,dtype=torch.bool),
        course_target=torch.randn(b,8),course_neighbors=torch.randn(b,4,8),course_neighbor_mask=torch.ones(b,4,dtype=torch.bool),course_relation_type=torch.randint(0,3,(b,4)),
        relation_events=torch.randn(b,6,7),relation_mask=torch.ones(b,6,dtype=torch.bool),relation_type=torch.randint(0,4,(b,6)),
        component_events=torch.randn(b,9,6),component_mask=torch.ones(b,9,dtype=torch.bool),component_type=torch.randint(0,11,(b,9)),
    )
    assert out['final_logit'].shape==(b,)
    assert out['private_logits'].shape==(b,5)
    assert out['gate_weights'].shape==(b,5)
    assert torch.allclose(out['gate_weights'].sum(1),torch.ones(b),atol=1e-5)
    assert out['expert_tokens'].shape==(b,5,24)


def test_e0_distillation_heads_have_three_teacher_outputs():
    from suite12_models import GlobalDistilledExpert
    m=GlobalDistilledExpert(10,4,3,24)
    h,y,t=m(torch.randn(5,10),torch.randn(5,4),torch.randint(0,3,(5,)))
    assert h.shape==(5,24) and y.shape==(5,) and t.shape==(5,3)


def test_dense_fusion_survives_e0_dropout():
    from suite12_models import CollaborativeEvidenceMoE
    m=CollaborativeEvidenceMoE(8,3,3,3,2,3,2,4,5,5,4,2,7,d_model=16,e0_dropout=1.0,expert_dropout=0.0)
    m.train()
    b=4
    out=m(flat=torch.randn(b,8),e0_context=torch.randn(b,3),fusion_context=torch.randn(b,3),e1_context=torch.randn(b,3),e2_context=torch.randn(b,2),e3_context=torch.randn(b,3),e4_context=torch.randn(b,2),faculty=torch.zeros(b,dtype=torch.long),
          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,5),relation_mask=torch.ones(b,3,dtype=torch.bool),relation_type=torch.ones(b,3,dtype=torch.long),
          component_events=torch.randn(b,3,4),component_mask=torch.ones(b,3,dtype=torch.bool),component_type=torch.ones(b,3,dtype=torch.long))
    assert torch.isfinite(out['final_logit']).all()
    assert (out['availability'][:,0]==0).all()


def test_semester_distance_respects_academic_year_rollover():
    from suite12_structured_cache import semester_distance
    assert semester_distance(20211,20202)==2.0
    assert semester_distance(20221,20212)==2.0
    assert semester_distance(20223,20221)==2.0


def test_grade_coalesce_uses_calculated_grade_when_primary_missing():
    from suite12_structured_cache import coalesce_grade
    import pandas as pd
    df=pd.DataFrame({'DiemHP':[8.0,np.nan,np.nan],'DiemHPCalc':[7.5,6.25,np.nan]})
    got=coalesce_grade(df)
    assert np.allclose(got.iloc[:2].to_numpy(float),[8.0,6.25])
    assert np.isnan(got.iloc[2])


def test_curriculum_edge_added_in_same_or_future_term_is_not_history():
    from suite12_structured_cache import date_to_nhhk, edge_is_available
    assert date_to_nhhk('2024-08-09')==20241
    assert date_to_nhhk('2025-01-10')==20242
    assert date_to_nhhk('2025-07-02')==20243
    assert edge_is_available(20241,20242)
    assert not edge_is_available(20242,20242)
    assert not edge_is_available(20243,20242)


def test_phase2_keeps_frozen_encoders_eval_but_training_dropout_active():
    from suite12_models import CollaborativeEvidenceMoE
    from suite12_training import set_phase_mode
    m=CollaborativeEvidenceMoE(8,3,3,3,2,3,2,4,5,5,4,2,7,d_model=16)
    set_phase_mode(m,2)
    assert m.training
    assert not m.e0.training and not m.e1.training and not m.e2.training and not m.e3.training and not m.e4.training
    assert m.exchange.training and m.gate.training and m.final.training


def test_prerequisite_relation_has_priority_over_generic_related_edge():
    from suite12_structured_cache import prefer_edge
    assert prefer_edge((7,20201),(1,20211))==(1,20211)
    assert prefer_edge((1,20221),(7,20191))==(1,20221)
    assert prefer_edge((1,20221),(1,20201))==(1,20201)


def test_relational_evidence_priority_prefers_same_then_prerequisite_then_related():
    from suite12_structured_cache import evidence_priority
    assert evidence_priority(True,0,0)==0
    assert evidence_priority(False,1,0)==1
    assert evidence_priority(False,2,0)==1
    assert evidence_priority(False,3,0)==1
    assert evidence_priority(False,7,0)==2
    assert evidence_priority(False,0,0)==3


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


def test_context_views_keep_course_expert_course_centric():
    from suite12_pipeline import build_context_views
    import pandas as pd
    names=[
        'target_semester_registered_credits','hist_warning_rate','prev_conduct_score',
        'course_history_missing_flag','student_gpa_history_missing_flag'
    ]
    raw=np.array([[18.,.5,80.,1.,1.],[21.,0.,90.,0.,0.]],dtype=np.float32)
    support=pd.DataFrame({
        'history_semesters':[2,5],'student_prior_rows':[8,30],'course_prior_rows':[0,120],
        'new_student':[1,0],'new_course':[1,0]
    })
    views=build_context_views(raw,names,support)
    # E2 must not inherit student warning/conduct/workload/student-history evidence.
    assert views['e2'].shape[1]==3
    assert np.allclose(views['e2'][:,0],[1.,0.])  # course_history_missing_flag
    assert np.allclose(views['e2'][:,1],[0.,120.]) # course_prior_rows
    assert np.allclose(views['e2'][:,2],[1.,0.])  # new_course
    # E1 contains the student-centric evidence and excludes course-only support.
    assert views['e1'].shape[1] >= 6
    assert not np.array_equal(views['e1'],views['e2'])


def test_model_accepts_separate_specialist_contexts_and_course_has_no_faculty_embedding():
    from suite12_models import CollaborativeEvidenceMoE
    b=5
    m=CollaborativeEvidenceMoE(
        flat_dim=8,e0_context_dim=4,fusion_context_dim=5,e1_context_dim=5,e2_context_dim=3,e3_context_dim=4,e4_context_dim=3,
        student_dim=4,course_dim=5,relation_dim=5,component_numeric_dim=4,
        n_faculties=2,n_component_types=7,d_model=16,e0_dropout=0.,expert_dropout=0.
    )
    out=m(
        flat=torch.randn(b,8),e0_context=torch.randn(b,4),fusion_context=torch.randn(b,5),e1_context=torch.randn(b,5),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),
        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,5),relation_mask=torch.ones(b,3,dtype=torch.bool),relation_type=torch.ones(b,3,dtype=torch.long),
        component_events=torch.randn(b,3,4),component_mask=torch.ones(b,3,dtype=torch.bool),component_type=torch.ones(b,3,dtype=torch.long)
    )
    assert out['final_logit'].shape==(b,)
    assert not hasattr(m.e2,'fac')


def test_fusion_context_is_support_only_not_direct_risk_context():
    from suite12_pipeline import build_context_views
    import pandas as pd
    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]})
    v=build_context_views(raw,names,support)
    assert v['fusion'].shape==(2,5)
    # Changing direct risk/context values must not change the fusion context.
    raw2=raw.copy();raw2[:,0]=1-raw2[:,0];raw2[:,1]+=100;raw2[:,2]+=50
    v2=build_context_views(raw2,names,support)
    assert np.array_equal(v['fusion'],v2['fusion'])


def test_raw_table_source_reads_extracted_database_directory(tmp_path):
    from suite12_structured_cache import RawTableSource
    import pandas as pd
    db=tmp_path/'database';db.mkdir()
    pd.DataFrame({'IDSV':[1,2],'NHHK':[20201,20202],'X':[3,4]}).to_csv(db/'DDTBHK.csv',index=False)
    src=RawTableSource(database_dir=db)
    got=src.read_table('DDTBHK.csv',['IDSV','NHHK'])
    assert got[['IDSV','NHHK']].to_dict('records')==[{'IDSV':1,'NHHK':20201},{'IDSV':2,'NHHK':20202}]
    chunks=list(src.read_chunks('DDTBHK.csv',['IDSV'],chunksize=1))
    assert [len(x) for x in chunks]==[1,1]


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


def test_alignment_audit_matches_canonical_fold_rows_to_extracted_database(tmp_path):
    from suite12_structured_cache import RawTableSource, audit_canonical_alignment
    import pandas as pd, joblib
    db=tmp_path/'database';db.mkdir();cache=tmp_path/'cache';cache.mkdir()
    train=pd.DataFrame({
        'IDKQDK':[101], 'IDSV':[11], 'IDToHoc':[201], 'target_NHHK':[20221], 'IDMH':[301],
        'target_IDCongDiem':[401], 'target_grade10':[4.5], 'prev_semester_NHHK':[20212], 'prev_semester_gpa10':[8.3],
    })
    test=pd.DataFrame({
        'IDKQDK':[102], 'IDSV':[12], 'IDToHoc':[202], 'target_NHHK':[20222], 'IDMH':[302],
        'target_IDCongDiem':[402], 'target_grade10':[7.0], 'prev_semester_NHHK':[20221], 'prev_semester_gpa10':[7.2],
    })
    joblib.dump({'train_frame':train,'test_frame':test},cache/'fac_fold_1.joblib')
    pd.DataFrame({'IDKQDK':[101,102],'IDSV':[11,12],'IDToHoc':[201,202],'NHHK':[20221,20222]}).to_csv(db/'MKQDK.csv',index=False)
    pd.DataFrame({'IDToHoc':[201,202],'IDMH':[301,302],'NHHK':[20221,20222]}).to_csv(db/'KToHoc.csv',index=False)
    pd.DataFrame({'IDCongDiem':[401,402],'IDSV':[11,12],'IDMH':[301,302],'NHHK':[20221,20222],
                  'DiemHP':[4.5,7.0],'DiemHPCalc':[4.5,7.0]}).to_csv(db/'DDTBHP.csv',index=False)
    pd.DataFrame({'IDSV':[11,11,12],'NHHK':[20212,20212,20221],'DTBHK':[7.1,8.3,7.2],
                  'NgayThem':['2022-03-01','2022-10-01','2023-01-01']}).to_csv(db/'DDTBHK.csv',index=False)
    src=RawTableSource(database_dir=db)
    report=audit_canonical_alignment(cache,['fac'],[1],src,chunksize=1)
    assert report['canonical_rows']==2
    assert report['registration_match_rate']==1.0
    assert report['course_mapping_match_rate']==1.0
    assert report['target_grade_match_rate']==1.0
    assert report['prev_gpa_match_rate']==1.0


def test_structured_cache_compatibility_rejects_old_schema(tmp_path):
    from suite12_structured_cache import structured_cache_compatible, SCHEMA_VERSION
    import joblib
    p=tmp_path/'fold.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)
