#!/usr/bin/env python3
"""Regenerate checked-in deployment and x402 registries and require a clean tree."""
from __future__ import annotations

import os
import subprocess
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def run(*command: str) -> None:
    subprocess.run(command, cwd=ROOT, check=True)


def main() -> None:
    env = os.environ.copy()
    env.setdefault("SOURCE_DATE_EPOCH", "0")
    subprocess.run(["python3", "pr7/registry/build_full_registry.py"], cwd=ROOT, env=env, check=True)
    subprocess.run(["python3", "pr7/registry/validate_bazaar_v2.py"], cwd=ROOT, env=env, check=True)
    subprocess.run(["python3", "baseline/build_deployments.py"], cwd=ROOT, env=env, check=True)

    result = subprocess.run(
        ["git", "diff", "--quiet", "--", "baseline/deployments.json", "pr7/generated"],
        cwd=ROOT,
    )
    if result.returncode:
        raise SystemExit(
            "generated deployment or x402 registry drift; regenerate locally and commit the reviewed output"
        )
    print("verified generated deployment and x402 registries are clean after regeneration")


if __name__ == "__main__":
    main()
