#!/usr/bin/env python3
"""Generate conservative review candidates between NCRC threats and UN R155 Annex 5 Part A."""
from __future__ import annotations

import argparse
import json
import re
import sys
from pathlib import Path
from typing import Any

STOP = {
    "and", "or", "the", "of", "to", "a", "an", "in", "on", "for", "with",
    "through", "from", "as", "by", "into", "across", "within", "regarding",
    "related", "vehicle", "vehicles", "cyber", "cybersecurity", "attack",
    "attacks", "system", "systems",
}

# These words are too generic to justify a regulatory mapping by themselves.
WEAK_TOKENS = {
    "end", "loss", "lost", "party", "third", "support", "service", "services",
    "information", "data", "used", "using", "related", "external", "hosted",
    "application", "applications", "software", "security", "risk", "risks",
    "management", "manufacturer", "oem",
}

# Concepts that must be treated as phrases, not as disconnected common words.
# The end-of-support concept deliberately has no forced Annex 5 target. It improves
# threat semantics but does not manufacture a regulatory relation that Annex 5 lacks.
CONCEPTS = {
    "end_of_support": {
        "phrases": (
            "end of support", "end-of-support", "end of life", "end-of-life",
            "oem support", "vendor support", "unsupported", "obsolete",
            "obsolescence", "security maintenance", "patch availability",
        ),
        "aliases": {"eol", "eos", "lifecycle", "legacy"},
    },
    "cloud_information_loss": {
        "phrases": (
            "loss of information in the cloud", "information lost in the cloud",
            "cloud data loss", "cloud information loss",
        ),
        "aliases": {"cloud", "backup", "availability"},
    },
    "third_party_hosted_application": {
        "phrases": (
            "hosted third-party application", "hosted third party application",
            "third-party application", "third party application",
            "external application provider",
        ),
        "aliases": {"supplier", "dependency", "hosted"},
    },
}

AREA_TO_CATEGORY = {f"4.3.{number}": f"4.3.{number}" for number in range(1, 8)}


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Build conservative UN R155 review mappings")
    parser.add_argument("--register", type=Path, default=Path("data/threats_register.json"))
    parser.add_argument("--annex5", type=Path, default=Path("regulatory/r155/annex5.json"))
    parser.add_argument("--output", type=Path, default=Path("regulatory/r155/r155_threat_mappings.json"))
    parser.add_argument("--min-score", type=float, default=0.20)
    parser.add_argument("--max-candidates", type=int, default=5)
    return parser.parse_args()


def load(path: Path) -> dict[str, Any]:
    try:
        data = json.loads(path.read_text(encoding="utf-8"))
    except FileNotFoundError as exc:
        raise ValueError(f"Input file not found: {path}") from exc
    except json.JSONDecodeError as exc:
        raise ValueError(f"Invalid JSON in {path}: {exc}") from exc
    if not isinstance(data, dict):
        raise ValueError(f"Expected JSON object in {path}")
    return data


def normalize(value: Any) -> str:
    text = str(value or "").lower()
    text = re.sub(r"[-_/]+", " ", text)
    return re.sub(r"\s+", " ", text).strip()


def tokens(value: Any) -> set[str]:
    return {
        word for word in re.findall(r"[a-z0-9]+", normalize(value))
        if len(word) > 2 and word not in STOP
    }


def threat_text(threat: dict[str, Any]) -> str:
    values = [
        threat.get("title", ""), threat.get("title_nl", ""),
        threat.get("category", ""), threat.get("annex5_area", ""),
        threat.get("impact_rationale", ""), threat.get("likelihood_rationale", ""),
        threat.get("risk_rationale", ""), " ".join(threat.get("terms", [])),
        " ".join(threat.get("surfaces", [])), " ".join(threat.get("tags", [])),
    ]
    return " ".join(str(value) for value in values if value)


def node_text(node: dict[str, Any]) -> str:
    return " ".join(
        str(value) for value in (
            node.get("label", ""), node.get("category_title", ""),
            node.get("description", ""),
        ) if value
    )


def concept_hits(text: str) -> set[str]:
    normalized = normalize(text)
    token_set = tokens(normalized)
    hits: set[str] = set()
    for name, rule in CONCEPTS.items():
        if any(normalize(phrase) in normalized for phrase in rule["phrases"]):
            hits.add(name)
        elif len(token_set & set(rule["aliases"])) >= 2:
            hits.add(name)
    return hits


def explicit_area(threat: dict[str, Any]) -> str | None:
    raw = str(threat.get("annex5_area") or "")
    return next((category for area, category in AREA_TO_CATEGORY.items() if area in raw), None)


