#!/usr/bin/env python3
"""
Run all built-in examples in tools/ru_bridge_extractor.py and validate outputs.

This script exists to make the package feel like a reproducible research artifact:

- regenerate audited objects from shipped host data + constraints
- schema-validate each generated robust certificate
- validate all robust certificates found under sources/ru/

Usage (from project root):
  python tools/run_all_examples.py

Optional:
  python tools/run_all_examples.py --alpha 0.15
"""

from __future__ import annotations
import argparse
import os
import subprocess
import sys

EXAMPLES = [
  ("toric_og3", ["--alpha", "0.15"]),
  ("fibonacci_contraction", ["--alpha", "0.2"]),
  ("fibonacci_og4_idle", []),
  ("fibonacci_og1_period2", []),
  ("ising_og3", ["--alpha", "0.15"]),
  ("ising_og2_projection", ["--alpha", "0.15"]),
]

def run(cmd: list[str]) -> int:
    p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
    print(p.stdout)
    return p.returncode

def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--root", default=".", help="Project root")
    args = ap.parse_args()
    root = os.path.abspath(args.root)

    extractor = [sys.executable, os.path.join(root, "tools", "ru_bridge_extractor.py")]
    validator = [sys.executable, os.path.join(root, "tools", "validate_certificates.py")]

    print("=== RU Bridge: regenerate + validate built-in examples ===")
    for ex, extra in EXAMPLES:
        print("\n--- Example:", ex, "---")
        rc = run(extractor + ["--root", root, "--example", ex, "--validate"] + extra)
        if rc != 0:
            print("FAILED example:", ex)
            return rc

    print("\n=== Validate all robust certificates under sources/ru/ ===")
    rc = run(validator + ["--root", root])
    return rc

if __name__ == "__main__":
    raise SystemExit(main())
