#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Build NCRC narrative overview and drill-down pages from signals and incidents."""
from __future__ import annotations

import argparse, hashlib, html, json, re, shutil, sys, time
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
from site_navigation import navigation as site_navigation

SIGNALS_FILE = Path("signals/signals.json")
INCIDENTS_FILE = Path("incidents/incidents.json")
INCIDENTS_RAW_DIR = Path("incidents/raw")
OUTPUT_FILE = Path("narratives/narratives.json")
HTML_FILE = Path("site/narratives.html")
DETAIL_DIR = Path("site/narratives")
DEFAULT_LIMIT = 10

NARRATIVES = {
    "AI-Powered Cyber Operations": {"keywords":["AI","Artificial Intelligence","Autonomous Systems","Machine Learning"],"theme":"AI","summary":"Artificial intelligence is increasingly changing offensive and defensive cybersecurity capabilities."},
    "Critical Infrastructure Under Pressure": {"keywords":["Critical Infrastructure","Infrastructure","Energy","Charging Infrastructure","EVSE"],"theme":"Critical Infrastructure","summary":"Critical infrastructure is becoming a growing target for cyber threats and disruption campaigns."},
    "Cyber as Geopolitical Instrument": {"keywords":["Geopolitics","State Actor","Nation State","Espionage","Sabotage"],"theme":"Geopolitics","summary":"Cyber activity is increasingly being used to achieve geopolitical and economic objectives."},
    "Transportation Digital Risk": {"keywords":["Transportation","Automotive","Vehicle","Connected Vehicle","Mobility","EV"],"theme":"Transportation","summary":"Transportation ecosystems are becoming more connected and therefore exposed to systemic cyber risks."},
    "Supply Chain Fragility": {"keywords":["Supply Chain","Supplier","Third Party","Dependency","Software Component"],"theme":"Supply Chain","summary":"Supply-chain dependencies continue to introduce systemic cyber risk."},
    "Governance and Regulation": {"keywords":["Governance","Regulation","Compliance","UN R155","UN R156","CRA","AI Act"],"theme":"Governance","summary":"Regulatory frameworks are evolving to address increasing cyber and digital resilience challenges."},
}
CSS=r''':root{--bg:#eef1f5;--surface:#fff;--surface-alt:#f7f9fb;--hover:#f1f5f8;--ink:#1b2430;--muted:#5d6b7c;--line:#d5dce5;--accent:#1f4e79;--kind:#526b7d}@media(prefers-color-scheme:dark){:root{--bg:#12171e;--surface:#1a212b;--surface-alt:#151c25;--hover:#202a36;--ink:#e6ebf1;--muted:#93a1b3;--line:#2c3644;--accent:#7db4e6;--kind:#91a7b7}}*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--ink);font:16px/1.5 "Segoe UI",system-ui,sans-serif}a{color:var(--accent);text-decoration:none}a:hover{text-decoration:underline}h1,h2,h3,h4{margin-top:0}.site-header{display:flex;align-items:center;justify-content:space-between;gap:24px;min-height:68px;padding:12px 28px;background:var(--surface);border-bottom:1px solid var(--line)}.site-nav{display:flex;flex-wrap:wrap;gap:4px}.site-nav-link{color:var(--muted);padding:7px 10px;border-radius:5px;font-size:.84rem;font-weight:600}.site-nav-link:hover{color:var(--ink);background:var(--surface-alt);text-decoration:none}.site-nav-link.active{color:var(--accent);background:color-mix(in srgb,var(--accent) 12%,transparent)}.site-brand{margin-left:auto;color:var(--ink);font-weight:700;white-space:nowrap}main{max-width:1240px;margin:auto;padding:26px 24px 44px}.hero,.card{background:var(--surface);border:1px solid var(--line);border-radius:7px;padding:22px;margin-bottom:20px}.hero h1{margin-bottom:6px}.sub,.muted{color:var(--muted)}.small{font-size:.82rem}.metrics,.tags,.meta{display:flex;flex-wrap:wrap;gap:8px}.metrics{margin-bottom:20px}.metric{display:flex;align-items:baseline;gap:7px;padding:9px 12px;background:var(--surface);border:1px solid var(--line);border-radius:6px}.metric strong{font-size:1.15rem}.metric span,.meta{color:var(--muted);font-size:.78rem}.toolbar{display:flex;justify-content:space-between;gap:12px;margin-bottom:14px}.filters{display:grid;grid-template-columns:1fr 220px;gap:10px;margin-bottom:16px}.filters input,.filters select{width:100%;background:var(--bg);color:var(--ink);border:1px solid var(--line);border-radius:5px;padding:8px 10px;font:inherit}.list{display:grid;grid-template-columns:repeat(auto-fit,minmax(340px,1fr));gap:12px}.item,.evidence-item{background:var(--surface-alt);border:1px solid var(--line);border-left:3px solid var(--kind);border-radius:6px;padding:16px}.item{display:flex;flex-direction:column;min-height:270px}.head{display:flex;justify-content:space-between;gap:12px}.head h3{margin:0 0 5px;font-size:1rem}.summary{margin:12px 0}.tag,.badge{padding:3px 7px;border:1px solid var(--line);border-radius:10px;font-size:.68rem}.tag{color:var(--muted)}.meta{margin-top:auto;padding-top:12px}.detail-link{display:inline-flex;margin-top:14px;padding:7px 10px;border:1px solid var(--line);border-radius:5px;font-size:.78rem;font-weight:700}.detail-list{display:grid;gap:10px}.evidence-item h3{margin-bottom:6px}.record-summary{margin:8px 0;color:var(--muted)}.back{display:inline-block;margin-bottom:15px}.footer{margin-top:28px;color:var(--muted);font-size:.82rem}@media(max-width:900px){.site-header{align-items:flex-start;flex-direction:column-reverse}.site-brand{margin-left:0}}@media(max-width:650px){.site-header{padding:12px 16px}main{padding:18px 12px}.filters,.list{grid-template-columns:1fr}.head,.toolbar{flex-direction:column}}'''

