#!/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 = [

    (
        "ENISA",
        "https://www.enisa.europa.eu/news/rss.xml"
    ),

    (
        "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/"
    )
]



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

SOURCE_WEIGHT = {

    # Overheidsbronnen krijgen meer gewicht

    "ENISA": 5,

    "CISA News": 4,

    "CISA Advisories": 5,

    "NCSC UK": 5,

    # Specialistische media

    "Krebs": 2,

    "The Hacker News": 2,

    "Bleeping Computer": 2,

    # default
    "Unknown": 1
}

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

SIGNAL_RULES = {

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

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

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

    "Transportation": [
        "vehicle",
        "automotive",
        "aviation",
        "aircraft",
        "rail",
        "maritime",
        "port"
    ],

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

    "Autonomous Systems": [
        "autonomous",
        "robotics",
        "self-driving",
        "adas"
    ],
   
    "Governance": [
        "regulation",
        "compliance",
        "cyber resilience act",
        "nis2",
        "critical entities",
        "governance",
        "policy",
        "standardization",
        "iso 21434",
        "unece",
        "r155"
    ],

}


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

# ============================================================
# 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
# ============================================================
