import json
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest

import numpy as np
import pandas as pd

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


class PipelineTests(unittest.TestCase):
    def test_feature_schema_is_the_intersection_across_all_requested_folds(self):
        import joblib
        import train_suite26
        from types import SimpleNamespace
        from tests.test_data import write_canonical_fixture
        with tempfile.TemporaryDirectory() as temporary:
            root = Path(temporary)
            cache, _, _, _ = write_canonical_fixture(root, faculty='A', fold=1)
            _, _, later, _ = write_canonical_fixture(root, faculty='A', fold=2)
            raw = joblib.load(later)
            raw['feature_names'] = ['raw_b', 'extra']
            joblib.dump(raw, later)
            args = SimpleNamespace(cache_dir=cache, faculties=['A'], folds=[1, 2])
            self.assertEqual(train_suite26.common_features_for_run(args), ['raw_b'])

    def test_full_reference_directory_names_resolve_to_the_old_primary(self):
        from train_suite26 import resolve_args
        from mint_metrics import add_references
        with tempfile.TemporaryDirectory() as temporary:
            root = Path(temporary)
            old = root / 'results' / 'suite26_tree_native_student_memory'
            (old / 'fold_6').mkdir(parents=True)
            target = pd.DataFrame(dict(fold=[6, 6], faculty=['IT', 'IT'],
                                       sample_id=['a', 'b'], student_id=['s1', 's2'], course_id=['c', 'c'],
                                       semester=[6070, 6070], actual=[0, 1]))
            source = target[['student_id', 'course_id', 'actual']].copy()
            source['suite26_tair'] = [.1, .8]
            source.to_csv(old / 'fold_6' / 'IT_scores.csv', index=False)
            args = resolve_args(['--root', str(root), '--faculties', 'IT'])
            wide, audit = add_references(target, args.reference_dirs)
            np.testing.assert_allclose(wide.suite26_original, [.1, .8])
            self.assertEqual(audit[audit.reference == 'suite26_original'].iloc[0].status, 'loaded')

    def command(self, *args, okay=True):
        result = subprocess.run([sys.executable, *map(str, args)], cwd=ROOT,
                                capture_output=True, text=True, timeout=180)
        if okay:
            self.assertEqual(result.returncode, 0, result.stdout[-3000:] + '\n' + result.stderr[-4000:])
        else:
            self.assertNotEqual(result.returncode, 0)
        return result

    def test_canonical_cache_to_refit_checkpoint_report_and_resume(self):
        with tempfile.TemporaryDirectory() as temporary:
            root = Path(temporary)
            self.command('make_synthetic_cache.py', '--out-dir', root / 'data', '--students', 18, '--folds', '1', '2')
            args = ['train_suite26.py', '--root', root / 'data', '--out-dir', root / 'run',
                    '--folds', '1', '2', '--faculties', 'IT', 'BUS', '--trees', '2', '--depth', '2',
                    '--min-leaf', '2', '--hidden', '8', '--ability-dim', '3', '--epochs', '2',
                    '--min-epochs', '1', '--patience', '1', '--batch-size', '32', '--eval-batch-size', '64',
                    '--max-samples', '1000', '--max-history', '4', '--val-semesters', '2', '--device', 'cpu',
                    '--threads', '1', '--bootstrap', '4', '--dev-folds', '1', '--holdout-folds', '2']
            self.command(*args, '--preflight-only')
            self.assertFalse((root / 'run').exists())
            self.command(*args)
            pred = pd.read_csv(root / 'run' / 'predictions.csv')
            self.assertEqual(set(pred['fold']), {1, 2})
            for col in ['suite26_seed_forest', 'suite26_tree_only', 'suite26_memory', 'suite26_mint']:
                self.assertTrue(np.isfinite(pred[col]).all(), col)
            self.assertTrue((root / 'run' / 'component_uplifts.csv').exists())
            self.assertTrue((root / 'run' / 'bootstrap_ap.csv').exists())
            self.assertTrue((root / 'run' / 'fold_1' / 'intervention_metrics.csv').exists())
            log = pd.read_csv(root / 'run' / 'fold_1' / 'training_log.csv')
            for variant in ['tree_only', 'memory', 'mint']:
                selected = log[(log.variant == variant) & (log.phase == 'selection')]
                refit = log[(log.variant == variant) & (log.phase == 'refit')]
                self.assertEqual(int(selected.iloc[0].examples), int(refit.iloc[0].examples),
                                 'Refit must preserve the selected optimizer example budget below the sampling cap.')
            mint = log[(log['variant'] == 'mint') & (log['phase'] == 'selection')]
            for name in ['gradient_thresholds', 'gradient_leaf_logits', 'gradient_memory_value.weight']:
                self.assertTrue((mint[name] > 0).all(), name)
            replay = root / 'replay.csv'
            self.command('predict_suite26.py', '--checkpoint', root / 'run' / 'fold_1' / 'mint.pt',
                         '--root', root / 'data', '--out-file', replay, '--device', 'cpu', '--threads', '1')
            stored = pred[pred['fold'] == 1].sort_values('sample_id')
            loaded = pd.read_csv(replay).sort_values('sample_id')
            np.testing.assert_array_equal(stored.sample_id, loaded.sample_id)
            np.testing.assert_allclose(stored.suite26_mint, loaded.suite26_mint, atol=2e-7)
            run_mtime = (root / 'run' / 'fold_1' / 'mint.pt').stat().st_mtime_ns
            self.command(*args, '--resume')
            self.assertEqual(run_mtime, (root / 'run' / 'fold_1' / 'mint.pt').stat().st_mtime_ns)
            self.command(*args, okay=False)
            mismatch = self.command(*args, '--resume', '--lr', '.02', okay=False)
            self.assertIn('configuration', mismatch.stderr.lower())
            complete = json.loads((root / 'run' / 'fold_1' / 'complete.json').read_text())
            self.assertEqual(complete['status'], 'complete')


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