#!/usr/bin/env python3
"""Build the verified principle layer from chair citations (#153, corpus redesign).

"Trusted = citation, not review" (chaim 2026-06-20). A principle is `verified` iff
its SOURCE precedent was actually cited by a chair (any committee decision); never
from human review. This:
  1. Runs the citator (`extract_internal_citations`) over ALL committee decisions —
     not just דפנה's — so other chairs' citations populate the graph too (tier-2).
  2. Recomputes halachot.verified / cite_count from precedent_internal_citations.

Idempotent. Run after ingesting new chair decisions (or wire into the ingest path)
so the verified layer grows automatically. EMBEDDING/REGEX-only for the citator,
no LLM.

  cd ~/legal-ai/mcp-server
  HOME=/home/chaim .venv/bin/python ../scripts/build_verified_layer.py            # full
  HOME=/home/chaim .venv/bin/python ../scripts/build_verified_layer.py --no-citator  # refresh only
"""
from __future__ import annotations

import argparse
import asyncio
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "mcp-server", "src"))

from legal_mcp.services import citation_extractor, db  # noqa: E402


async def _run(run_citator: bool) -> int:
    if run_citator:
        print("→ extracting citations from ALL committee decisions (citator)…", flush=True)
        res = await citation_extractor.extract_all_internal_committee()
        print(f"  citator: {res}", flush=True)
    print("→ refreshing verified/cite_count from chair citations…", flush=True)
    stats = await db.refresh_verified_layer()
    print(f"\n── verified layer ──")
    print(f"   verified principles: {stats['verified_principles']}")
    print(f"   verified precedents: {stats['verified_precedents']}")
    return 0


def main() -> int:
    p = argparse.ArgumentParser(description="Build verified principle layer (#153)")
    p.add_argument("--no-citator", action="store_true",
                   help="skip citation extraction; only recompute verified/cite_count")
    a = p.parse_args()
    return asyncio.run(_run(run_citator=not a.no_citator))


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