"""All-six canonical-cache CLI, checkpoint, provenance, resume and archive contracts."""
from __future__ import annotations

import json
import hashlib
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
import zipfile

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

ROOT = Path(__file__).resolve().parents[1]
VARIANTS = ('stock', 'native', 'deterministic', 'global', 'path', 'mcgb')


def tiny_arguments(root, output, seeds=('19', '23')):
    return ['--root', str(root), '--out-dir', str(output), '--folds', '1', '2',
            '--faculties', 'IT', 'BUS', '--dev-folds', '1', '--holdout-folds', '2',
            '--depth-grid', '2', '--tree-grid', '2', '4', '--max-trees', '4', '--path-grid', '0', '1.5',
            '--bins', '8', '--min-leaf', '2', '--min-hessian', '0', '--max-history', '4', '--hidden', '8',
            '--reader-epochs', '1', '--reader-max-rows', '1000', '--reader-batch', '32',
            '--adapt-epochs', '1', '--adapt-max-rows', '128', '--adapt-batch', '32', '--adapt-trees', '2',
            '--batch-size', '64', '--origins', '1', '--origin-min-rows', '8',
            '--origin-min-faculty-rows', '4', '--origin-min-positive', '1', '--origin-min-negative', '1',
            '--origin-min-train-rows', '8', '--device', 'cpu', '--threads', '1', '--bootstrap', '4',
            '--reference-dirs', '--seeds', *seeds]


