#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse, html, json, re, sys
from collections import Counter, defaultdict
from datetime import datetime, timezone
from email.utils import parsedate_to_datetime
from pathlib import Path
from typing import Any
from site_navigation import navigation

KIND = 'signal'
TITLE = 'Signals'
INPUT = Path('signals/signals.json')
OUTPUT = Path('site/signals.html')
RECORD_KEYS = ('records','signals','items')
ACTIVE = 'Signals'

def e(v: Any)->str: return html.escape(str(v if v is not None else ""), quote=True)
def clean(v: Any)->str: return re.sub(r"\s+"," ",str(v or "")).strip()
def sf(v: Any)->float:
    try:return float(v)
    except (TypeError,ValueError):return 0.0
def si(v: Any)->int:
    try:return int(v)
    except (TypeError,ValueError):return 0

def date_key(v: Any) -> datetime:
    value = clean(v)

    if not value:
        return datetime.min.replace(tzinfo=timezone.utc)

    try:
        return parsedate_to_datetime(value).astimezone(timezone.utc)
    except (TypeError, ValueError, OverflowError):
        pass

    try:
        dt = datetime.fromisoformat(value.replace("Z", "+00:00"))
        if dt.tzinfo is None:
            dt = dt.replace(tzinfo=timezone.utc)
        return dt.astimezone(timezone.utc)
    except (TypeError, ValueError):
        return datetime.min.replace(tzinfo=timezone.utc)

def truncate_text(v:Any,limit:int=520)->str:
    v=clean(v)
    if len(v)<=limit:return v
    if limit<=3:return v[:limit]
    x=v[:limit-3].rsplit(" ",1)[0].rstrip(" ,;:")
    return (x or v[:limit-3]).rstrip()+"..."
def values(v:Any)->list[str]:
    if isinstance(v,list):return [clean(x) for x in v if clean(x)]
    if isinstance(v,str):return [x.strip() for x in re.split(r"[;,]",v) if x.strip()]
    return []
def load(path:Path)->dict[str,Any]:
    if not path.exists():raise FileNotFoundError(f"Input file not found: {path}")
    with path.open(encoding="utf-8") as f:data=json.load(f)
    if not isinstance(data,dict):raise ValueError(f"Expected JSON object in {path}")
    return data
def records(data:dict[str,Any])->list[dict[str,Any]]:
    for key in RECORD_KEYS:
        v=data.get(key)
        if isinstance(v,list):return [x for x in v if isinstance(x,dict)]
    raise ValueError("No records found; expected one of: "+", ".join(RECORD_KEYS))
def rid(r:dict[str,Any])->str:
    if KIND=="cve":return clean(r.get("name") or r.get("id") or r.get("cve_id")).upper()
    if KIND=="paper":return clean(r.get("id") or r.get("doi") or r.get("arxiv_id") or r.get("title"))
    return clean(r.get("signal_id") or r.get("evidence_id") or r.get("id") or r.get("uuid") or r.get("title"))
def desc(r:dict[str,Any])->str:
    if KIND=="cve":
        out=[]
        for x in r.get("vulnerabilities",[]) or []:
            out.append(clean((x.get("text") or x.get("description")) if isinstance(x,dict) else x))
        return " ".join(x for x in out if x) or clean(r.get("description"))
    if KIND=="paper":return clean(r.get("abstract") or r.get("description"))
    return clean(r.get("summary") or r.get("description") or r.get("abstract") or r.get("narrative") or r.get("text") or r.get("content"))
def title(r:dict[str,Any])->str:
    return rid(r) if KIND=="cve" else clean(r.get("title") or r.get("name") or r.get("headline") or rid(r))
def url(r:dict[str,Any])->str:
    v=clean(r.get("url") or r.get("pdf_url") or r.get("link"))
    if v:return v
    sources=r.get("sources",[])
    if isinstance(sources,list):
        for x in sources:
            if isinstance(x,dict) and clean(x.get("url") or x.get("link")):return clean(x.get("url") or x.get("link"))
    return ""
