from pathlib import Path
import argparse, shutil, subprocess

ROOT = Path(__file__).resolve().parent
WS = ROOT / "workspace"
SEED = ROOT / "fixture_seed.bundle"

parser = argparse.ArgumentParser(description="Initialize the synthetic local fixture without replacing existing learner work unless --reset is explicit.")
parser.add_argument("--reset", action="store_true", help="Delete and recreate the local synthetic workspace. This is destructive to local learner edits/evidence in workspace/.")
args = parser.parse_args()

if not SEED.exists():
    raise SystemExit("Fixture seed is missing.")

if WS.exists():
    if not args.reset:
        if not (WS / ".git").exists():
            raise SystemExit("workspace/ already exists but is not the expected Git fixture. Preserve it or use --reset explicitly if replacement is intended.")
        print("FIXTURE_PRESENT_PRESERVED", WS)
        raise SystemExit(0)
    shutil.rmtree(WS)

subprocess.run(["git", "clone", "-q", str(SEED), str(WS)], check=True)
subprocess.run(["git", "branch", "main", "origin/main"], cwd=WS, check=True, stdout=subprocess.DEVNULL)
subprocess.run(["git", "switch", "-q", "feature/coupon-rounding"], cwd=WS, check=True)
print("FIXTURE_READY", WS)
