import sys
from pathlib import Path
from types import SimpleNamespace
import numpy as np
import pandas as pd
import pytest

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


def test_common_feature_intersection_preserves_first_cache_order():
    from cross_faculty_common import common_raw_features
    caches={
        'a':{'feature_names':['f3','f1','f2','only_a']},
        'b':{'feature_names':['f2','f3','f1','only_b']},
        'c':{'feature_names':['f1','f3','f2']},
    }
    assert common_raw_features(caches,['a','b','c']) == ['f3','f1','f2']


def test_align_raw_features_uses_feature_names_not_column_positions():
    from cross_faculty_common import align_raw_features
    X=np.array([[10.,20.,30.],[40.,50.,60.]])
    out=align_raw_features(X,['b','c','a'],['a','b'])
    np.testing.assert_array_equal(out,np.array([[30.,10.],[60.,40.]]))


def test_faculty_class_sampling_weights_equalize_faculty_mass_and_upweight_rare_positive():
    from cross_faculty_common import faculty_class_sampling_weights
    y=np.array([0]*90+[1]*10 + [0]*18+[1]*2)
    faculty=np.array([0]*100+[1]*20)
    w,diag=faculty_class_sampling_weights(y,faculty,['large','small'],beta=.99)
    assert np.isfinite(w).all() and np.all(w>0)
    # Each faculty gets the same total expected sampling mass.
    assert np.isclose(w[faculty==0].sum(),w[faculty==1].sum(),rtol=1e-6)
    # Within each faculty, the rarer positive class receives more per-row mass.
    assert w[(faculty==0)&(y==1)].mean() > w[(faculty==0)&(y==0)].mean()
    assert w[(faculty==1)&(y==1)].mean() > w[(faculty==1)&(y==0)].mean()
    assert set(diag)=={'large','small'}


def test_latest_block_split_is_per_faculty_and_chronological():
    from cross_faculty_common import latest_block_split
    faculty=np.array([0]*6+[1]*6)
    blocks=np.array([1,1,2,2,3,3, 5,5,6,6,7,7],float)
    tr,va=latest_block_split(faculty,blocks,val_blocks=1)
    assert set(np.where(va)[0]) == {4,5,10,11}
    assert not np.any(tr & va)
    assert tr.sum()==8


def test_macro_delta_summary_uses_faculty_level_not_row_level():
    from cross_faculty_common import macro_delta_summary
    rows=pd.DataFrame({
        'faculty':['big','small'],
        'committee_ap':[.50,.20],
        'model_ap':[.51,.25],
        'committee_f2_20':[.40,.20],
        'model_f2_20':[.40,.24],
    })
    s=macro_delta_summary(rows)
    assert np.isclose(s['median_delta_ap'],(.01+.05)/2)
    assert np.isclose(s['win_rate_ap'],1.0)
    assert np.isclose(s['median_delta_f2_20'],(.00+.04)/2)


def test_zero_initialized_residual_moe_is_exact_committee_and_bounded():
    torch=pytest.importorskip('torch')
    from cross_faculty_residual_moe import ResidualMoE
    torch.manual_seed(3)
    model=ResidualMoE(input_dim=6,n_faculties=3,n_experts=4,faculty_dim=3,encoder_hidden=12,representation_dim=8,expert_hidden=6,delta_max=2.0,dropout=0.0)
    x=torch.randn(9,6)
    f=torch.tensor([0,1,2,0,1,2,0,1,2])
    q=torch.linspace(.05,.95,9)
    out=model(x,f,q)
    assert out['weights'].shape==(9,5)
    torch.testing.assert_close(out['weights'].sum(1),torch.ones(9))
    torch.testing.assert_close(out['residuals'],torch.zeros_like(out['residuals']))
    torch.testing.assert_close(out['final_prob'],q,rtol=1e-5,atol=1e-6)
    assert torch.max(torch.abs(out['residuals'])) <= 2.0+1e-6


def test_capability_target_prefers_expert_that_reduces_committee_loss():
    torch=pytest.importorskip('torch')
    from cross_faculty_residual_moe import capability_targets
    y=torch.tensor([1.,0.])
    q=torch.tensor([.2,.8])
    # Expert0 corrects both cases; expert1 makes both worse.
    cf=torch.tensor([[.8,.1],[.2,.9]])
    target,utility=capability_targets(y,q,cf,temperature=.25)
    assert torch.all(utility[:,0] > utility[:,1])
    assert torch.all(target[:,1] > target[:,2])  # col0 is NULL, col1 expert0


def test_residual_moe_loss_is_finite_and_backpropagates():
    torch=pytest.importorskip('torch')
    from cross_faculty_residual_moe import ResidualMoE,residual_moe_loss
    model=ResidualMoE(input_dim=4,n_faculties=2,n_experts=3,faculty_dim=2,encoder_hidden=8,representation_dim=6,expert_hidden=5,dropout=0.0)
    x=torch.randn(16,4); f=torch.tensor([0,1]*8); q=torch.full((16,),.25); y=torch.tensor([0,1]*8,dtype=torch.float32)
    out=model(x,f,q)
    losses=residual_moe_loss(out,y,q,router_lambda=.5,specialization_lambda=.01,magnitude_lambda=.01,utility_temperature=.25)
    losses['total'].backward()
    assert torch.isfinite(losses['total'])
    assert any(p.grad is not None for p in model.parameters())


def test_reference_log_parser_handles_v2_and_council_pooled_holdout(tmp_path):
    from collect_reference_metrics import parse_reference_log
    v2=tmp_path/'v2_khoa_demo.log'
    v2.write_text('''### HOLDOUT (test) [signal=classifier, beta=2]\nPOOLED   1000  10.00% |  0.4000  0.8000 |  0.3000  0.6000  0.4000  0.5000   20.00% |  0.3500  0.5500  0.4300  0.5000   15.00%\n''')
    rows=parse_reference_log(v2,faculty='khoa_demo',default_model='v2')
    assert len(rows)==1 and rows[0]['model']=='v2'
    assert np.isclose(rows[0]['ap'],.4) and np.isclose(rows[0]['fixed_f2'],.5)

    council=tmp_path/'council_khoa_demo.log'
    council.write_text('''### HOLDOUT (test) [model=competence_council, beta=2]\nPOOLED   1000  10.00% |  0.4500  0.8200 |  0.3200  0.6200  0.4200  0.5200   20.00% |  0.3700  0.5700  0.4500  0.5300   15.00%\n''')
    rows=parse_reference_log(council,faculty='khoa_demo')
    assert rows[0]['model']=='competence_council'
    assert np.isclose(rows[0]['drift_f2'],.53)


def test_drift_rate_uses_previous_fold_prevalence():
    from cross_faculty_eval import drift_rate_for_fold
    prevalences={5:.05,6:.10,7:.15}
    assert np.isclose(drift_rate_for_fold(6,2.0,prevalences,.03),.10)
    assert np.isclose(drift_rate_for_fold(7,2.0,prevalences,.03),.20)