def meta(r:dict[str,Any])->dict[str,Any]:
    if KIND=="cve":return {"group":clean(r.get("score") or r.get("severity") or "unknown").lower(),"date":clean(r.get("published")),"score":r.get("cvss"),"tags":values(r.get("matched_terms"))+values(r.get("keywords"))+values(r.get("surfaces"))}
    if KIND=="paper":return {"group":clean(r.get("venue") or "unknown"),"date":clean(r.get("year") or r.get("date")),"score":r.get("cited_by"),"tags":values(r.get("topics"))+values(r.get("matched_terms"))+values(r.get("vendors"))+values(r.get("linked_cves"))+values(r.get("cves"))}
    dates=r.get("dates",{}) if isinstance(r.get("dates"),dict) else {}
    return {"group":clean(r.get("signal_type") or r.get("record_type") or r.get("type") or "signal").lower(),"date":clean(dates.get("published") or r.get("published") or r.get("date") or r.get("created_at")),"score":(r.get("evidence",{}).get("confidence_score") if isinstance(r.get("evidence"),dict) else r.get("confidence")),"tags":values(r.get("themes"))+values(r.get("tags"))+values(r.get("signal_types"))+values(r.get("sectors"))+values(r.get("keywords"))}
def correlation_index(data:dict[str,Any])->dict[str,list[dict[str,Any]]]:
    out=defaultdict(list); threats=data.get("threats",{})
    if not isinstance(threats,dict):return out
    for tid,t in threats.items():
        if not isinstance(t,dict):continue
        for item in t.get("evidence",[]) if isinstance(t.get("evidence",[]),list) else []:
            if not isinstance(item,dict) or clean(item.get("evidence_type")).lower()!=KIND:continue
            key=clean(item.get("evidence_id")).casefold()
            if key:out[key].append({"threat":tid,"title":clean(t.get("title")),"status":clean(item.get("status")).lower(),"score":sf(item.get("relevance_score"))})
    for x in out.values():x.sort(key=lambda y:y["score"],reverse=True)
    return out
def nav() -> str:
    return navigation(ACTIVE)