class EndToEndTests(unittest.TestCase):
    @classmethod
    def command(cls, script, *args, okay=True, env=None):
        command = ['bash', str(ROOT / script)] if str(script).endswith('.sh') else [sys.executable, str(ROOT / script)]
        result = subprocess.run([*command, *map(str, args)], cwd=ROOT, capture_output=True,
                                text=True, timeout=240, env=env)
        if okay and result.returncode:
            raise AssertionError(result.stdout[-8000:] + '\n' + result.stderr[-8000:])
        if not okay and not result.returncode:
            raise AssertionError('Expected a failing command: ' + str(command))
        return result

    @classmethod
    def setUpClass(cls):
        cls.temporary = tempfile.TemporaryDirectory(prefix='suite27_e2e_')
        cls.root = Path(cls.temporary.name)
        cls.data, cls.output = cls.root / 'data', cls.root / 'run'
        cls.command('make_synthetic_cache.py', '--out-dir', cls.data, '--students', '16', '--folds', '1', '2')
        cls.arguments = tiny_arguments(cls.data, cls.output)
        result = cls.command('train_suite27.py', *cls.arguments, '--preflight-only')
        if cls.output.exists():
            raise AssertionError('Preflight wrote result files')
        cls.preflight_console = result.stdout
        cls.training_console = cls.command('train_suite27.py', *cls.arguments).stdout
        cls.predictions = pd.read_csv(cls.output / 'predictions.csv')

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

    def test_all_six_two_seeds_reproduce_from_reload_and_average_by_key(self):
        for fold in (1, 2):
            folder = self.output / f'fold_{fold}'
            aggregate = pd.read_csv(folder / 'predictions.csv').sort_values('sample_id')
            for variant in VARIANTS:
                values = []
                for seed in (19, 23):
                    checkpoint = folder / f'model_{variant}_seed_{seed}.pt'
                    stored = pd.read_csv(folder / f'predictions_{variant}_seed_{seed}.csv').sort_values('sample_id')
                    replay = self.root / f'replay_f{fold}_{variant}_{seed}.csv'
                    self.command('predict_suite27.py', '--checkpoint', checkpoint, '--root', self.data,
                                 '--out-file', replay, '--threads', '1')
                    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[f'suite27_{variant}'], loaded[f'suite27_{variant}'], atol=2e-10)
                    values.append(stored[f'suite27_{variant}'].to_numpy())
                np.testing.assert_allclose(aggregate[f'suite27_{variant}'], np.mean(values, axis=0), atol=2e-10)
                np.testing.assert_allclose(aggregate[f'seed_sd_{variant}'], np.std(values, axis=0), atol=2e-10)
        self.assertEqual(set(self.predictions.fold), {1, 2})

    def test_manifest_and_completion_markers_cover_all_fingerprints_and_artifacts(self):
        manifest = json.loads((self.output / 'run_manifest.json').read_text())
        for name in ('configuration_sha256', 'schema_sha256', 'code_sha256', 'runtime_sha256', 'input_sha256'):
            self.assertEqual(len(manifest[name]), 64)
        self.assertEqual(len(manifest['inputs']), 8)
        self.assertIn('run_suite27.sh', manifest['code'])
        self.assertIn('torch', manifest['runtime'])
        self.assertIn('scipy', manifest['runtime'])
        self.assertIn('joblib', manifest['runtime'])
        for fold in (1, 2):
            folder = self.output / f'fold_{fold}'
            marker = json.loads((folder / 'complete.json').read_text())
            self.assertEqual(marker['status'], 'complete')
            expected_files = {str(path.relative_to(folder)) for path in folder.rglob('*')
                              if path.is_file() and path.name != 'complete.json'}
            self.assertEqual(set(marker['files']), expected_files)
            self.assertEqual(sum(name.endswith('.pt') for name in expected_files), 12)

    def test_resume_reuses_complete_checkpoints_and_refuses_changed_configuration(self):
        path = self.output / 'fold_1/model_mcgb_seed_19.pt'
        before = path.stat().st_mtime_ns
        result = self.command('train_suite27.py', *self.arguments, '--resume')
        self.assertIn('[RESUME F1] Verified complete fold', result.stdout)
        self.assertEqual(before, path.stat().st_mtime_ns)
        refusal = self.command('train_suite27.py', *self.arguments, '--resume', '--learning-rate', '.12', okay=False)
        self.assertIn('fingerprints do not match', refusal.stderr)
        self.command('train_suite27.py', *self.arguments, okay=False)

    def test_changed_input_is_rejected_by_reload_and_resume(self):
        path = self.data / 'results/cross_faculty_residual_moe/cache/IT_fold_1.joblib'
        content = path.read_bytes()
        try:
            path.write_bytes(content + b'fingerprint change')
            refused = self.command('train_suite27.py', *self.arguments, '--resume', okay=False)
            self.assertIn('fingerprints do not match', refused.stderr)
            refused = self.command('predict_suite27.py', '--checkpoint', self.output / 'fold_1/model_mcgb_seed_19.pt',
                                   '--root', self.data, '--out-file', self.root / 'bad_input.csv', okay=False)
            self.assertIn('differs from training', refused.stderr)
            self.assertFalse((self.root / 'bad_input.csv').exists())
        finally:
            path.write_bytes(content)

    def test_source_fingerprints_and_tampered_complete_artifact_are_rejected(self):
        checkpoint = torch.load(self.output / 'fold_1/model_mcgb_seed_19.pt', weights_only=False)
        checkpoint['provenance']['code_hashes']['runtime.py'] = '0' * 64
        corrupt = self.root / 'wrong_source.pt'
        torch.save(checkpoint, corrupt)
        refused = self.command('predict_suite27.py', '--checkpoint', corrupt, '--root', self.data,
                               '--out-file', self.root / 'wrong_source.csv', okay=False)
        self.assertIn('source code fingerprints differ', refused.stderr)
        path = self.output / 'fold_1/tree_summary.csv'
        content = path.read_bytes()
        try:
            path.write_bytes(content + b'corrupt')
            refused = self.command('train_suite27.py', *self.arguments, '--resume', okay=False)
            self.assertIn('modified artifact', refused.stderr)
        finally:
            path.write_bytes(content)

    def test_changed_runtime_provenance_is_rejected_before_prediction_output(self):
        checkpoint = torch.load(self.output / 'fold_1/model_mcgb_seed_19.pt', weights_only=False)
        checkpoint['provenance']['runtime']['sklearn'] = '0.fake'
        corrupt = self.root / 'wrong_runtime.pt'
        output = self.root / 'wrong_runtime.csv'
        torch.save(checkpoint, corrupt)
        refused = self.command('predict_suite27.py', '--checkpoint', corrupt, '--root', self.data,
                               '--out-file', output, okay=False)
        self.assertIn('runtime fingerprints differ', refused.stderr)
        self.assertIn('use the original', refused.stderr)
        self.assertFalse(output.exists())

    def test_unsupported_preflight_prints_audit_without_files_and_training_saves_failure_audit(self):
        output = self.root / 'unsupported'
        args = tiny_arguments(self.data, output)
        failed = self.command('train_suite27.py', *args, '--origin-min-rows', '100000', '--preflight-only', okay=False)
        self.assertIn('failed origin support', failed.stdout)
        self.assertIn('train_faculty_rows', failed.stdout)
        self.assertFalse(output.exists())
        self.command('train_suite27.py', *args, '--origin-min-rows', '100000', okay=False)
        audit = pd.read_csv(output / 'fold_1/origin_support_failure.csv')
        self.assertGreater(len(audit), 0)
        self.assertFalse(list(output.rglob('*.pt')))

    def test_cli_rejects_future_operational_calibration_before_preflight_or_training_outputs(self):
        for preflight in (True, False):
            output = self.root / f'future_threshold_{preflight}'
            args = tiny_arguments(self.data, output)
            extras = ['--preflight-only'] if preflight else []
            refused = self.command('train_suite27.py', *args, '--dev-folds', '2', '--holdout-folds', '1', *extras, okay=False)
            self.assertIn('chronology violated', refused.stderr)
            self.assertFalse(output.exists())

    def test_collector_omits_weights_and_predictions_by_default(self):
        output = self.root / 'reports.zip'
        self.command('collect_suite27_reports.py', '--results-dir', self.output, '--out-file', output)
        with zipfile.ZipFile(output) as archive:
            names = archive.namelist()
            self.assertIn('COLLECTION_MANIFEST.json', names)
            manifest = json.loads(archive.read('COLLECTION_MANIFEST.json'))
            self.assertEqual(manifest['method'], 'archive_existing_reports_without_recomputation')
            self.assertFalse(manifest['include_predictions'])
            self.assertTrue(manifest['weightsExcluded'])
            self.assertEqual({row['path'] for row in manifest['inventory']}, set(names) - {'COLLECTION_MANIFEST.json'})
            for row in manifest['inventory']:
                payload = archive.read(row['path'])
                self.assertEqual(row['bytes'], len(payload))
                self.assertEqual(row['sha256'], hashlib.sha256(payload).hexdigest())
            self.assertIn('run_manifest.json', names)
            self.assertIn('student_workload.csv', names)
            self.assertIn('fold_1/complete.json', names)
            self.assertFalse(any(name.endswith('.pt') or 'prediction' in name or name.endswith('_scores.csv') for name in names))
        included = self.root / 'reports_predictions.zip'
        self.command('collect_suite27_reports.py', '--results-dir', self.output, '--out-file', included, '--include-predictions')
        with zipfile.ZipFile(included) as archive:
            self.assertIn('predictions.csv', archive.namelist())
            self.assertFalse(any(name.endswith('.pt') for name in archive.namelist()))
            self.assertTrue(json.loads(archive.read('COLLECTION_MANIFEST.json'))['include_predictions'])

    def test_resource_selection_refit_and_all_console_tables_are_complete(self):
        for fold in (1, 2):
            folder = self.output / f'fold_{fold}'
            resource = pd.read_csv(folder / 'resource_timing.csv')
            self.assertEqual(len(resource), 13)
            self.assertTrue((resource.seconds >= 0).all())
            self.assertTrue((resource.peak_host_rss_mb > 0).all())
            self.assertEqual(set(resource.peak_host_rss_semantics), {'lifetime_process_peak'})
            refits = resource[resource.phase == 'refit_and_diagnostics']
            self.assertTrue((refits.checkpoint_bytes > 0).all())
            for row in refits.itertuples(index=False):
                model_path = folder / f'model_{row.variant}_seed_{int(row.seed)}.pt'
                self.assertEqual(row.checkpoint_bytes, model_path.stat().st_size)
            log = pd.read_csv(folder / 'reader_training.csv')
            self.assertIn('selection', set(log.phase))
            self.assertIn('refit', set(log.phase))
            selection = pd.read_csv(folder / 'selection_cells.csv')
            self.assertEqual(set(selection.seed), {19, 23})
            self.assertEqual(set(selection.faculty), {'IT', 'BUS'})
            support = pd.read_csv(folder / 'target_support.csv')
            self.assertEqual(list(support.columns), ['faculty', 'rows', 'positives', 'negatives', 'students'])
            self.assertEqual(set(support.faculty), {'IT', 'BUS'})
        for text in ('full origin support', 'target faculty metrics', 'Seed variation', 'Per Cell Metrics',
                     'Per Fold Metrics', 'Per Faculty Metrics', 'Operational Metrics', 'Bootstrap Ap', 'IT', 'BUS'):
            self.assertIn(text, self.training_console)
        workload = pd.read_csv(self.output / 'student_workload.csv')
        self.assertEqual(set(workload.faculty), {'IT', 'BUS'})
        self.assertEqual(set(workload.policy), {'fixed', 'budget_10', 'budget_15', 'budget_20', 'budget_25'})

    def test_logger_captures_both_streams_append_and_exit_status(self):
        log = self.root / 'shell/custom.log'
        env = dict(os.environ, PYTHON=sys.executable, SUITE27_LOG_FILE=str(log))
        result = self.command('run_suite27.sh', '--root', self.data, '--faculties', 'IT', '--folds', '99', okay=False, env=env)
        self.assertNotEqual(result.returncode, 0)
        first = log.read_text()
        self.assertIn('Started:', first)
        self.assertIn('Traceback', first)
        self.assertIn('Finished:', first)
        self.command('run_suite27.sh', '--help', env=env)
        second = log.read_text()
        self.assertTrue(second.startswith(first))
        self.assertEqual(second.count('[suite27] Started:'), 2)
        self.assertIn('exit=0', second)


