#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Legacy-named, read-only guard: verify frozen vector errors; never rewrite them."""
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT / "tools"))

from conform import Failure, load_json, mutate, validate_document


def main() -> int:
    positive = load_json(ROOT / "vectors/positive.json")
    adversarial = load_json(ROOT / "vectors/adversarial.json")
    by_id = {item["id"]: item for item in positive["vectors"]}
    mismatches = []
    for vector in adversarial["vectors"]:
        try:
            base = by_id[vector["base"]]
            validate_document(mutate(base["document"], vector["mutation"]), base.get("terms"))
            mismatches.append((vector["id"], "unexpectedly accepted"))
        except Failure as exc:
            if exc.code != vector["expectedError"]:
                mismatches.append((vector["id"], f"expected {vector['expectedError']}, observed {exc.code}"))
        except Exception:
            mismatches.append((vector.get("id", "MALFORMED"), "unsanitized internal exception"))
    for vector_id, message in mismatches:
        print(f"{vector_id}: {message}", file=sys.stderr)
    return 1 if mismatches else 0


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