def e(v:Any)->str:return html.escape(str(v if v is not None else ""),quote=True)
def num(v:Any,d:float=0.0)->float:
    try:return float(v)
    except (TypeError,ValueError):return d

def extract(data:Any,keys:tuple[str,...],source:Path)->list[dict[str,Any]]:
    if isinstance(data,list):return [x for x in data if isinstance(x,dict)]
    if isinstance(data,dict):
        for k in keys:
            if isinstance(data.get(k),list):return [x for x in data[k] if isinstance(x,dict)]
        if any(k in data for k in ("incident_id","signal_id","id","title","name","headline")):return [data]
    raise ValueError(f"No supported record structure in {source}")
def load_file(path:Path,keys:tuple[str,...],required:bool=False)->list[dict[str,Any]]:
    if not path.exists():
        if required:raise FileNotFoundError(f"Input file not found: {path}")
        return []
    with path.open(encoding="utf-8") as f:return extract(json.load(f),keys,path)
def title(r:dict[str,Any])->str:return str(r.get("title") or r.get("name") or r.get("headline") or r.get("incident_id") or r.get("signal_id") or r.get("id") or "Untitled record")
def source(r:dict[str,Any])->str:
    v=r.get("source") or r.get("publisher") or r.get("provider") or r.get("feed") or ""
    return str(v.get("name") or v.get("title") or v.get("url") or "") if isinstance(v,dict) else str(v)
def date(r:dict[str,Any])->str:return str(r.get("date") or r.get("published") or r.get("occurred_at") or r.get("created_at") or "")
def url(r:dict[str,Any])->str:
    v=r.get("url") or r.get("link") or r.get("source_url") or ""
    if not v and isinstance(r.get("source"),dict):v=r["source"].get("url") or r["source"].get("link") or ""
    return str(v)
def description(r:dict[str,Any])->str:return str(r.get("summary") or r.get("description") or r.get("narrative") or r.get("text") or r.get("content") or "")
def texts(v:Any)->list[str]:
    if isinstance(v,str):return [v]
    if isinstance(v,list):return [s for x in v for s in texts(x)]
    if isinstance(v,dict):return [s for x in v.values() for s in texts(x)]
    return []
