import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
import pandas as pd

PACKAGE = Path(__file__).resolve().parents[1]


class SyntheticEndToEndTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.tmp = tempfile.TemporaryDirectory()
        cls.root = Path(cls.tmp.name)
        cls.command = [sys.executable, "-u", str(PACKAGE/"train_suite34.py"),
                       "--synthetic", "--root", str(cls.root), "--epochs", "1",
                       "--min-epochs", "1", "--patience", "1", "--seeds", "29",
                       "--backbone-width", "16", "--residual-blocks", "1", "--view-dim", "8",
                       "--interaction-rank", "4", "--threads", "1", "--hgb-iterations", "3",
                       "--bootstrap", "10", "--batch-size", "64", "--include-a3"]
        cls.log = cls.root/"integration.log"
        with cls.log.open("w") as output:
            cls.result = subprocess.run(cls.command, cwd=PACKAGE, stdout=output,
                                        stderr=subprocess.STDOUT, timeout=180)
        cls.output = cls.root/"results/suite34_ipmvcl_synthetic"

    @classmethod
    def tearDownClass(cls):
        cls.tmp.cleanup()

    def test_full_pipeline_all_folds_controls_and_complete_log(self):
        self.assertEqual(self.result.returncode, 0, self.log.read_text()[-6000:])
        m = pd.read_csv(self.output/"all_metrics.csv")
        self.assertEqual(len(m[m.arm.isin(["A0", "A0M", "A1", "A2", "P1"])]), 2*7*5)
        self.assertEqual(set(m.fold), set(range(1, 8)))
        self.assertEqual(len(m[m.arm == "A3"]), 14)
        self.assertFalse(m.duplicated(["faculty", "fold", "seed", "arm"]).any())
        pred = pd.read_csv(self.output/"all_predictions.csv")
        self.assertEqual(len(pred), len(m)*48)
        self.assertTrue((pred.groupby(["faculty", "fold", "seed", "arm"]).size() == 48).all())
        text = self.log.read_text()
        required = ["RUN_CONFIGURATION", "ENVIRONMENT", "DEPENDENCIES", "DATA_DISCOVERY",
                    "FEATURE_SCHEMA", "FEATURE_VIEW_MANIFEST", "PREPROCESSING_AUDIT",
                    "CHRONOLOGY_AUDIT", "CACHE_ALIGNMENT", "MODEL_PARAMETER_COUNTS",
                    "TRAINING_START", "EPOCH_METRICS", "EARLY_STOPPING", "REFIT",
                    "FACULTY_FOLD_RESULTS", "DEVELOPMENT_MACRO_RESULTS",
                    "DEVELOPMENT_FOLD_MATRIX", "DEVELOPMENT_FACULTY_MATRIX",
                    "DEVELOPMENT_PAIRED_CONTRASTS", "DEVELOPMENT_DECISION",
                    "RETROSPECTIVE_RESULTS", "RETROSPECTIVE_FOLD_MATRIX",
                    "RETROSPECTIVE_FACULTY_MATRIX", "HISTORICAL_COMPARISONS",
                    "OPERATIONAL_THRESHOLDS", "ALERT_BUDGET_RESULTS", "INTERACTION_ABLATIONS",
                    "VIEW_DIAGNOSTICS", "ERROR_RESCUES_AND_HARMS", "HISTORY_AVAILABILITY",
                    "TEMPORAL_STABILITY", "SEED_VARIABILITY", "UNCERTAINTY",
                    "RESEARCH_CONCLUSIONS", "RUN_STATUS"]
        for section in required:
            self.assertIn("["+section+"]", text)
        decision = json.loads((self.output/"development_selection.json").read_text())
        self.assertEqual(decision["research_promotion"], "INCONCLUSIVE")
        self.assertFalse(decision["promoted_for_deployment"])
        late = list((self.output/"cells").glob("*__f6__*/complete.json"))
        self.assertTrue(late)
        self.assertLessEqual((self.output/"development_selection.json").stat().st_mtime,
                             min(p.stat().st_mtime for p in late))

    def test_resume_reuses_cells_and_reconstructs_full_report(self):
        self.assertEqual(self.result.returncode, 0, self.log.read_text()[-6000:])
        before = pd.read_csv(self.output/"all_metrics.csv")
        with (self.root/"resume.log").open("w") as log:
            r = subprocess.run(self.command+["--resume"], cwd=PACKAGE, stdout=log,
                               stderr=subprocess.STDOUT, timeout=180)
        self.assertEqual(r.returncode, 0, (self.root/"resume.log").read_text()[-4000:])
        after = pd.read_csv(self.output/"all_metrics.csv")
        pd.testing.assert_frame_equal(before, after)
        self.assertIn("[RESUME_CELL]", (self.root/"resume.log").read_text())
        self.assertIn("[EPOCH_METRICS]", (self.root/"resume.log").read_text())
        resumed_text = (self.root/"resume.log").read_text()
        for section in ["PREPROCESSING_AUDIT", "TRAINING_START", "EARLY_STOPPING", "REFIT"]:
            self.assertTrue("["+section+"]" in resumed_text, "Missing resumed section "+section)
        self.assertTrue('"fallback"' in resumed_text, "Missing historical-fallback audit")

    def test_changed_configuration_rejects_resume(self):
        self.assertEqual(self.result.returncode, 0, self.log.read_text()[-6000:])
        with (self.root/"changed.log").open("w") as log:
            r = subprocess.run(self.command+["--resume", "--learning-rate", ".009"],
                               cwd=PACKAGE, stdout=log, stderr=subprocess.STDOUT, timeout=30)
        self.assertNotEqual(r.returncode, 0)
        self.assertIn("signature", (self.root/"changed.log").read_text().lower())

    def test_checkpoint_cli_replays_exact_target_list(self):
        import numpy as np
        self.assertEqual(self.result.returncode, 0, self.log.read_text()[-6000:])
        faculty = "synthetic_faculty_0"
        checkpoint = self.output/f"cells/{faculty}__f1__A2__s29/checkpoint.pt"
        output = self.root/"replayed.csv"
        r = subprocess.run([sys.executable, str(PACKAGE/"s34_checkpoint.py"),
            "--checkpoint", str(checkpoint),
            "--canonical", str(self.root/f"results/cross_faculty_residual_moe/cache/{faculty}_fold_1.joblib"),
            "--structured", str(self.root/f"results/suite12_structured_cache/{faculty}_fold_1.joblib"),
            "--faculty", faculty, "--fold", "1", "--output", str(output)],
            cwd=PACKAGE, capture_output=True, text=True, timeout=30)
        self.assertEqual(r.returncode, 0, r.stdout+r.stderr)
        expected = pd.read_csv(checkpoint.parent/"predictions.csv")
        actual = pd.read_csv(output)
        np.testing.assert_allclose(expected.p, actual.p, atol=1e-7)
        self.assertEqual(expected.row_index.tolist(), actual.row_index.tolist())

    def test_launcher_preserves_failure_exit_and_log(self):
        with tempfile.TemporaryDirectory() as td:
            root = Path(td)
            env = dict(os.environ, PYTHON=sys.executable, S34_SKIP_TESTS="1")
            r = subprocess.run(["bash", str(PACKAGE/"run_suite34.sh"), "--root", str(root),
                                "--cache-dir", str(root/"absent"), "--preflight-only"],
                               cwd=PACKAGE, env=env, capture_output=True, text=True, timeout=30)
            self.assertNotEqual(r.returncode, 0)
            logfile = root/"log/suite34_ipmvcl.log"
            self.assertTrue(logfile.exists())
            self.assertIn("[RUN_STATUS]", logfile.read_text())

    def test_tampered_completed_prediction_is_rejected(self):
        self.assertEqual(self.result.returncode, 0, self.log.read_text()[-6000:])
        path = next((self.output/"cells").glob("*__A2__*/predictions.csv"))
        original = path.read_bytes()
        try:
            path.write_bytes(original+b"\n")
            with (self.root/"tampered.log").open("w") as log:
                r = subprocess.run(self.command+["--resume"], cwd=PACKAGE, stdout=log,
                                   stderr=subprocess.STDOUT, timeout=30)
            self.assertNotEqual(r.returncode, 0)
            self.assertIn("integrity", (self.root/"tampered.log").read_text().lower())
        finally:
            path.write_bytes(original)

    def test_source_change_is_rejected_on_resume(self):
        import joblib
        self.assertEqual(self.result.returncode, 0, self.log.read_text()[-6000:])
        path = next((self.root/"results/cross_faculty_residual_moe/cache").glob("*.joblib"))
        original = path.read_bytes()
        try:
            d = joblib.load(path)
            d["Xtr"][0, 0] += 1
            joblib.dump(d, path, compress=3)
            with (self.root/"source_changed.log").open("w") as log:
                r = subprocess.run(self.command+["--resume"], cwd=PACKAGE, stdout=log,
                                   stderr=subprocess.STDOUT, timeout=30)
            self.assertNotEqual(r.returncode, 0)
            self.assertIn("signature", (self.root/"source_changed.log").read_text().lower())
        finally:
            path.write_bytes(original)

    def test_synthetic_overrides_cannot_mutate_existing_project_cache(self):
        with tempfile.TemporaryDirectory() as td:
            root = Path(td)
            actual = root/"results/cross_faculty_residual_moe/cache"
            actual.mkdir(parents=True)
            sentinel = actual/"real_faculty_fold_1.joblib"
            sentinel.write_bytes(b"DO_NOT_TOUCH")
            before = sorted(p.name for p in actual.iterdir())
            command = [sys.executable, str(PACKAGE/"train_suite34.py"), "--synthetic",
                       "--root", str(root), "--cache-dir", str(root/"custom_empty"),
                       "--preflight-only"]
            r = subprocess.run(command, cwd=PACKAGE, capture_output=True, text=True, timeout=30)
            self.assertNotEqual(r.returncode, 0)
            self.assertEqual(sorted(p.name for p in actual.iterdir()), before)
            self.assertEqual(sentinel.read_bytes(), b"DO_NOT_TOUCH")
            self.assertFalse((root/"SYNTHETIC_DATA.json").exists())

    def test_synthetic_generation_protects_structured_destination(self):
        with tempfile.TemporaryDirectory() as td:
            root = Path(td)
            actual = root/"results/suite12_structured_cache"
            actual.mkdir(parents=True)
            sentinel = actual/"real_faculty_fold_1.joblib"
            sentinel.write_bytes(b"DO_NOT_TOUCH")
            r = subprocess.run([sys.executable, str(PACKAGE/"train_suite34.py"),
                "--synthetic", "--root", str(root), "--preflight-only"],
                cwd=PACKAGE, capture_output=True, text=True, timeout=30)
            self.assertNotEqual(r.returncode, 0)
            self.assertEqual(sorted(p.name for p in actual.iterdir()), [sentinel.name])
            self.assertFalse((root/"SYNTHETIC_DATA.json").exists())
