#!/usr/bin/env python3
"""Run both exact lift verifiers and check the generated display formulas."""
from __future__ import annotations

import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent
COMMANDS = (
    ("sympy_verify_lifts.py",),
    ("independent_verify_lifts.py",),
    (
        "generate_operators.py",
        "--check",
        "--check-source",
        "weyl-poisson-lifts.tex",
    ),
)


def main() -> int:
    print(f"Python {sys.version.split()[0]}")
    try:
        import sympy  # type: ignore
    except ImportError:
        print(
            "ERROR: SymPy is not installed. Run: python -m pip install -r requirements.txt",
            file=sys.stderr,
        )
        return 2
    print(f"SymPy {sympy.__version__}")

    for command in COMMANDS:
        path = ROOT / command[0]
        if not path.exists():
            print(f"ERROR: missing {command[0]}", file=sys.stderr)
            return 2
        print(f"\nRunning {' '.join(command)} ...")
        completed = subprocess.run(
            [sys.executable, str(path), *command[1:]],
            cwd=ROOT,
            text=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            check=False,
        )
        if completed.returncode != 0:
            print(completed.stdout)
            print(f"FAILED: {' '.join(command)}", file=sys.stderr)
            return completed.returncode
        print(completed.stdout.rstrip())
        print(f"PASSED: {' '.join(command)}")

    print("\nALL EXACT VERIFICATION CHECKS PASSED")
    print(
        "Verified: det(JF), the three-point collision, JA=AJ=I, all nine "
        "commuting-vector-field coefficients, the Weyl and Poisson lift "
        "relations, operators.tex, and the inline manuscript formulas."
    )
    return 0


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