import json,re
from pathlib import Path
R=json.loads(Path('data/threats_register.json').read_text())
A=json.loads(Path('data/annex5.json').read_text())
threats=R['threats']; anodes=[n for n in A['nodes'] if n['part']=='A']
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'}
def toks(x): return {w for w in re.findall(r'[a-z0-9]+',x.lower()) if len(w)>2 and w not in STOP}
def txt(t):
    vals=[t.get('title',''),t.get('category',''),t.get('annex5_area',''),' '.join(t.get('terms',[])),' '.join(t.get('surfaces',[])),' '.join(t.get('tags',[]))]
    return ' '.join(vals)
# Explicit Annex-5 areas already present in the register are authoritative seeds for review, not accepted mappings.
area_to_cat={'4.3.1':'4.3.1','4.3.2':'4.3.2','4.3.3':'4.3.3','4.3.4':'4.3.4','4.3.5':'4.3.5','4.3.6':'4.3.6','4.3.7':'4.3.7'}
rows=[]
for t in threats:
    tt=toks(txt(t)); explicit=None
    for ar,c in area_to_cat.items():
        if ar in t.get('annex5_area',''): explicit=c
    candidates=[]
    for n in anodes:
        nt=toks(n['label']+' '+n.get('category_title',''))
        overlap=tt & nt
        union=tt | nt
        score=(len(overlap)/len(union) if union else 0)
        # boost direct lexical signal and exact Annex 5 category declarations in user register
        if explicit and n.get('category_reference')==explicit: score=max(score,0.70)
        if overlap: score += min(0.22,0.055*len(overlap))
        if score>=0.16:
            candidates.append((min(score,0.99),n,sorted(overlap)))
    candidates.sort(key=lambda x:(x[0],len(x[2])),reverse=True)
    # broad Annex area threats deliberately retain all nodes from their declared area; otherwise top 5.
    selected=candidates if explicit else candidates[:5]
    for score,n,overlap in selected:
        rows.append({
          'mapping_id':f"R155-{t['id']}-{n['reference'].replace('.','_')}",
          'threat_id':t['id'],'threat_title':t['title'],
          'regulatory_node_id':n['id'],'regulatory_reference':n['reference'],
          'regulatory_category':n.get('category_reference'),
          'relationship':'related','status':'review',
          'score':round(score,3),
          'basis':'explicit_annex5_area' if explicit and n.get('category_reference')==explicit else 'lexical_seed',
          'matched_terms':overlap,
          'mitigation_nodes':n.get('related_mitigations',[]),
          'rationale':('Threat register explicitly identifies Annex 5 area '+explicit+'.' if explicit and n.get('category_reference')==explicit else 'Candidate generated from overlapping threat-register and Annex 5 terminology; expert review required.'),
          'review':{'decision':None,'reviewed_by':None,'reviewed_at':None,'note':None}
        })
# Ensure every T has a review envelope even if no candidate.
by={t['id']:0 for t in threats}
for x in rows: by[x['threat_id']]+=1
out={'schema_version':'1.0','mapping_set_id':'NCRC-R155-ANNEX5-REVIEW-1','regulation_id':'UN-R155','annex_id':'UN-R155-ANNEX-5',
     'status':'review','method':'explicit Annex 5 area seeds plus conservative lexical candidate generation',
     'source_threat_register':{'file':'register.json','threat_count':len(threats)},
     'regulatory_source':{'file':'annex5.json','part_a_reference_count':len(anodes)},
     'rules':{'accepted_created':False,'legal_compliance_inference':False,'expert_review_required':True},
     'summary':{'mapping_count':len(rows),'threats_with_candidates':sum(v>0 for v in by.values()),'threats_without_candidates':[k for k,v in by.items() if not v]},
     'mappings':rows}
Path('/mnt/data/r155_threat_mappings.json').write_text(json.dumps(out,indent=2,ensure_ascii=False)+'\n')
print(json.dumps(out['summary'],indent=2))
