"""Executable, real-cache fixtures for the chronological data contract."""
from __future__ import annotations

import tempfile
import unittest
from dataclasses import replace
from pathlib import Path

import joblib
import numpy as np
import pandas as pd
import torch

try:
    import mint_data as md
except ModuleNotFoundError as exc:
    if exc.name != "mint_data":
        raise
    md = None


def make_table(semesters, students=None, courses=None, faculties=None,
               x=None, grades=None, labels=None, names=("A", "B")):
    n = len(semesters)
    grades = np.full(n, np.nan) if grades is None else np.asarray(grades, float)
    labels = np.zeros(n, int) if labels is None else np.asarray(labels)
    return md.Table(
        x=np.arange(n * 2, dtype=float).reshape(n, 2) if x is None else np.asarray(x),
        course_meta=np.full((n, 17), np.nan), y=labels, grade=grades,
        semester=np.asarray(semesters),
        student=np.asarray(students if students is not None else ["s"] * n),
        course=np.asarray(courses if courses is not None else ["c"] * n),
        faculty=np.asarray(faculties if faculties is not None else [0] * n),
        sample_id=np.asarray([f"row-{i}" for i in range(n)]),
        committee=np.full(n, np.nan), faculty_names=names,
        feature_names=("raw_a", "raw_b"),
    )


def write_canonical_fixture(root: Path, faculty="A", fold=1):
    """Six canonical events, two structured-covered rows, one future target."""
    cache = root / "canonical"
    structured = root / "structured"
    cache.mkdir(exist_ok=True)
    structured.mkdir(exist_ok=True)
    train = pd.DataFrame({
        "IDSV": [1, 2, 1, 2, 1, 2],
        "IDMH": ["c", "c", "c", "c", "c", "d"],
        "target_NHHK": [20231, 20231, 20232, 20232, 20233, 20233],
        "target_grade10": [2., 8., 4., 6., 9., np.nan],
        "SoTinChi": [2., np.nan, np.nan, 3., np.nan, 4.],
    })
    target = pd.DataFrame({
        "IDSV": [1.0], "IDMH": ["new-course"], "target_NHHK": [20241],
        "target_grade10": [7.], "SoTinChi": [99.],
    })
    data = {
        "feature_names": ["raw_a", "raw_b"],
        "Xtr": np.arange(12, dtype=np.float32).reshape(6, 2),
        "Xte": np.array([[30., 40.]], dtype=np.float32),
        "ytr": np.array([1, 0, 1, 0, 0, 1]), "yte": np.array([0]),
        "train_frame": train, "test_frame": target,
        "cf_covered_mask": np.array([False, True, False, False, True, False]),
        "cf_probabilities": np.array([[np.nan, np.nan], [.2, .4],
                                      [np.nan, np.nan], [np.nan, np.nan],
                                      [.1, .3], [np.nan, np.nan]]),
        "qte": np.array([.25]),
    }
    # Deliberately reversed: the loader must honor indices, not array position.
    covered_meta = np.array([[40.] * 17, [10.] * 17], dtype=np.float32)
    sc = {"train_row_index": np.array([4, 1]),
          "train": {"course_target": covered_meta},
          "target": {"course_target": np.array([[70.] * 17], dtype=np.float32)}}
    cp = cache / f"{faculty}_fold_{fold}.joblib"
    sp = structured / f"{faculty}_fold_{fold}.joblib"
    joblib.dump(data, cp)
    joblib.dump(sc, sp)
    return cache, structured, cp, sp


