"""Checks the ATT&CK mapping: ID format, technique-to-tactic membership, known controls, no stage without a control."""
import json, re
KNOWN_CONTROLS = {"email_filtering", "mfa", "patching", "edr", "least_privilege", "segmentation",
                  "egress_filtering", "logging_alerts", "offline_backups"}
chain = json.load(open("data/attack_chain.json"))["stages"]
tt = json.load(open("data/technique_tactics.json"))
problems = 0
for s in chain:
    for tid, name in s["techniques"]:
        if not re.fullmatch(r"T\d{4}(\.\d{3})?", tid):
            print(f"[ERROR] stage {s['n']}: bad ID format {tid}"); problems += 1; continue
        base = tid.split(".")[0]
        if base not in tt:
            print(f"[WARN ] stage {s['n']}: {tid} not in my technique table"); problems += 1
        elif s["tactic_id"] not in tt[base]:
            print(f"[ERROR] stage {s['n']}: {tid} is not listed under {s['tactic_id']}"); problems += 1
    bad = set(s["controls"]) - KNOWN_CONTROLS
    if bad: print(f"[ERROR] stage {s['n']}: unknown controls {bad}"); problems += 1
    if not s["controls"]: print(f"[ERROR] stage {s['n']}: no control breaks this stage"); problems += 1
n_tech = len({t[0] for s in chain for t in s["techniques"]})
print(f"stages: {len(chain)}  distinct technique IDs: {n_tech}  problems: {problems}")