class CLIUtilityTests(unittest.TestCase):
    def test_target_support_keeps_zero_row_faculty_without_dummy_predictions(self):
        from runtime import faculty_support
        from types import SimpleNamespace
        table = SimpleNamespace(faculty_names=('IT', 'BUS'), faculty=np.array([0, 0]),
                                y=np.array([1, 0]), student=np.array(['s0', 's0']))
        support = faculty_support(table, ['IT', 'BUS'])
        self.assertEqual(list(support.faculty), ['IT', 'BUS'])
        self.assertEqual(tuple(support.iloc[0][['rows', 'positives', 'negatives', 'students']]), (2, 1, 1, 1))
        self.assertEqual(tuple(support.iloc[1][['rows', 'positives', 'negatives', 'students']]), (0, 0, 0, 0))
        self.assertEqual(int(support.rows.sum()), len(table.y))

    def test_common_feature_schema_intersects_all_folds(self):
        from make_synthetic_cache import generate
        from train_suite27 import common_features_for_run
        from types import SimpleNamespace
        with tempfile.TemporaryDirectory() as temporary:
            root = generate(Path(temporary) / 'fixture', students=8)
            cache = root / 'results/cross_faculty_residual_moe/cache'
            path = cache / 'IT_fold_2.joblib'
            raw = joblib.load(path)
            raw['feature_names'][-1] = 'fold2_only'
            joblib.dump(raw, path)
            names = common_features_for_run(SimpleNamespace(cache_dir=cache, faculties=['IT'], folds=[1, 2]))
            self.assertNotIn('fold2_only', names)
            self.assertNotIn('course_kind', names)

    def test_prior_mint_aliases_align_to_previous_keyed_outputs(self):
        from evaluation import add_references
        from train_suite27 import resolve_args
        with tempfile.TemporaryDirectory() as temporary:
            root = Path(temporary)
            folder = root / 'results/suite26_joint_tree_memory/fold_6'
            folder.mkdir(parents=True)
            target = pd.DataFrame(dict(fold=[6, 6], faculty=['IT', 'IT'], sample_id=['a', 'b'],
                                       student_id=['s0', 's1'], course_id=['c0', 'c1'], semester=[6070, 6070], actual=[0, 1]))
            source = target.assign(p_suite26_mint=[.1, .9], p_suite26_memory=[.2, .8], p_suite26_tree=[.3, .7])
            source.iloc[::-1].to_csv(folder / '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_mint, [.1, .9])
            np.testing.assert_allclose(wide.suite26_memory, [.2, .8])
            np.testing.assert_allclose(wide.suite26_tree, [.3, .7])
            self.assertTrue((audit.status == 'loaded').all())


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