class DataContractTests(unittest.TestCase):
    def setUp(self):
        self.assertIsNotNone(md, "mint_data chronological data module is not implemented")

    def test_full_bank_preserves_structured_mapping_and_asof_reconstruction(self):
        with tempfile.TemporaryDirectory() as tmp:
            cache, structured, _, _ = write_canonical_fixture(Path(tmp))
            history, target, audit = md.load_fold(cache, structured, ["A"], 1)
            self.assertEqual(len(history), 6)
            np.testing.assert_array_equal(history.course_meta[4], [40.] * 17)
            np.testing.assert_array_equal(history.course_meta[1], [10.] * 17)
            np.testing.assert_array_equal(target.course_meta[0], [70.] * 17)
            np.testing.assert_array_equal(history.x[:, :2], np.arange(12).reshape(6, 2))
            np.testing.assert_array_equal(history.x[:, 2:], history.course_meta)
            # Row 2 sees grades 2 and 8, never its own 4 or simultaneous grade 6.
            np.testing.assert_allclose(history.course_meta[2, 12:],
                                       [2., 5., np.sqrt(18.), .5, .5])
            self.assertTrue(np.isnan(history.course_meta[2, 0]))
            self.assertEqual(history.course_meta[3, 0], 3.)
            self.assertEqual(history.course_meta[0, 12], 0.)
            self.assertTrue(np.isnan(history.course_meta[0, 13:]).all())
            self.assertTrue(np.isnan(target.grade).all())
            self.assertEqual(history.student[0], target.student[0])
            self.assertEqual(audit["history_rows"], 6)
            self.assertEqual(audit["structured_covered_rows"], 2)
            self.assertEqual(audit["reconstructed_history_rows"], 4)
            self.assertEqual(len(set(history.sample_id)), 6)

    def test_reconstruction_does_not_read_future_or_same_semester_outcomes(self):
        with tempfile.TemporaryDirectory() as tmp:
            cache, structured, cp, _ = write_canonical_fixture(Path(tmp))
            first, _, _ = md.load_fold(cache, structured, ["A"], 1)
            raw = joblib.load(cp)
            raw["train_frame"].loc[3, "target_grade10"] = 0.
            raw["ytr"][3] = 1
            raw["test_frame"].loc[0, "target_grade10"] = 1.
            raw["test_frame"].loc[0, "SoTinChi"] = 100000.
            raw["yte"][0] = 1
            joblib.dump(raw, cp)
            second, _, _ = md.load_fold(cache, structured, ["A"], 1)
            np.testing.assert_array_equal(first.course_meta[2], second.course_meta[2])
            np.testing.assert_array_equal(first.sample_id, second.sample_id)

    def test_loader_rejects_bad_structured_indices_labels_grades_and_ids(self):
        cases = [
            ("indices", "train_row_index"), ("label", "binary"),
            ("grade_range", "grade"), ("grade_label", "consisten"),
            ("student", "student|IDSV"), ("semester", "semester|target_NHHK"),
        ]
        for case, message in cases:
            with self.subTest(case=case), tempfile.TemporaryDirectory() as tmp:
                cache, structured, cp, sp = write_canonical_fixture(Path(tmp))
                raw, sc = joblib.load(cp), joblib.load(sp)
                if case == "indices":
                    sc["train_row_index"] = np.array([4, 4])
                elif case == "label":
                    raw["ytr"][0] = 2
                elif case == "grade_range":
                    raw["train_frame"].loc[0, "target_grade10"] = 11.
                elif case == "grade_label":
                    raw["train_frame"].loc[0, "target_grade10"] = 5.
                elif case == "student":
                    raw["train_frame"].loc[0, "IDSV"] = np.nan
                else:
                    raw["train_frame"].loc[0, "target_NHHK"] = 20234
                joblib.dump(raw, cp)
                joblib.dump(sc, sp)
                with self.assertRaisesRegex(ValueError, message):
                    md.load_fold(cache, structured, ["A"], 1)

    def test_common_features_use_intersection_and_first_faculty_order(self):
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            cache, structured, _, _ = write_canonical_fixture(root)
            _, _, bp, _ = write_canonical_fixture(root, faculty="B")
            data = joblib.load(bp)
            data["feature_names"] = ["raw_b", "extra"]
            joblib.dump(data, bp)
            self.assertEqual(md.discover_faculties(cache), ["A", "B"])
            self.assertEqual(md.common_feature_names(cache, ["A", "B"]), ["raw_b"])
            hist, _, _ = md.load_fold(cache, structured, ["A", "B"], 1)
            self.assertEqual(hist.x.shape, (12, 18))
            self.assertEqual(hist.feature_names[0], "raw_b")
            self.assertEqual(len(set(hist.sample_id)), 12)

    def test_global_split_excludes_late_faculty_rows_from_early_validation(self):
        table = make_table([1, 2, 3, 4, 4, 5, 6, 7], faculties=[0]*4 + [1]*4)
        train, valid, audit = md.chronological_split(table, val_semesters=3)
        np.testing.assert_array_equal(train, [0, 1, 2, 3, 4])
        np.testing.assert_array_equal(valid, [5, 6, 7])
        self.assertLess(table.semester[train].max(), table.semester[valid].min())
        self.assertEqual(audit["validation_semesters"], [5, 6, 7])
        tiny = table.subset([0, 1])
        small_train, small_valid, _ = md.chronological_split(tiny, 3)
        np.testing.assert_array_equal(small_train, [0])
        np.testing.assert_array_equal(small_valid, [1])
        with self.assertRaisesRegex(ValueError, "semester"):
            md.chronological_split(table.subset([0]))

    def test_assert_future_uses_global_faculty_ranges(self):
        bank = make_table([1, 4], faculties=[0, 1])
        future = make_table([3, 5], faculties=[0, 1])
        with self.assertRaisesRegex(ValueError, "history.*4.*target.*3"):
            md.assert_future(bank, future)
        md.assert_future(bank, replace(future, semester=np.array([5, 6])))

    def test_train_only_preprocessing_unknown_course_missingness_and_roundtrip(self):
        table = make_table([1, 2, 3], courses=["b", "a", "unseen"],
                           x=[[1., np.nan], [3., np.inf], [1000., 5.]],
                           faculties=[0, 1, 0])
        table.course_meta[0, 0] = 2.
        table.course_meta[1, 0] = 4.
        table.course_meta[2, 0] = 999.
        pre = md.Preprocessor.fit(table, [0, 1])
        prepared = pre.transform(table)
        np.testing.assert_array_equal(prepared.x[:2, :2], [[-1., 0.], [1., 0.]])
        np.testing.assert_array_equal(prepared.x[:, -2:], [[1., 0.], [0., 1.], [1., 0.]])
        self.assertEqual(prepared.x[2, 0], 998.)
        self.assertEqual(prepared.course_id[2], 0)
        self.assertEqual(pre.n_courses, 3)
        self.assertEqual(prepared.course_meta.shape, (3, 34))
        np.testing.assert_array_equal(prepared.course_meta[:, 17], [1., 1., 1.])
        self.assertTrue(np.isfinite(prepared.x).all())
        self.assertTrue(np.isfinite(prepared.course_meta).all())
        restored = md.Preprocessor.from_dict(pre.to_dict()).transform(table)
        np.testing.assert_array_equal(restored.x, prepared.x)
        np.testing.assert_array_equal(restored.course_id, prepared.course_id)
        np.testing.assert_array_equal(restored.course_meta, prepared.course_meta)

    def test_subset_accepts_integer_order_and_boolean_mask(self):
        table = make_table([1, 2, 3])
        np.testing.assert_array_equal(table.subset([2, 0]).semester, [3, 1])
        np.testing.assert_array_equal(table.subset([False, True, False]).semester, [2])

    def test_numeric_string_ids_match_numeric_ids_without_erasing_leading_zeros(self):
        bank = make_table([1, 1], students=["17.0", "001"], courses=["101.00", "009"])
        query = make_table([2, 2], students=[17, "001"], courses=[101, "009"])
        history = md.build_history(bank, query, 1)
        np.testing.assert_array_equal(history.indices[:, 0], [0, 1])
        self.assertEqual(bank.student[1], "001")
        self.assertEqual(bank.course[1], "009")
        pre = md.Preprocessor.fit(bank, [0, 1])
        self.assertTrue((pre.transform(query).course_id > 0).all())

    def test_history_is_strict_recent_allowed_and_outcome_independent(self):
        bank = make_table([1, 2, 3, 3, 4, 1],
                          students=["s", "s", "s", "s", "s", "other"])
        query = make_table([3, 5], students=["s", "s"])
        history = md.build_history(bank, query, max_history=2)
        np.testing.assert_array_equal(history.indices, [[0, 1], [3, 4]])
        np.testing.assert_array_equal(history.full_count, [2, 5])
        np.testing.assert_array_equal(history.retained_count, [2, 2])
        np.testing.assert_array_equal(history.recency, [[2., 1.], [2., 1.]])
        changed = replace(bank, y=np.array([1, 1, 1, 0, 0, 1]))
        other = md.build_history(changed, query, max_history=2)
        np.testing.assert_array_equal(history.indices, other.indices)
        fixed_origin = md.build_history(bank, query, 4,
                                        allowed=np.array([True, True, False, False, False, False]))
        np.testing.assert_array_equal(fixed_origin.indices, [[0, 1, -1, -1], [0, 1, -1, -1]])
        np.testing.assert_array_equal(fixed_origin.full_count, [2, 2])

    def test_history_shuffle_uses_other_students_in_same_faculty_and_semester(self):
        bank = make_table([1, 1, 1, 2], students=["a", "b", "c", "z"],
                          courses=["x", "y", "z", "x"], faculties=[0, 0, 1, 0])
        query = make_table([3, 3, 3, 4], students=["a", "b", "c", "z"],
                           courses=["x", "x", "z", "x"], faculties=[0, 0, 1, 0])
        original = md.build_history(bank, query, 2)
        shuffled, audit = md.shuffle_history(bank, query, original, seed=4)
        np.testing.assert_array_equal(shuffled.indices[:, 0], [1, 0, 2, 3])
        self.assertEqual(shuffled.same_course[0, 0], 0.)
        self.assertEqual(shuffled.same_course[1, 0], 1.)
        self.assertEqual(audit["different_student_rows"], 2)
        self.assertEqual(audit["unshuffleable_rows"], 2)
        for qi, ids in enumerate(shuffled.indices):
            valid = ids >= 0
            self.assertTrue((bank.semester[ids[valid]] < query.semester[qi]).all())

    def test_shuffle_avoids_same_students_even_in_imbalanced_query_groups(self):
        bank = make_table([1, 1], students=["a", "b"])
        query = make_table([3] * 4, students=["a", "a", "a", "b"])
        history = md.build_history(bank, query, 1)
        shuffled, audit = md.shuffle_history(bank, query, history, seed=8)
        np.testing.assert_array_equal(shuffled.indices[:, 0], [1, 1, 1, 0])
        self.assertEqual(audit["different_student_rows"], 4)

    def test_dataset_missing_grades_and_padding_are_finite_and_masked(self):
        bank = make_table([1, 1], students=["a", "b"], labels=[1, 0], grades=[np.nan, 8.])
        query = make_table([2, 2], students=["a", "cold"])
        pre = md.Preprocessor.fit(bank, [0, 1])
        hist = md.build_history(bank, query, 3)
        dataset = md.MemoryDataset(pre.transform(query), pre.transform(bank), hist)
        item, cold = dataset[0], dataset[1]
        self.assertEqual(item["h_y"].tolist(), [1., 0., 0.])
        self.assertEqual(item["h_grade_present"].tolist(), [0., 0., 0.])
        self.assertEqual(item["h_mask"].tolist(), [True, False, False])
        self.assertEqual(cold["h_mask"].tolist(), [False, False, False])
        for row in (item, cold):
            self.assertTrue(all(torch.isfinite(value).all() for value in row.values()))
            for key in ("h_x", "h_meta", "h_course_id", "h_y", "h_grade"):
                self.assertTrue((row[key][~row["h_mask"]] == 0).all(), key)
        empty = bank.subset([])
        empty_hist = md.build_history(empty, query, 3)
        empty_item = md.MemoryDataset(pre.transform(query), pre.transform(empty), empty_hist)[0]
        self.assertFalse(empty_item["h_mask"].any())
        self.assertTrue(torch.isfinite(empty_item["h_x"]).all())

    def test_all_missing_historical_grade_column_is_supported(self):
        with tempfile.TemporaryDirectory() as tmp:
            cache, structured, cp, _ = write_canonical_fixture(Path(tmp))
            raw = joblib.load(cp)
            del raw["train_frame"]["target_grade10"]
            joblib.dump(raw, cp)
            history, _, _ = md.load_fold(cache, structured, ["A"], 1)
            self.assertTrue(np.isnan(history.grade).all())
            self.assertEqual(history.course_meta[2, 12], 2.)
            self.assertTrue(np.isnan(history.course_meta[2, 13:15]).all())
            np.testing.assert_array_equal(history.course_meta[2, 15:], [.5, .5])


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