import importlib.util
import sys
from pathlib import Path
import numpy as np

MODULE_PATH = Path('/mnt/data/train_expert_v3.py')
spec = importlib.util.spec_from_file_location('tev3', MODULE_PATH)
m = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = m
spec.loader.exec_module(m)


def test_temporal_crossfit_indices_strictly_earlier():
    groups = np.array([1,1,2,2,3,3,4,4])
    plans = m.temporal_crossfit_plan(groups, min_train_rows=2)
    assert plans
    for train_idx, val_idx, block in plans:
        assert np.all(groups[train_idx] < block)
        assert np.all(groups[val_idx] == block)
        assert not np.intersect1d(train_idx, val_idx).size


def test_batch_percentile_rank_semantics():
    score = np.array([0.8, 0.1, 0.4, 0.2])
    rank = m.batch_percentile_rank(score)
    assert np.allclose(rank[[1,3,2,0]], [0.25,0.50,0.75,1.00])


def test_asymmetric_bounded_delta_respects_caps():
    raw = np.array([-100.0, -1.0, 0.0, 1.0, 100.0])
    delta = m.asymmetric_bounded_delta_numpy(raw, positive_cap=2.0, negative_cap=0.75)
    assert delta.min() >= -0.75 - 1e-8
    assert delta.max() <= 2.0 + 1e-8
    assert delta[0] < 0 and delta[-1] > 0 and delta[2] == 0


def test_authority_zero_is_committee_identity():
    q = np.array([0.03, 0.2, 0.8])
    delta = np.array([2.0, -0.75, 1.0])
    out = m.apply_residual_correction(q, delta, authority=0.0)
    assert np.allclose(out, q)


def test_total_specialist_loss_contains_penalty():
    bce, penalty, total = m.specialist_loss_components_numpy(
        logits=np.array([0.0, 1.0]),
        y=np.array([0.0, 1.0]),
        weights=np.array([1.0, 1.0]),
        delta=np.array([1.0, -0.5]),
        residual_penalty=0.2,
    )
    assert penalty > 0
    assert np.isclose(total, bce + penalty)


def test_soft_exit_is_not_forced_normal():
    score = np.array([0.9, 0.1])
    easy = np.array([True, False])
    pred = m.apply_decision_rule(score, 0.5, easy_negative=easy, hard_negative_guard=False)
    assert pred.tolist() == [1, 0]


def test_strong_positive_cannot_be_downgraded():
    q = np.array([0.8, 0.4])
    specialist = np.array([0.2, 0.7])
    easy = np.array([False, False])
    strong = np.array([True, False])
    out = m.combine_cascade_scores(q, specialist, easy, strong)
    assert out[0] >= q[0]
    pred = m.apply_decision_rule(out, 0.95, strong_positive=strong)
    assert pred[0] == 1


def test_feature_selector_does_not_use_validation_rows():
    # Training says feature 0 is informative; a contradictory validation set must not affect selection.
    X_train = np.array([[0,0],[0.1,1],[1,0],[1.1,1],[0.2,0],[1.2,1]], float)
    y_train = np.array([0,0,1,1,0,1])
    selector = m.RawFeatureSelector(max_features=1).fit(X_train, y_train, ['train_signal','other'])
    X_val = np.array([[0,10],[1,-10],[0,9],[1,-9]], float)  # deliberately unused
    _ = X_val
    assert selector.selected_feature_names_ == ['train_signal']


def test_specialist_validation_fallback_is_deterministic_and_explicit():
    y = np.array([0,1] * 20)
    blocks = np.array(['a'] * 40)
    a = m.choose_specialist_validation(y, blocks, 'stratified_oof', seed=7)
    b = m.choose_specialist_validation(y, blocks, 'stratified_oof', seed=7)
    assert a[2] == 'stratified_fallback'
    assert np.array_equal(a[0], b[0]) and np.array_equal(a[1], b[1])


def test_crossfit_temporal_fallback_is_explicit():
    rng = np.random.default_rng(11)
    X = rng.normal(size=(60, 5))
    y = np.array([0,1] * 30)
    groups = np.full(60, np.nan)
    cf = m.generate_crossfit_predictions(
        X, y, groups, 'unavailable', seed=3, n_estimators=2, hist_max_iter=5,
        crossfit_folds=3, min_train_rows=10, min_oof_coverage=.4, verbose=False,
    )
    assert cf.mode == 'stratified_oof'
    assert 'falling back' in cf.warning.lower()
    assert cf.covered_mask.all()
