"""Read pinned Git objects and execute local, isolated regression probes."""
import pathlib
import subprocess
import sys
import tempfile

HERE = pathlib.Path(__file__).resolve().parent
REPO = pathlib.Path(sys.argv[1]) if len(sys.argv) > 1 else HERE.parents[2] / "zhctproject/store"
SHA = "6129c44162df0747c8f294c9906fdabb239006ed"
FILES = {
    "BillLogic.php": "application/p/logic/Bill.php",
    "BillModel.php": "application/model/Bill.php",
    "recipeManagement.vue": "public/static/src/view/restaurantManagement/recipeManagement.vue",
}
with tempfile.TemporaryDirectory(prefix="xinwu-menu-regression-") as folder:
    root = pathlib.Path(folder)
    for name, path in FILES.items():
        (root / name).write_bytes(subprocess.check_output(["git", "-C", str(REPO), "show", f"{SHA}:{path}"]))
    for command, output in [
        (["php", str(HERE / "reproduce.php"), str(root / "BillLogic.php"), str(root / "BillModel.php")], "reproduction-results.json"),
        (["node", str(HERE / "reproduce-frontend.cjs"), str(root / "recipeManagement.vue")], "frontend-results.json"),
    ]:
        result = subprocess.run(command, check=True, capture_output=True, text=True)
        if result.stderr:
            print(result.stderr, file=sys.stderr)
        (HERE / output).write_text(result.stdout)
        print(output, result.stdout)
