"""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 = ['main.cpp', 'CMakeLists.txt', 'vendor/factory-forge/src/core/simulation.cpp', 'vendor/factory-forge/include/factory/core/pathfinder.h', 'vendor/factory-forge/include/factory/core/project.h', 'vendor/factory-forge/include/factory/core/simulation.h', 'vendor/factory-forge/src/core/puzzles.cpp', 'vendor/factory-forge/include/factory/core/puzzles.h', 'vendor/factory-forge/src/core/pathfinder.cpp', 'vendor/factory-forge/src/core/project.cpp', 'vendor/factory-forge/SOURCE.txt', 'vendor/factory-forge/LICENSE', 'solution/main.cpp']
COMMANDS = [['cmake', '-S', '.', '-B', 'build', '-DCMAKE_BUILD_TYPE=Debug'], ['cmake', '--build', 'build', '-j', '2'], ['./build/lesson'], ['python3', '-c', "from pathlib import Path; Path('main.cpp').write_text(Path('solution/main.cpp').read_text())"], ['cmake', '--build', 'build', '-j', '2'], ['./build/lesson']]

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.")