def searchable(r:dict[str,Any])->str:
    fields=("title","name","headline","summary","description","narrative","text","content","themes","narrative_tags","tags","keywords","sectors","categories","incident_type","affected_assets","attack_vectors")
    return " ".join(s for f in fields for s in texts(r.get(f))).casefold()
def matches(r:dict[str,Any],d:dict[str,Any])->bool:
    h=searchable(r);return any(re.search(r"(?<!\w)"+re.escape(k.casefold())+r"(?!\w)",h) for k in d["keywords"])
def fingerprint(r:dict[str,Any])->str:
    x=r.get("incident_id") or r.get("id") or r.get("uuid") or r.get("evidence_id")
    if x:return "id:"+str(x).strip().casefold()
    return "fp:"+hashlib.sha1("|".join((title(r).casefold(),date(r).casefold(),source(r).casefold())).encode()).hexdigest()
def dedupe(rows:list[dict[str,Any]])->list[dict[str,Any]]:
    out={}
    for r in rows:
        k=fingerprint(r)
        if k not in out:out[k]=r
        else:
            m=dict(out[k])
            for f,v in r.items():
                if f not in m or m[f] in (None,"",[],{}):m[f]=v
            out[k]=m
    return list(out.values())
def load_incidents(main:Path,raw:Path)->tuple[list[dict[str,Any]],list[str]]:
    rows=[];files=[]
    if main.exists():rows+=load_file(main,("records","incidents","items"));files.append(str(main))
    if raw.exists():
        for p in sorted(raw.rglob("*.json")):
            try:rows+=load_file(p,("records","incidents","items"));files.append(str(p))
            except (OSError,ValueError,json.JSONDecodeError) as ex:print(f"[WARNING] Skipping {p}: {ex}",file=sys.stderr)
    return dedupe(rows),files
def signal_score(r:dict[str,Any])->float:return max(0,min(num(r.get("signal_strength",r.get("confidence",0))),10))
def incident_score(r:dict[str,Any])->float:
    for k in ("incident_strength","severity_score","risk_score","confidence_score","confidence","score"):
        if r.get(k) not in (None,""):
            v=num(r[k]);v=v*10 if 0<=v<=1 else v/10 if 10<v<=100 else v
            return max(0,min(v,10))
    return {"critical":9.5,"high":8,"medium":6,"moderate":6,"low":3.5}.get(str(r.get("severity") or r.get("risk_level") or r.get("impact") or "").casefold(),5)
def confidence(ss:list[dict[str,Any]],ii:list[dict[str,Any]])->float:
    sa=sum(map(signal_score,ss))/len(ss) if ss else 0;ia=sum(map(incident_score,ii))/len(ii) if ii else 0
    base=sa*.65+ia*.35 if ss and ii else sa or ia;sources={source(x) for x in ss+ii if source(x)}
    return round(min(base+min(len(ss)*.08+len(ii)*.18,1.2)+min(len(sources)*.12,.6),10),1)
def compact(r:dict[str,Any],kind:str)->dict[str,Any]:
    return {"id":str(r.get("signal_id") or r.get("incident_id") or r.get("evidence_id") or r.get("id") or fingerprint(r)),"title":title(r),"source":source(r),"date":date(r),"url":url(r),"summary":description(r),"strength":signal_score(r) if kind=="signal" else incident_score(r)}
def build_narratives(signals:list[dict[str,Any]],incidents:list[dict[str,Any]])->list[dict[str,Any]]:
    out=[]
    for name,d in NARRATIVES.items():
        ss=[x for x in signals if matches(x,d)];ii=[x for x in incidents if matches(x,d)]
        if not ss and not ii:continue
        score=confidence(ss,ii);nid=hashlib.sha1(name.encode()).hexdigest()[:12]
        out.append({"id":nid,"slug":nid,"title":name,"theme":d["theme"],"summary":d["summary"],"confidence":score,"trend":"accelerating" if score>=8 else "active" if score>=6 else "emerging","signals":len(ss),"incidents":len(ii),"sources":sorted({source(x) for x in ss+ii if source(x)},key=str.casefold),"signal_records":[compact(x,"signal") for x in sorted(ss,key=signal_score,reverse=True)],"incident_records":[compact(x,"incident") for x in sorted(ii,key=incident_score,reverse=True)]})
    return sorted(out,key=lambda x:(x["confidence"],x["incidents"],x["signals"]),reverse=True)