CSS=r''':root{--bg:#eef1f5;--surface:#fff;--surface-alt:#f7f9fb;--hover:#f1f5f8;--ink:#1b2430;--muted:#5d6b7c;--line:#d5dce5;--accent:#1f4e79;--accepted:#2f7d59;--review:#b37a20;--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;--accepted:#58c493;--review:#e0a74d;--kind:#91a7b7}}*{box-sizing:border-box}body{margin:0;padding: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{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;justify-content:flex-start;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}.site-brand:hover{text-decoration:none}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{display:flex;flex-wrap:wrap;gap:8px;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{color:var(--muted);font-size:.78rem}.toolbar{display:flex;justify-content:space-between;align-items:center;gap:12px;margin-bottom:14px}.toolbar h2{margin:0}.filters{display:grid;grid-template-columns:minmax(280px,1fr) repeat(2,minmax(160px,auto));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(360px,1fr));gap:12px}.item{display:flex;flex-direction:column;background:var(--surface-alt);border:1px solid var(--line);border-left:3px solid var(--kind);border-radius:6px;padding:16px;min-width:0}.item:hover{background:var(--hover)}.head{display:flex;justify-content:space-between;gap:12px}.head h3{margin:0 0 5px;font-size:1rem;overflow-wrap:anywhere}.summary{margin:9px 0 12px}.tags,.matches,.meta{display:flex;flex-wrap:wrap;gap:5px}.tag,.match,.badge{padding:3px 7px;border:1px solid var(--line);border-radius:10px;font-size:.68rem}.tag{background:var(--bg);color:var(--muted)}.meta{margin-top:auto;color:var(--muted);font-size:.74rem}.matches{margin-top:11px;padding-top:10px;border-top:1px solid var(--line)}.match.accepted{color:var(--accepted)}.match.review{color:var(--review)}.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}.filters{grid-template-columns:1fr 1fr}}@media(max-width:650px){.site-header{padding:12px 16px}main{padding:18px 12px 34px}.filters,.list{grid-template-columns:1fr}.head,.toolbar{flex-direction:column}}'''
def build(data:dict[str,Any],corr:dict[str,Any],limit:int)->str:
    rows=records(data); idx=correlation_index(corr); enriched=[]; groups=Counter(); mapped=accepted=0
    for r in rows:
        m=meta(r); groups[m["group"]]+=1; matches=idx.get(rid(r).casefold(),[])
        if matches:mapped+=1
        if any(x["status"]=="accepted" for x in matches):accepted+=1
        enriched.append((r,m,matches))
    enriched.sort(key=lambda x: (date_key(x[1]["date"]),sf(x[1]["score"]),),reverse=True,)
    labels={"cve":"Vulnerability evidence","paper":"Academic research","signal":"Strategic signals"}
    out=['<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>',e(TITLE),'</title><style>',CSS,'</style></head><body>',nav(),'<main><section class="hero"><h1>',e(TITLE),'</h1><p class="sub">',e(labels[KIND]),' connected to the Automotive Cybersecurity Threat Register through the evidence correlation pipeline.</p></section><section class="metrics">']
    for value,label in [(len(rows),"Records"),(mapped,"With threat mapping"),(accepted,"With accepted mapping")]:out.append(f'<div class="metric"><strong>{value}</strong><span>{e(label)}</span></div>')
    out+=['</section><section class="card"><div class="toolbar"><h2>',e(labels[KIND]),'</h2><span class="muted small" id="count"></span></div><div class="filters"><input id="q" type="search" placeholder="Search title, description, tags ..."><select id="group"><option value="">All groups</option>', ''.join(f'<option value="{e(g)}">{e(g.replace("_"," ").title())}</option>' for g in sorted(groups,key=str.casefold)),'</select><select id="mapping"><option value="">All mappings</option><option value="accepted">Accepted</option><option value="review">Review</option><option value="unmatched">Unmatched</option></select></div><div class="list">']
    for r,m,matches in enriched:
        name=title(r); summary=truncate_text(desc(r),limit); source=url(r); group=m["group"]; mapping="accepted" if any(x["status"]=="accepted" for x in matches) else ("review" if matches else "unmatched"); tags=[]
        for x in m["tags"]:
            if x.casefold() not in {z.casefold() for z in tags}:tags.append(x)
        hay=" ".join([name,summary,group," ".join(tags)]).lower(); heading=f'<a href="{e(source)}" target="_blank" rel="noopener noreferrer">{e(name)}</a>' if source else e(name)
        match_html=''.join(f'<a class="match {e(x["status"])}" href="threats/{e(x["threat"])}.html#evidence" title="{e(x["title"])}">{e(x["threat"])} · {x["score"]:.2f}</a>' for x in matches[:8]) or '<span class="muted small">No threat mapping above the review threshold</span>'
        out.append(f'<article class="item" data-hay="{e(hay)}" data-group="{e(group)}" data-mapping="{e(mapping)}"><div class="head"><div><h3>{heading}</h3><span class="badge">{e(group or "unknown")}</span></div></div><p class="summary">{e(summary or "No description available.")}</p><div class="tags">'+''.join(f'<span class="tag">{e(x)}</span>' for x in tags[:12])+f'</div><div class="meta"><span>{e(rid(r))}</span>'+(f'<span>{e(m["date"])}</span>' if m["date"] else '')+(f'<span>{e(m["score"])}</span>' if m["score"] not in (None,"") else '')+f'</div><div class="matches">{match_html}</div></article>')
    out+=['</div></section><footer class="footer">Source: ',e(str(INPUT)),'</footer>',r'''<script>const items=[...document.querySelectorAll(".item")],q=document.getElementById("q"),group=document.getElementById("group"),mapping=document.getElementById("mapping");function apply(){const terms=q.value.trim().toLowerCase().split(/\s+/).filter(Boolean);let n=0;items.forEach(x=>{const show=terms.every(t=>x.dataset.hay.includes(t))&&(!group.value||x.dataset.group===group.value)&&(!mapping.value||x.dataset.mapping===mapping.value);x.style.display=show?"":"none";if(show)n++});count.textContent="Showing "+n+" of "+items.length+" records"}q.addEventListener("input",apply);group.addEventListener("input",apply);mapping.addEventListener("input",apply);apply();</script>''','</main></body></html>']
    return ''.join(out)
def main()->int:
    p=argparse.ArgumentParser();p.add_argument("--input",type=Path,default=INPUT);p.add_argument("--correlations",type=Path,default=Path("evidence/threat_correlations.json"));p.add_argument("--output",type=Path,default=OUTPUT);p.add_argument("--text-limit",type=int,default=520);a=p.parse_args()
    try:
        page=build(load(a.input),load(a.correlations),a.text_limit);a.output.parent.mkdir(parents=True,exist_ok=True);a.output.write_text(page,encoding="utf-8");print(f"Wrote {a.output}");return 0
    except (FileNotFoundError,ValueError,OSError,json.JSONDecodeError) as exc:print(f"ERROR: {exc}",file=sys.stderr);return 2
if __name__=="__main__":sys.exit(main())
