import copy
import io
import unittest

import numpy as np
import torch

from mint_seed import fit_seed_forest, seed_predict
from mint_model import MemoryIntegratedForest


def fixture():
    rng = np.random.default_rng(4)
    x = rng.normal(size=(160, 5)).astype(np.float32)
    y = (x[:, 0] + .5 * x[:, 1] > 0).astype(int)
    blueprint = fit_seed_forest(x, y, n_trees=3, depth=3, min_leaf=3, seed=4, n_jobs=1)
    torch.manual_seed(11)
    b, h = 7, 6
    batch = dict(x=torch.tensor(x[:b]), course_meta=torch.randn(b, 4),
                 course_id=torch.randint(1, 7, (b,)), y=torch.tensor(y[:b], dtype=torch.float32),
                 faculty=torch.zeros(b, dtype=torch.long), h_x=torch.randn(b, h, 5),
                 h_meta=torch.randn(b, h, 4), h_course_id=torch.randint(1, 7, (b, h)),
                 h_y=torch.randint(0, 2, (b, h)).float(), h_grade=torch.rand(b, h),
                 h_grade_present=torch.ones(b, h), h_mask=torch.ones(b, h, dtype=torch.bool),
                 h_recency=torch.randint(1, 6, (b, h)).float(), h_same=torch.zeros(b, h))
    model = MemoryIntegratedForest(blueprint, 4, 7, hidden=12, ability_dim=5, dropout=0)
    return blueprint, batch, model