def nav(prefix: str = "") -> str:
    return site_navigation("Narratives", prefix)

def record_html(r:dict[str,Any])->str:
    heading=f'<a href="{e(r["url"])}" target="_blank" rel="noopener noreferrer">{e(r["title"])}</a>' if r["url"] else e(r["title"])
    summary=f'<p class="record-summary">{e(r["summary"])}</p>' if r["summary"] else ""
    meta="".join(f'<span>{e(x)}</span>' for x in (r["source"],r["date"],f'Strength {r["strength"]}/10') if x)
    return f'<article class="evidence-item" data-hay="{e((r["title"]+" "+r["source"]+" "+r["summary"]).casefold())}"><h3>{heading}</h3>{summary}<div class="meta">{meta}</div></article>'
def detail_page(n:dict[str,Any])->str:
    metrics="".join(f'<div class="metric"><strong>{e(v)}</strong><span>{e(l)}</span></div>' for v,l in ((n["confidence"],"Confidence / 10"),(n["trend"],"Trend"),(n["signals"],"Signals"),(n["incidents"],"Incidents")))
    signals="".join(record_html(x) for x in n["signal_records"]) or '<p class="muted">No supporting signals.</p>'
    incidents="".join(record_html(x) for x in n["incident_records"]) or '<p class="muted">No supporting incidents.</p>'
    script=r'''<script>const q=document.getElementById("q"),items=[...document.querySelectorAll(".evidence-item")],count=document.getElementById("count");function apply(){const ts=q.value.trim().toLowerCase().split(/\s+/).filter(Boolean);let n=0;items.forEach(x=>{const show=ts.every(t=>x.dataset.hay.includes(t));x.style.display=show?"":"none";if(show)n++});count.textContent="Showing "+n+" of "+items.length+" records"}q.addEventListener("input",apply);apply();</script>'''
    return f'<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>{e(n["title"])}</title><style>{CSS}</style></head><body>{nav("../")}<main><a class="back" href="../narratives.html">← Back to narratives</a><section class="hero"><h1>{e(n["title"])}</h1><p class="sub">{e(n["summary"])}</p></section><section class="metrics">{metrics}</section><section class="card"><div class="toolbar"><h2>Supporting evidence</h2><span class="muted small" id="count"></span></div><div class="filters"><input id="q" type="search" placeholder="Search all supporting signals and incidents ..."></div></section><section class="card"><h2>Signals ({n["signals"]})</h2><div class="detail-list">{signals}</div></section><section class="card"><h2>Incidents ({n["incidents"]})</h2><div class="detail-list">{incidents}</div></section><footer class="footer">Narrative ID: {e(n["id"])}</footer>{script}</main></body></html>'
