"""Run a complete, small synthetic experiment in a new output directory."""
from __future__ import annotations

import argparse
from pathlib import Path
import subprocess
import sys
import numpy as np
import pandas as pd
from mint_runtime import atomic_json


def main():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument('--out-dir', required=True, help='A new or empty directory; never the real AQ result directory.')
    args = p.parse_args()
    folder = Path(args.out_dir).expanduser().resolve()
    if folder.exists() and any(folder.iterdir()):
        raise FileExistsError(f'Smoke output is nonempty: {folder}')
    folder.mkdir(parents=True, exist_ok=True)
    here = Path(__file__).resolve().parent

    def run(script, *options):
        with (folder / f'{Path(script).stem}.log').open('w') as log:
            child = subprocess.Popen([sys.executable, '-u', str(here / script), *map(str, options)],
                                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, cwd=here)
            for line in child.stdout:
                print(line, end='', flush=True)
                log.write(line)
            code = child.wait()
        if code:
            raise RuntimeError(f'{script} failed with code {code}; inspect its smoke log.')

    run('make_synthetic_cache.py', '--out-dir', folder / 'data', '--students', 24, '--folds', '1', '2')
    run('train_suite26.py', '--root', folder / 'data', '--out-dir', folder / 'results',
        '--folds', '1', '2', '--faculties', 'IT', 'BUS', '--trees', '2', '--depth', '2', '--min-leaf', '2',
        '--hidden', '12', '--ability-dim', '4', '--epochs', '2', '--min-epochs', '1', '--patience', '1',
        '--batch-size', '32', '--eval-batch-size', '64', '--max-samples', '1000', '--max-history', '8',
        '--device', 'cpu', '--threads', '1', '--bootstrap', '20', '--dev-folds', '1', '--holdout-folds', '2')
    run('predict_suite26.py', '--checkpoint', folder / 'results' / 'fold_1' / 'mint.pt',
        '--root', folder / 'data', '--out-file', folder / 'reloaded.csv', '--device', 'cpu', '--threads', '1')
    expected = pd.read_csv(folder / 'results' / 'predictions.csv')
    expected = expected[expected.fold == 1].sort_values('sample_id')
    reloaded = pd.read_csv(folder / 'reloaded.csv').sort_values('sample_id')
    np.testing.assert_array_equal(expected.sample_id, reloaded.sample_id)
    error = float(np.max(np.abs(expected.suite26_mint.to_numpy() - reloaded.suite26_mint.to_numpy())))
    np.testing.assert_allclose(expected.suite26_mint, reloaded.suite26_mint, atol=2e-7)
    atomic_json(dict(status='passed', rows=len(expected), checkpoint_max_absolute_error=error,
                     synthetic_only=True, real_AQ_tested=False, CUDA_tested=False), folder / 'smoke_verification.json')
    print(f'Smoke experiment and checkpoint replay passed: {folder}', flush=True)


if __name__ == '__main__':
    main()