class ModelTests(unittest.TestCase):
    def setUp(self):
        torch.set_num_threads(1)

    def test_seed_leaves_use_natural_counts_with_balanced_split_discovery(self):
        x = np.zeros((20, 3), dtype=np.float32)
        y = np.array([1, 1] + [0] * 18)
        seed = fit_seed_forest(x, y, n_trees=2, depth=2, min_leaf=2, n_jobs=1)
        np.testing.assert_allclose(seed_predict(seed, x), (2 + .5) / (20 + 1), atol=1e-7)
        from sklearn.ensemble import ExtraTreesClassifier
        rng = np.random.default_rng(8)
        x = rng.normal(size=(300, 4)).astype(np.float32)
        y = (x[:, 0] + .4 * x[:, 1] > 1.4).astype(int)
        reference = ExtraTreesClassifier(n_estimators=4, max_depth=3, min_samples_leaf=3,
                                          class_weight='balanced', random_state=9, n_jobs=1).fit(x, y)
        expected = []
        for tree in reference.estimators_:
            leaves = tree.apply(x)
            counts = np.bincount(leaves, minlength=tree.tree_.node_count)
            positives = np.bincount(leaves, weights=y, minlength=tree.tree_.node_count)
            expected.append(((positives + .5) / (counts + 1))[leaves])
        seed = fit_seed_forest(x, y, n_trees=4, depth=3, min_leaf=3, seed=9, n_jobs=1)
        np.testing.assert_allclose(seed_predict(seed, x), np.mean(expected, axis=0), atol=2e-7)

    def test_hard_conversion_reproduces_seed_and_path_mass(self):
        blueprint, batch, model = fixture()
        out = model(batch, intervention={'hard_routing', 'no_context'}, return_details=True)
        np.testing.assert_allclose(out['prob'].detach().numpy(), seed_predict(blueprint, batch['x'].numpy()), atol=2e-7)
        out = model(batch, return_details=True)
        torch.testing.assert_close(out['leaf_mass'].sum(-1), torch.ones(7, 3))
        torch.testing.assert_close(out['attention'].sum(-1), torch.ones(7))
        self.assertTrue(torch.all((out['prob'] > 0) & (out['prob'] < 1)))

    def test_adjacent_float32_split_boundaries_keep_sklearn_routes(self):
        x = np.tile([np.float32(1000), np.nextafter(np.float32(1000), np.float32(np.inf))], 20).reshape(-1, 1)
        y = np.tile([0, 1], 20)
        # All valid thresholds lie between two adjacent float32 values. Rounding
        # to the larger value would wrongly send both classes left.
        blueprint = fit_seed_forest(x, y, n_trees=20, depth=1, min_leaf=1, seed=4, n_jobs=1)
        np.testing.assert_allclose(seed_predict(blueprint, x)[y == 0], .5 / 21, atol=2e-7)
        np.testing.assert_allclose(seed_predict(blueprint, x)[y == 1], 20.5 / 21, atol=2e-7)

    def test_partial_padding_and_history_permutation_invariance(self):
        _, batch, model = fixture()
        batch['h_mask'][:, -2:] = False
        baseline = model(batch)
        changed = copy.deepcopy(batch)
        changed['h_x'][:, -2:] += 100
        changed['h_meta'][:, -2:] -= 100
        changed['h_y'][:, -2:] = 1 - changed['h_y'][:, -2:]
        changed['h_grade'][:, -2:] = 1 - changed['h_grade'][:, -2:]
        torch.testing.assert_close(baseline['prob'], model(changed)['prob'])
        order = torch.tensor([3, 1, 5, 0, 4, 2])
        for key in batch:
            if key.startswith('h_'):
                batch[key] = batch[key][:, order]
        torch.testing.assert_close(baseline['prob'], model(batch)['prob'])

    def test_final_loss_reaches_tree_and_memory(self):
        _, batch, model = fixture()
        out = model(batch)
        torch.nn.functional.binary_cross_entropy(out['prob'], batch['y']).backward()
        for name in ('selector_logits', 'thresholds', 'leaf_logits', 'memory_value.weight', 'memory_key.weight',
                     'node_memory_weight', 'skill_assignment.weight', 'course_requirement.weight',
                     'course_importance.weight', 'node_gap_weight'):
            grad = dict(model.named_parameters())[name].grad
            self.assertIsNotNone(grad, name)
            self.assertTrue(torch.isfinite(grad).all(), name)
            self.assertGreater(float(grad.norm()), 1e-10, name)

    def test_history_changes_internal_routes_and_outcome_mask(self):
        _, batch, model = fixture()
        before = model(batch, return_details=True)
        changed = copy.deepcopy(batch)
        changed['h_y'] = 1 - changed['h_y']
        changed['h_grade'] = 1 - changed['h_grade']
        after = model(changed, return_details=True)
        self.assertGreater(float((before['routes'] - after['routes']).detach().abs().max()), 1e-6)
        masked_before = model(batch, 'no_outcomes', True)
        masked_after = model(changed, 'no_outcomes', True)
        torch.testing.assert_close(masked_before['routes'], masked_after['routes'])

    def test_readiness_is_target_independent_and_grade_monotone(self):
        _, batch, model = fixture()
        low = copy.deepcopy(batch)
        high = copy.deepcopy(batch)
        low['h_grade'].fill_(.1)
        high['h_grade'].fill_(.9)
        a, b = model(low), model(high)
        self.assertTrue(torch.all(b['readiness'] > a['readiness']))
        high['x'] += 2
        high['course_id'] = (high['course_id'] % 6) + 1
        high['course_meta'] -= 3
        torch.testing.assert_close(b['readiness'], model(high)['readiness'])

    def test_empty_and_padded_history_is_ignored(self):
        _, batch, model = fixture()
        batch['h_mask'][:] = False
        out = model(batch)
        for v in out.values():
            self.assertTrue(torch.isfinite(v).all())
        for name in ('attention', 'memory_shift_abs', 'gap_shift_abs'):
            self.assertEqual(float(out[name].detach().abs().sum()), 0)
        changed = copy.deepcopy(batch)
        changed['h_x'] += 100
        changed['h_meta'] -= 100
        changed['h_y'] = 1 - changed['h_y']
        changed['h_grade'] = 1 - changed['h_grade']
        torch.testing.assert_close(out['prob'], model(changed)['prob'])

    def test_current_labels_unused_and_state_roundtrip(self):
        blueprint, batch, model = fixture()
        out = model(batch)['prob']
        batch['y'] = 1 - batch['y']
        torch.testing.assert_close(out, model(batch)['prob'])
        stream = io.BytesIO()
        torch.save({'blueprint': blueprint, 'config': model.config(), 'state': model.state_dict()}, stream)
        stream.seek(0)
        state = torch.load(stream, weights_only=False)
        restored = MemoryIntegratedForest(state['blueprint'], **state['config'])
        restored.load_state_dict(state['state'])
        torch.testing.assert_close(out, restored(batch)['prob'], rtol=0, atol=0)


if __name__ == '__main__':
    unittest.main()
