#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
signal_detector.py

NCRC Strategic Signal Detector

Collects RSS feeds and identifies strategic cyber,
AI, infrastructure and geopolitical signals.

Output:
    signals/signals.json
"""

import hashlib
import json
import logging
import re
import time
from collections import Counter
from datetime import datetime, timezone
from pathlib import Path

import feedparser
import requests

# ============================================================
# CONFIG
# ============================================================

OUTPUT_FILE = "signals/signals.json"

CACHE_DIR = Path("cache")
CACHE_DIR.mkdir(exist_ok=True)

LOG_DIR = Path("logs")
LOG_DIR.mkdir(exist_ok=True)

CACHE_FILE = CACHE_DIR / "signals_cache.json"

CACHE_TTL_HOURS = 6

HEADERS = {
    "User-Agent": "NCRC-Signal-Detector/1.0"
}

TIMEOUT = 30


RSS_FEEDS = [

    (
        "NCSC",
        "https://advisories.ncsc.nl/rss/advisories"
    ),

    (
        "CISA News",
        "https://www.cisa.gov/news.xml"
    ),

    (
        "CISA Advisories",
        "https://www.cisa.gov/cybersecurity-advisories/all.xml"
    ),

    (
        "NCSC UK",
        "https://www.ncsc.gov.uk/api/1/services/v1/all-rss-feed.xml"
    ),

    (
        "The Hacker News",
        "https://feeds.feedburner.com/TheHackersNews"
    ),

    (
        "Bleeping Computer",
        "https://www.bleepingcomputer.com/feed/"
    ),

    (
        "Krebs",
        "https://krebsonsecurity.com/feed/"
    ),
    (   "Bloomberg",
        "https://news.google.com/rss/search?q=when:24h+site:bloomberg.com&hl=en-US&gl=US&ceid=US:en"
    )
]



# ============================================================
# SOURCE WEIGHTS
# ============================================================

SOURCE_WEIGHT = {

    # Overheidsbronnen krijgen meer gewicht

    "NCSC": 5,

    "CISA News": 4,

    "CISA Advisories": 5,

    "NCSC UK": 5,

    # Specialistische media

    "Krebs": 2,

    "The Hacker News": 2,

    "Bleeping Computer": 2,

    # default
    "Unknown": 1
}


# ============================================================
# SOURCE TYPES
# ============================================================
SOURCE_TYPES = {

    "NCSC": "Government",
    "NCSC UK": "Government",
    "CISA News": "Government",
    "CISA Advisories": "Government",

    "The Hacker News": "Threat Intelligence",
    "Bleeping Computer": "Threat Intelligence",
    "Krebs": "Threat Intelligence",

    "Bloomberg": "Strategic Media",
    "Financial Times": "Strategic Media",
    "The Economist": "Strategic Media",

    "Unknown": "Other"
}




# ============================================================
# SIGNAL RULES
# ============================================================


SIGNAL_RULES = {

    "AI": [
        "artificial intelligence",
        "generative ai",
        "agentic ai",
        "llm",
        "large language model"
    ],

    "Critical Infrastructure": [
        "critical infrastructure",
        "power grid",
        "energy",
        "water utility",
        "telecommunications"
    ],

    "Geopolitics": [
        "china",
        "russia",
        "iran",
        "north korea",
        "nation state",
        "state actor",
        "geopolitical"
    ],

    "Supply Chain": [
        "supply chain",
        "dependency",
        "sbom",
        "third party"
    ],

    "Governance": [
        "nis2",
        "cyber resilience act",
        "ai act",
        "regulation",
        "governance",
        "compliance",
        "policy"
    ],

    "Digital Sovereignty": [
        "digital sovereignty",
        "technological sovereignty",
        "national capability"
    ],

    "Automotive": [
        "vehicle",
        "connected vehicle",
        "v2x",
        "adas",
        "ads", 
        "dcas",
        "iso 21434",
        "unece r155"
    ],

    "Transportation": [
        "aviation",
        "airline",
        "airport",
        "railway",
        "rail transport",
        "maritime",
        "shipping",
        "port authority",
        "ev charging"
    ]
}



STRATEGIC_KEYWORDS = {

    "critical infrastructure": 3,
    "nation state": 3,
    "artificial intelligence": 2,
    "generative ai": 2,
    "supply chain": 2,
    "transportation": 2,
    "aviation": 2,
    "automotive": 2,
    "autonomous": 2,
    "cyber warfare": 3,
    "energy": 2,
    "geopolitical": 2,
    "satellite": 2,
    "gps": 2,
    "quantum": 2,
    "resilience": 1,
    "sovereignty": 2,

    "cyber resilience act": 3,
    "nis2": 3,
    "critical entities": 3,
    "software supply chain": 3,
    "systemic risk": 3,
    "digital sovereignty": 3,
    "transportation infrastructure": 3,
    "critical transport": 3,
    "autonomous systems": 3,
    "ai-assisted": 3,
    "llm": 2,
    "agentic ai": 3,
    "transport": 2,
    "rail": 2,
    "maritime": 2,

    "unece": 3,
    "r155": 3,
    "iso 21434": 3,

    "connected vehicle": 3,
    "connected vehicles": 3,
    "v2x": 3,
    "vehicle-to-everything": 3,

    "electric vehicle": 2,
    "ev charging": 3,
    "ocpp": 3,

    "cloud dependency": 3,
    "concentration risk": 3,

    "critical software": 2,
    "critical systems": 3
}




# ============================================================
# LOGGING
# ============================================================

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(message)s",
    handlers=[
        logging.FileHandler(LOG_DIR / "signals.log"),
        logging.StreamHandler()
    ]
)

logger = logging.getLogger("signals")

# ============================================================
# CACHE
# ============================================================

def load_cache():

    if not CACHE_FILE.exists():
        return {}

    try:
        return json.loads(
            CACHE_FILE.read_text(encoding="utf-8")
        )
    except Exception:
        return {}


def save_cache(cache):

    CACHE_FILE.write_text(
        json.dumps(cache),
        encoding="utf-8"
    )

# ============================================================
# HTTP
# ============================================================

def fetch_url(url):

    try:

        r = requests.get(
            url,
            timeout=TIMEOUT,
            headers=HEADERS
        )

        if r.status_code == 200:
            return r.text

        logger.warning(
            "HTTP %s %s",
            r.status_code,
            url
        )

    except Exception as e:

        logger.warning(
            "Fetch failed %s (%s)",
            url,
            e
        )

    return None

# ============================================================
#  Strategic Relevance
# ============================================================
def strategic_relevance(categories):

    score = 0

    weights = {

        "AI": 3,

        "Critical Infrastructure": 4,

        "Geopolitics": 4,

        "Supply Chain": 3,

        "Governance": 2,

        "Digital Sovereignty": 3,

        "Transportation": 2,

        "Automotive": 2
    }

    for cat in categories:
        score += weights.get(
            cat,
            1
        )

    return round(
        min(score, 10),
        1
    )



# ============================================================
# RSS COLLECTION
# ============================================================

def collect_feed(source, url):

    logger.info("Fetching %s", source)

    txt = fetch_url(url)

    if not txt:
        return []

    feed = feedparser.parse(txt)

    records = []

    for entry in feed.entries:

        records.append({

            "source": source,

            "title":
                getattr(entry, "title", "").strip(),

            "summary":
                getattr(entry, "summary", "").strip(),

            "url":
                getattr(entry, "link", ""),

            "published":
                getattr(entry, "published", "")
        })

    return records

# ============================================================
# RELEVANCE
# ============================================================

def classify_signal(text):

    text = text.lower()

    matches = []

    for category, terms in SIGNAL_RULES.items():

        for term in terms:

            if term.lower() in text:
                matches.append(category)
                break

    return sorted(set(matches))


def signal_strength(text, source):

    text = text.lower()

    # startscore vanuit de bron
    score = SOURCE_WEIGHT.get(source, 1)

    matches = 0

    for term, weight in STRATEGIC_KEYWORDS.items():

        if term in text:

            score += weight
            matches += 1

    # bonus voor meerdere signalen in één artikel
    score += min(matches * 0.5, 2.0)

    return round(min(score, 10), 1)


def priority(score):

    if score >= 8:
        return "critical"

    if score >= 6:
        return "high"

    if score >= 4:
        return "medium"

    return "low"

# ============================================================
# DEDUPLICATION
# ============================================================

def title_key(title):

    return re.sub(
        r"[^a-z0-9]",
        "",
        title.lower()
    )


def dedupe(records):

    seen = set()

    out = []

    for r in records:

        key = title_key(r["title"])

        if key in seen:
            continue

        seen.add(key)

        out.append(r)

    return out

# ============================================================
# PROCESSING
# ============================================================


def determine_theme(categories):

    priority = [

        "Critical Infrastructure",
        "AI",
        "Transportation",
        "Supply Chain",
        "Governance",
        "Geopolitics",
        "Autonomous Systems"
    ]

    for p in priority:

        if p in categories:
            return p

    return "Other"


def title_key(title):

    return re.sub(
        r"[^a-z0-9]",
        "",
        title.lower()
    )


def signal_strength(text, source):

    text = text.lower()

    score = SOURCE_WEIGHT.get(source, 1)

    matches = 0

    for term, weight in STRATEGIC_KEYWORDS.items():

        if term in text:
            score += weight
            matches += 1

    score += min(matches * 0.5, 2.0)

    return round(min(score, 10), 1)


def build_record(item):

    text = f"{item['title']} {item['summary']}"

    categories = classify_signal(text)

    if not categories:
        return None

    signal_score = signal_strength(
        text,
        item["source"]
    )

    if signal_score < 4:
        return None

    primary_theme = determine_theme(
        categories
    )

    relevance = strategic_relevance(
        categories
    )

    tags = narrative_tags(
        categories
    )

    summary = re.sub(
        r"<[^>]+>",
        "",
        item["summary"]
    ).strip()

    rec_id = hashlib.sha1(
        item["title"].encode()
    ).hexdigest()[:12]

    return {

        # ====================================================
        # Identity
        # ====================================================

        "id":
            rec_id,

        "title":
            item["title"],

        "url":
            item["url"],

        "date":
            item["published"],

        # ====================================================
        # Source
        # ====================================================

        "source":
            item["source"],

        "source_type":
            SOURCE_TYPES.get(
                item["source"],
                "Other"
            ),

        # ====================================================
        # Content
        # ====================================================

        "summary":
            summary,

        # ====================================================
        # Signal Scoring
        # ====================================================

        "signal_strength":
            signal_score,

        "strategic_relevance":
            relevance,

        "priority":
            priority(signal_score),

        # ====================================================
        # Classification
        # ====================================================

        "theme":
            primary_theme,

        "themes":
            categories,

        # ====================================================
        # Narrative Support
        # ====================================================

        "narrative_tags":
            tags,

        # ====================================================
        # Future Correlation
        # ====================================================

        "related_papers": 0,

        "related_cves": 0,

        "related_repositories": 0,

        # ====================================================
        # Metadata
        # ====================================================

        "collected_at":
            datetime.now(
                timezone.utc
            ).isoformat()
    }




def narrative_tags(categories):

    tags = []

    if "AI" in categories:
        tags.append(
            "AI-Powered Cyber Operations"
        )

    if (
        "AI" in categories and
        "Critical Infrastructure" in categories
    ):
        tags.append(
            "AI & Critical Infrastructure"
        )

    if "Geopolitics" in categories:
        tags.append(
            "Cyber as Geopolitical Instrument"
        )

    if "Supply Chain" in categories:
        tags.append(
            "Supply Chain Fragility"
        )

    if "Transportation" in categories:
        tags.append(
            "Transportation Digital Risk"
        )

    if "Governance" in categories:
        tags.append(
            "Regulation & Resilience"
        )

    if "Digital Sovereignty" in categories:
        tags.append(
            "Digital Sovereignty"
        )

    return tags



def collect_signals():

    raw = []

    for source, url in RSS_FEEDS:

        raw.extend(
            collect_feed(source, url)
        )

        time.sleep(1)

    logger.info(
        "Collected %s raw articles",
        len(raw)
    )

    raw = dedupe(raw)

    records = []

    for item in raw:

        rec = build_record(item)

        if rec:
            records.append(rec)

    records.sort(
        key=lambda x: x["signal_strength"],
        reverse=True
    )

    return records


def main():

    records = collect_signals()

    # ============================================================
    # TOP SIGNALS
    # ============================================================

    top_signals = sorted(
        records,
        key=lambda r: r["signal_strength"],
        reverse=True
    )[:10]

    # ============================================================
    # STRATEGIC ALERTS
    # ============================================================

    strategic_alerts = sorted(
        [
            r
            for r in records
            if (
                r["signal_strength"] >= 8
                or r["priority"] == "critical"
            )
        ],
        key=lambda r: r["signal_strength"],
        reverse=True
    )[:25]

    summary = {

        "total_signals":
            len(records),

        "critical":
            sum(
                r["priority"] == "critical"
                for r in records
            ),

        "high":
            sum(
                r["priority"] == "high"
                for r in records
            ),


        "ai_related":
            sum(
                "AI" in r.get("themes", [])
                for r in records
            ),

        "geopolitical":
            sum(
                "Geopolitics" in r.get("themes", [])
                for r in records
            ),

        "transport_related":
            sum(
                "Transportation" in r.get("themes", [])
                for r in records
            ),


        "government_sources":
            sum(
                r["source"] in [
                    "NCSCA",
                    "CISA News",
                    "CISA Advisories",
                    "NCSC UK"
                ]
                for r in records
            ),

        "themes": dict(
            Counter(
                r["theme"]
                for r in records
            )
        )
    }

    output = {

        "module": "signals",

        "generated":
            time.time(),

        "generated_iso":
            datetime.now(
                timezone.utc
            ).isoformat(),

        "summary":
            summary,

        "strategic_alerts":
            strategic_alerts,

        "top_signals":
            top_signals,

        "records":
            records
    }

    Path("signals").mkdir(
        exist_ok=True
    )

    with open(
        OUTPUT_FILE,
        "w",
        encoding="utf-8"
    ) as f:

        json.dump(
            output,
            f,
            indent=2,
            ensure_ascii=False
        )

    print(
        f"[SIGNALS] Wrote "
        f"{len(records)} signals"
    )

    print(
        f"[SIGNALS] Strategic alerts: "
        f"{len(strategic_alerts)}"
    )

    print(
        f"[SIGNALS] Top signals: "
        f"{len(top_signals)}"
    )


if __name__ == "__main__":
    main()
