"""Stufe 5 — Review-Diff mergen: vorgeschlagene Beziehungskanten (review_status=proposed)
dedupliziert in die Registry mergen (kein LLM/Key). Kleine Beziehungs-Sprache:
depends_on/supports/produces_evidence_for/implements/derived_from.

  python3 scripts/obligation_discovery/merge_review_diff.py obligations/cra.json /tmp/cra_edges_review.json
"""
from __future__ import annotations

import argparse
import json

from _core import SEMANTIC_EDGE_TYPES, merge_edges


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("registry")
    ap.add_argument("review_diff")
    ap.add_argument("--write", action="store_true", help="in die Registry schreiben (sonst dry-run)")
    a = ap.parse_args()
    reg = json.load(open(a.registry, encoding="utf-8"))
    diff = json.load(open(a.review_diff, encoding="utf-8"))
    proposed = diff.get("proposed_edges", diff if isinstance(diff, list) else [])
    merged, added = merge_edges(reg.get("relationships", []), proposed)
    print(f"proposed: {len(proposed)} | added (dedupliziert): {added}")
    if a.write:
        reg["relationships"] = merged
        reg["relationship_types"] = list(SEMANTIC_EDGE_TYPES)
        json.dump(reg, open(a.registry, "w", encoding="utf-8"), ensure_ascii=False, indent=1)
        print(f"written: {a.registry}")
    else:
        print("dry-run (use --write to apply)")


if __name__ == "__main__":
    main()