def overview(narratives:list[dict[str,Any]],signal_count:int,incident_count:int,limit:int)->str:
    selected=narratives[:max(limit,0)];cards=[]
    for n in selected:
        tags="".join(f'<span class="tag">{e(x)}</span>' for x in n["sources"][:8]);hay=" ".join((n["title"],n["theme"],n["summary"],*n["sources"])).casefold()
        cards.append(f'<article class="item" data-hay="{e(hay)}" data-theme="{e(n["theme"])}"><div class="head"><div><h3>{e(n["title"])}</h3><span class="badge">{e(n["theme"])}</span></div><span class="badge">{e(n["trend"])}</span></div><p class="summary">{e(n["summary"])}</p><div class="tags">{tags}</div><div class="meta"><span>Confidence {n["confidence"]}/10</span><span>{n["signals"]} signals</span><span>{n["incidents"]} incidents</span></div><a class="detail-link" href="narratives/{e(n["slug"])}.html">View narrative →</a></article>')
    metrics="".join(f'<div class="metric"><strong>{v}</strong><span>{e(l)}</span></div>' for v,l in ((len(narratives),"Narratives"),(signal_count,"Signals analysed"),(incident_count,"Unique incidents analysed"),(len(selected),"Shown")))
    options="".join(f'<option value="{e(x)}">{e(x)}</option>' for x in sorted({n["theme"] for n in selected},key=str.casefold))
    script=r'''<script>const items=[...document.querySelectorAll(".item")],q=document.getElementById("q"),theme=document.getElementById("theme"),count=document.getElementById("count");function apply(){const ts=q.value.trim().toLowerCase().split(/\s+/).filter(Boolean);let n=0;items.forEach(x=>{const show=ts.every(t=>x.dataset.hay.includes(t))&&(!theme.value||x.dataset.theme===theme.value);x.style.display=show?"":"none";if(show)n++});count.textContent="Showing "+n+" of "+items.length+" narratives"}q.addEventListener("input",apply);theme.addEventListener("input",apply);apply();</script>'''
    return f'<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Narratives</title><style>{CSS}</style></head><body>{nav()}<main><section class="hero"><h1>Narratives</h1><p class="sub">Strategic narratives derived from signals and incidents in the NCRC intelligence pipeline.</p></section><section class="metrics">{metrics}</section><section class="card"><div class="toolbar"><h2>Strategic narratives</h2><span class="muted small" id="count"></span></div><div class="filters"><input id="q" type="search" placeholder="Search title, summary, theme ..."><select id="theme"><option value="">All themes</option>{options}</select></div><div class="list">{"".join(cards)}</div></section><footer class="footer">Select a narrative to view all supporting signals and incidents.</footer>{script}</main></body></html>'
def main()->int:
    p=argparse.ArgumentParser();p.add_argument("--signals",type=Path,default=SIGNALS_FILE);p.add_argument("--incidents",type=Path,default=INCIDENTS_FILE);p.add_argument("--incidents-raw",type=Path,default=INCIDENTS_RAW_DIR);p.add_argument("--output",type=Path,default=OUTPUT_FILE);p.add_argument("--html-output",type=Path,default=HTML_FILE);p.add_argument("--detail-dir",type=Path,default=DETAIL_DIR);p.add_argument("--limit",type=int,default=DEFAULT_LIMIT);a=p.parse_args()
    try:
        signals=load_file(a.signals,("records","signals","items"),True);incidents,files=load_incidents(a.incidents,a.incidents_raw);narratives=build_narratives(signals,incidents)
        data={"module":"narratives","generated":time.time(),"generated_iso":datetime.now(timezone.utc).isoformat(),"inputs":{"signals":str(a.signals),"incident_files":files},"summary":{"narratives":len(narratives),"signals":len(signals),"incidents_unique":len(incidents),"incident_files":len(files),"high_confidence":sum(1 for n in narratives if n["confidence"]>=8)},"narratives":narratives}
        a.output.parent.mkdir(parents=True,exist_ok=True);a.output.write_text(json.dumps(data,indent=2,ensure_ascii=False),encoding="utf-8")
        a.html_output.parent.mkdir(parents=True,exist_ok=True);a.html_output.write_text(overview(narratives,len(signals),len(incidents),a.limit),encoding="utf-8")
        if a.detail_dir.exists():shutil.rmtree(a.detail_dir)
        a.detail_dir.mkdir(parents=True,exist_ok=True)
        for n in narratives:(a.detail_dir/f'{n["slug"]}.html').write_text(detail_page(n),encoding="utf-8")
        print(f"[NARRATIVES] {len(signals)} signals and {len(incidents)} unique incidents loaded from {len(files)} incident files")
        print(f"[NARRATIVES] {len(narratives)} narratives and {len(narratives)} detail pages written")
        return 0
    except (FileNotFoundError,ValueError,OSError,json.JSONDecodeError) as ex:print(f"[ERROR] {ex}",file=sys.stderr);return 2
if __name__=="__main__":raise SystemExit(main())