def candidate_score(threat: dict[str, Any], node: dict[str, Any], explicit: str | None) -> tuple[float, list[str], list[str], str] | None:
    threat_raw = threat_text(threat)
    node_raw = node_text(node)
    threat_tokens = tokens(threat_raw)
    node_tokens = tokens(node_raw)
    overlap = threat_tokens & node_tokens
    strong_overlap = overlap - WEAK_TOKENS
    shared_concepts = concept_hits(threat_raw) & concept_hits(node_raw)

    if explicit and node.get("category_reference") == explicit:
        return 0.70, sorted(overlap), sorted(shared_concepts), "explicit_annex5_area"

    # Phrase-level meaning can support a candidate. Generic word overlap cannot.
    if shared_concepts:
        score = 0.50 + min(0.20, 0.10 * len(shared_concepts))
        score += min(0.12, 0.04 * len(strong_overlap))
        return min(score, 0.99), sorted(overlap), sorted(shared_concepts), "semantic_concept"

    # Require at least two non-generic shared terms for a lexical candidate.
    if len(strong_overlap) < 2:
        return None

    union = threat_tokens | node_tokens
    jaccard = len(strong_overlap) / len(union) if union else 0.0
    score = jaccard + min(0.24, 0.08 * len(strong_overlap))
    return min(score, 0.99), sorted(overlap), [], "strong_lexical_seed"


def main() -> int:
    config = parse_args()
    register = load(config.register)
    annex = load(config.annex5)
    threats = register.get("threats", [])
    nodes = [node for node in annex.get("nodes", []) if isinstance(node, dict) and node.get("part") == "A"]
    if not isinstance(threats, list) or not nodes:
        raise ValueError("Threat register or Annex 5 Part A nodes are empty")

    rows: list[dict[str, Any]] = []
    rejected_weak_only = 0
    for threat in threats:
        if not isinstance(threat, dict) or not threat.get("id"):
            continue
        explicit = explicit_area(threat)
        candidates = []
        for node in nodes:
            result = candidate_score(threat, node, explicit)
            if result is None:
                rejected_weak_only += 1
                continue
            score, overlap, concepts, basis = result
            if score < config.min_score:
                continue
            candidates.append((score, node, overlap, concepts, basis))
        candidates.sort(key=lambda item: (item[0], len(item[3]), len(item[2])), reverse=True)
        selected = candidates if explicit else candidates[:config.max_candidates]
        for score, node, overlap, concepts, basis in selected:
            rows.append({
                "mapping_id": f"R155-{threat['id']}-{node['reference'].replace('.', '_')}",
                "threat_id": threat["id"],
                "threat_title": threat.get("title", ""),
                "regulatory_node_id": node["id"],
                "regulatory_reference": node["reference"],
                "regulatory_category": node.get("category_reference"),
                "relationship": "related",
                "status": "review",
                "score": round(score, 3),
                "basis": basis,
                "matched_terms": overlap,
                "matched_concepts": concepts,
                "mitigation_nodes": node.get("related_mitigations", []),
                "rationale": (
                    f"Threat register explicitly identifies Annex 5 area {explicit}."
                    if basis == "explicit_annex5_area"
                    else "Candidate generated from shared domain-specific concepts or multiple non-generic terms; expert review required."
                ),
                "review": {"decision": None, "reviewed_by": None, "reviewed_at": None, "note": None},
            })

    counts = {str(threat["id"]): 0 for threat in threats if isinstance(threat, dict) and threat.get("id")}
    for row in rows:
        counts[row["threat_id"]] += 1

    output = {
        "schema_version": "1.1",
        "mapping_set_id": "NCRC-R155-ANNEX5-REVIEW-2",
        "regulation_id": "UN-R155",
        "annex_id": "UN-R155-ANNEX-5",
        "status": "review",
        "method": "explicit Annex 5 area seeds plus phrase-aware conservative lexical candidate generation",
        "source_threat_register": {"file": str(config.register), "threat_count": len(counts)},
        "regulatory_source": {"file": str(config.annex5), "part_a_reference_count": len(nodes)},
        "rules": {
            "accepted_created": False,
            "legal_compliance_inference": False,
            "expert_review_required": True,
            "weak_tokens_cannot_create_candidate": sorted(WEAK_TOKENS),
            "minimum_non_generic_overlap": 2,
        },
        "summary": {
            "mapping_count": len(rows),
            "threats_with_candidates": sum(value > 0 for value in counts.values()),
            "threats_without_candidates": [key for key, value in counts.items() if not value],
            "weak_only_pairs_rejected": rejected_weak_only,
        },
        "mappings": rows,
    }
    config.output.parent.mkdir(parents=True, exist_ok=True)
    config.output.write_text(json.dumps(output, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
    print(json.dumps(output["summary"], indent=2))
    return 0


if __name__ == "__main__":
    try:
        raise SystemExit(main())
    except (ValueError, OSError) as exc:
        print(f"ERROR: {exc}", file=sys.stderr)
        raise SystemExit(2)
