"""Verify the example and exercise solution in a disposable copy."""
import os
import shutil
import subprocess
import tempfile
from pathlib import Path

ROOT = Path(__file__).resolve().parent
FILES = ['pubspec.yaml', 'lib/model.dart', 'test/model_test.dart', 'lib/main.dart', 'solution/model_test.dart', 'pubspec.lock']
COMMANDS = [['flutter', 'pub', 'get'], ['flutter', 'analyze', '--no-pub', 'lib', 'test'], ['flutter', 'test'], ['python3', '-c', "from pathlib import Path\nPath('test/solution_test.dart').write_text(Path('solution/model_test.dart').read_text())"], ['flutter', 'test', 'test/solution_test.dart'], ['flutter', 'analyze', '--no-pub', 'lib', 'test']]

with tempfile.TemporaryDirectory(prefix="appfabrik-learning-") as temporary:
    work = Path(temporary)
    for name in FILES:
        target = work / name
        target.parent.mkdir(parents=True, exist_ok=True)
        shutil.copyfile(ROOT / name, target)
    for command in COMMANDS:
        print("Running:", " ".join(command), flush=True)
        subprocess.run(command, cwd=work, check=True, timeout=900)
print("Example and solution checks completed.")
