#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
feeds_automotive.py - Automotive Threat Intelligence Collector (NVD)

Changes vs. previous version:
  * Pagination (totalResults / startIndex) - nothing is silently cut off at page 1
  * keywordExactMatch so "can bus" is searched as a phrase, not can AND bus
  * Proper URL encoding
  * NVD rate limits respected (6.5 s without API key, 0.7 s with NVD_API_KEY env var)
  * Backoff on 403/429/503 (NVD answers 403 when rate limited)
  * Cache has a TTL (otherwise new CVEs never show up) and partial results are never cached
  * Extra queries on OEM / Tier-1 vendor names, not only protocol keywords
"""

import json
import logging
import os
import time
from pathlib import Path
from urllib.parse import quote

import requests

# ============================================================
# CONFIG
# ============================================================
TIMEOUT = 30
NVD_URL = "https://services.nvd.nist.gov/rest/json/cves/2.0"
NVD_API_KEY = os.getenv("NVD_API_KEY")          # free key: https://nvd.nist.gov/developers/request-an-api-key
REQUEST_DELAY = 0.7 if NVD_API_KEY else 6.5      # 50 req/30s with key, 5 req/30s without
PAGE_SIZE = 2000
CACHE_TTL_HOURS = 12

CACHE_DIR = Path("cache");   CACHE_DIR.mkdir(exist_ok=True)
OUTPUT_DIR = Path("output"); OUTPUT_DIR.mkdir(exist_ok=True)
LOG_DIR = Path("logs");      LOG_DIR.mkdir(exist_ok=True)
CACHE_FILE = CACHE_DIR / "nvd_cache.json"
LOG_FILE = LOG_DIR / "feeds.log"

HEADERS = {"User-Agent": "NCRC-Threat-Collector/1.1"}
if NVD_API_KEY:
    HEADERS["apiKey"] = NVD_API_KEY

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(message)s",
    handlers=[logging.FileHandler(LOG_FILE), logging.StreamHandler()],
)
logger = logging.getLogger("feeds")

# ============================================================
# QUERIES
# ============================================================
SURFACE_QUERIES = {
    "CAN Bus": ["controller area network", "can bus"],
    "UDS Diagnostics": ["unified diagnostic services", "uds"],
    "ISO-TP": ["iso-tp", "isotp"],
    "DOIP": ["diagnostics over ip", "doip"],
    "SOME/IP": ["some/ip"],
    "J1939": ["j1939"],
    "OTA": ["over-the-air update", "ota update"],
    "EV Charging": ["evse", "ocpp", "charging station"],
    "Telematics": ["telematics", "telematics control unit", "vehicle cloud"],
    "Infotainment": ["infotainment", "in-vehicle infotainment"],
    "Fleet": ["fleet management"],
    "ECU": ["electronic control unit", "automotive ecu"],
    "Keyless / Hardware": ["keyless entry", "key fob", "immobilizer",
                           "rolling code", "tpms"],
}

# Vendor-name queries: catches CVEs that never mention a protocol term
# (e.g. "Tesla Model 3 ... allows attackers to ...").
# Ambiguous names (mini, seat, lucid, ford alone, bosch) are filtered locally with context.
VENDOR_QUERIES = [
    "tesla", "bmw", "audi", "volkswagen", "porsche", "skoda", "mercedes-benz",
    "toyota", "lexus", "hyundai", "kia", "honda", "nissan", "volvo cars",
    "polestar", "renault", "stellantis", "jeep", "rivian", "byd", "xpeng",
    "suzuki", "ford motor", "general motors", "harman", "denso", "visteon",
    "aptiv", "autosar", "qnx", "vector informatik",
]
SURFACE_QUERIES["OEM / Supplier"] = VENDOR_QUERIES


# ============================================================
# CACHE
# ============================================================
def load_cache():
    if not CACHE_FILE.exists():
        return {}
    try:
        return json.loads(CACHE_FILE.read_text(encoding="utf-8"))
    except Exception as e:
        logger.warning("Cache load failed: %s", e)
        return {}


def save_cache(cache):
    tmp = CACHE_FILE.with_suffix(".tmp")
    tmp.write_text(json.dumps(cache), encoding="utf-8")
    tmp.replace(CACHE_FILE)


def cache_fresh(item):
    return (isinstance(item, dict) and "ts" in item and "entry" in item
            and time.time() - item["ts"] < CACHE_TTL_HOURS * 3600)


# ============================================================
# HTTP
# ============================================================
def safe_get_json(url, retries=4):
    for attempt in range(1, retries + 1):
        wait = attempt * 2
        try:
            r = requests.get(url, timeout=TIMEOUT, headers=HEADERS)
            if r.status_code == 200:
                return r.json()
            logger.warning("HTTP %s: %s", r.status_code, url)
            if r.status_code in (403, 429, 503):   # NVD rate limiting
                wait = 10 * attempt
        except Exception as e:
            logger.warning("Attempt %s failed: %s", attempt, e)
        time.sleep(wait)
    return None


# ============================================================
# NVD
# ============================================================
def nvd_query(search):
    """Fetch ALL pages for one keyword phrase. Returns None if any page failed."""
    vulns, start, total = [], 0, 0
    while True:
        url = (f"{NVD_URL}?keywordSearch={quote(search)}&keywordExactMatch"
               f"&resultsPerPage={PAGE_SIZE}&startIndex={start}")
        data = safe_get_json(url)
        if data is None:
            return None                      # partial -> do not cache
        vulns.extend(data.get("vulnerabilities", []))
        total = data.get("totalResults", 0)
        start += data.get("resultsPerPage", PAGE_SIZE) or PAGE_SIZE
        if start >= total:
            break
        time.sleep(REQUEST_DELAY)
    return {"totalResults": total, "vulnerabilities": vulns}


def feed_nvd():
    cache = load_cache()
    results = []
    logger.info("Starting NVD collection (api key: %s)", bool(NVD_API_KEY))

    for surface, searches in SURFACE_QUERIES.items():
        for search in searches:
            key = f"{surface}:{search}"
            if cache_fresh(cache.get(key)):
                logger.info("CACHE HIT %s", key)
                results.append(cache[key]["entry"])
                continue

            logger.info("NVD QUERY -> %s", search)
            data = nvd_query(search)
            if data is None:
                logger.warning("No/partial data for %s (not cached)", search)
                continue

            entry = {"source": "NVD", "surface": surface,
                     "keyword": search, "data": data}
            results.append(entry)
            cache[key] = {"ts": time.time(), "entry": entry}
            save_cache(cache)
            logger.info("  %s -> %s CVEs", search, len(data["vulnerabilities"]))
            time.sleep(REQUEST_DELAY)

    logger.info("NVD collection complete: %s feeds", len(results))
    return results


# ============================================================
# CISA ICS (disabled: its JSON structure is not NVD-shaped,
# zero_day_intel only processes source == "NVD")
# ============================================================
def feed_cisa_ics():
    url = "https://www.cisa.gov/sites/default/files/feeds/ics-advisories.json"
    data = safe_get_json(url)
    if not data:
        logger.warning("CISA feed unavailable")
        return []
    return [{"source": "CISA_ICS", "surface": "ICS", "data": data}]


def enrich_feed_entry(entry):
    source = entry.get("source", "")
    meta = {"severity": "unknown", "domain": "generic", "category": "intel"}
    if source == "NVD":
        meta.update(severity="medium", domain="vulnerability", category="cve")
    elif source == "CISA_ICS":
        meta.update(severity="high", domain="ics", category="advisory")
    entry["meta"] = meta
    return entry


def collect_all_feeds():
    feeds = []
    feeds.extend(feed_nvd())
    # feeds.extend(feed_cisa_ics())
    feeds = [enrich_feed_entry(x) for x in feeds]
    logger.info("Total feeds collected: %s", len(feeds))
    return feeds


if __name__ == "__main__":
    feeds = collect_all_feeds()
    counts = {}
    for item in feeds:
        counts[item.get("surface", "Unknown")] = counts.get(item.get("surface", "Unknown"), 0) + 1
    summary = {"generated": time.time(), "feed_count": len(feeds),
               "surface_summary": counts, "feeds": feeds}
    outfile = OUTPUT_DIR / "automotive_feeds.json"
    outfile.write_text(json.dumps(summary, indent=2), encoding="utf-8")
    logger.info("Written: %s", outfile)
