#!/usr/bin/env python3
from __future__ import annotations
import argparse, json, sys
from collections import Counter
from pathlib import Path
from typing import Any

ALLOWED_MAPPING_STATUS={"candidate","review","accepted","rejected","superseded"}

def load(path:Path, errors:list[str])->dict[str,Any]:
    if not path.is_file(): errors.append(f"missing file: {path}"); return {}
    try: data=json.loads(path.read_text(encoding="utf-8"))
    except (OSError,json.JSONDecodeError) as e: errors.append(f"invalid JSON {path}: {e}"); return {}
    if not isinstance(data,dict): errors.append(f"top level must be an object: {path}"); return {}
    return data

def uniq(items:list[str], label:str, errors:list[str])->set[str]:
    c=Counter(items); dup=sorted(k for k,v in c.items() if k and v>1)
    if dup: errors.append(f"duplicate {label}: {', '.join(dup)}")
    return {x for x in items if x}

def main()->int:
    ap=argparse.ArgumentParser(description="Validate NCRC UN R155 regulatory traceability")
    ap.add_argument("root", nargs="?", type=Path, default=Path("regulatory/r155"))
    ap.add_argument("--threat-register", type=Path, default=Path("data/threats_register.json"))
    ap.add_argument("--allow-orphans", action="store_true", help="Do not fail on Annex 5 nodes unreachable from mappings or related mitigations")
    a=ap.parse_args(); errors=[]; warnings=[]; root=a.root
    registry=load(root/"registry.json",errors); versions=load(root/"versions.json",errors); sources=load(root/"sources.json",errors); annex=load(root/"annex5.json",errors); mappings=load(root/"r155_threat_mappings.json",errors); threats=load(a.threat_register,errors)
    if errors:
        print("R155 VALIDATION FAILED"); [print("ERROR",x) for x in errors]; return 2
    reg_id=registry.get("regulation_id")
    if reg_id!="UN-R155": errors.append(f"registry regulation_id must be UN-R155, got {reg_id!r}")
    for name,data in [("versions",versions),("sources",sources),("annex5",annex),("mappings",mappings)]:
        if data.get("regulation_id")!=reg_id: errors.append(f"{name}.regulation_id {data.get('regulation_id')!r} != {reg_id!r}")
    model=registry.get("model",{})
    for key,expected in [("versions_file","versions.json"),("sources_file","sources.json")]:
        if model.get(key)!=expected: warnings.append(f"registry model {key} is {model.get(key)!r}, expected {expected!r}")
    if "annex5.json" not in model.get("annex_files",[]): warnings.append("registry model does not list annex5.json")

    source_rows=sources.get("sources",[]); version_rows=versions.get("versions",[]); nodes=annex.get("nodes",[]); map_rows=mappings.get("mappings",[]); threat_rows=threats.get("threats",[])
    for label,rows in [("sources",source_rows),("versions",version_rows),("annex5.nodes",nodes),("mappings",map_rows),("threat register",threat_rows)]:
        if not isinstance(rows,list): errors.append(f"{label} must be a list")
    if errors:
        print("R155 VALIDATION FAILED"); [print("ERROR",x) for x in errors]; return 2

    source_ids=uniq([str(x.get("source_id") or "") for x in source_rows if isinstance(x,dict)],"source_id",errors)
    version_ids=uniq([str(x.get("version_id") or "") for x in version_rows if isinstance(x,dict)],"version_id",errors)
    node_ids=uniq([str(x.get("id") or "") for x in nodes if isinstance(x,dict)],"node id",errors)
    mapping_ids=uniq([str(x.get("mapping_id") or "") for x in map_rows if isinstance(x,dict)],"mapping_id",errors)
    threat_ids=uniq([str(x.get("id") or "") for x in threat_rows if isinstance(x,dict)],"threat id",errors)

    # Version -> source.
    for v in version_rows:
        sid=str(v.get("source_id") or "")
        if not sid: errors.append(f"version {v.get('version_id')} has no source_id")
        elif sid not in source_ids: errors.append(f"version {v.get('version_id')} references missing source {sid}")

    # Annex dataset -> sources and version basis.
    for sid in annex.get("source_ids",[]):
        if sid not in source_ids: errors.append(f"annex5 references missing source {sid}")
    vb=annex.get("version_basis")
    if vb:
        vals=vb if isinstance(vb,list) else [vb]
        for value in vals:
            # Existing data sometimes stores free text here. Only enforce ID-shaped refs.
            if isinstance(value,str) and value.startswith("UN-R155-") and value not in version_ids:
                errors.append(f"annex5 version_basis references missing version {value}")

    # Annex graph edges.
    mitigation_edges=0; referenced_by_nodes=set()
    for n in nodes:
        nid=str(n.get("id") or "")
        if not nid: errors.append("annex5 node without id"); continue
        for field in ("related_mitigations","addresses","related_nodes"):
            refs=n.get(field,[]) or []
            if not isinstance(refs,list): errors.append(f"node {nid} {field} must be a list"); continue
            for ref in refs:
                if ref not in node_ids: errors.append(f"node {nid} {field} references missing node {ref}")
                else: referenced_by_nodes.add(ref); mitigation_edges += field in {"related_mitigations","addresses"}
        for sid in n.get("source_ids",[]) or []:
            if sid not in source_ids: errors.append(f"node {nid} references missing source {sid}")

    # Mapping -> threat/node/mitigations and controlled status.
    mapped_nodes=set(); mapped_threats=set()
    for m in map_rows:
        mid=str(m.get("mapping_id") or "<missing>"); tid=str(m.get("threat_id") or ""); nid=str(m.get("regulatory_node_id") or "")
        if not tid: errors.append(f"mapping {mid} has no threat_id")
        elif tid not in threat_ids: errors.append(f"mapping {mid} references missing threat {tid}")
        else: mapped_threats.add(tid)
        if not nid: errors.append(f"mapping {mid} has no regulatory_node_id")
        elif nid not in node_ids: errors.append(f"mapping {mid} references missing regulatory node {nid}")
        else: mapped_nodes.add(nid)
        status=str(m.get("status") or "")
        if status not in ALLOWED_MAPPING_STATUS: errors.append(f"mapping {mid} has invalid status {status!r}")
        mitigations=m.get("mitigation_nodes",[]) or []
        if not isinstance(mitigations,list): errors.append(f"mapping {mid} mitigation_nodes must be a list")
        else:
            for ref in mitigations:
                if ref not in node_ids: errors.append(f"mapping {mid} references missing mitigation node {ref}")
                else: referenced_by_nodes.add(ref)
        review=m.get("review")
        if status=="review" and review is not None and not isinstance(review,dict): errors.append(f"mapping {mid} review must be an object")

    # Mapping summary is derived data; verify if present.
    summary=mappings.get("summary",{})
    if isinstance(summary,dict):
        stated=summary.get("mapping_count")
        if stated is not None and stated != len(map_rows): errors.append(f"summary.mapping_count={stated} but actual={len(map_rows)}")
        stated=summary.get("threats_with_candidates")
        if stated is not None and stated != len(mapped_threats): warnings.append(f"summary.threats_with_candidates={stated}, mapped threats={len(mapped_threats)}")

    orphans=sorted(node_ids - mapped_nodes - referenced_by_nodes)
    if orphans:
        msg=f"{len(orphans)} orphan Annex 5 nodes (not mapped and not referenced by another node): {', '.join(orphans[:12])}" + (" ..." if len(orphans)>12 else "")
        (warnings if a.allow_orphans else errors).append(msg)

    print("UN R155 TRACEABILITY VALIDATION")
    print("="*38)
    print(f"Threats checked            : {len(threat_ids)}")
    print(f"Mappings checked           : {len(mapping_ids)}")
    print(f"Regulatory nodes checked   : {len(node_ids)}")
    print(f"Mitigation graph edges     : {mitigation_edges}")
    print(f"Versions checked           : {len(version_ids)}")
    print(f"Sources checked            : {len(source_ids)}")
    print(f"Threats with mappings      : {len(mapped_threats)}")
    print(f"Orphan regulatory nodes    : {len(orphans)}")
    if warnings:
        print("\nWARNINGS")
        for x in warnings: print(f"  - {x}")
    if errors:
        print("\nERRORS")
        for x in errors: print(f"  - {x}")
        print("\nR155 VALIDATION FAILED")
        return 2
    print("\nR155 VALIDATION PASSED")
    return 0

if __name__=="__main__": sys.exit(main())
