// adeater-security.js — security reporting + performance metrics.
// Depends on: adeater-utils.js (stats, scheduleReport, isWhitelisted)
//
// SCOPE: heuristic threat DETECTION (phishing link scoring, drive-by /
// script-injection / encoded-payload DOM scanning) was removed — it prevented
// nothing, only outlined links red / sent "threat" reports, and false-positived
// on ordinary pages (brand names in link text, IDN domains, CDN scripts). What
// remains: reportThreat() (still used by the real MAIN-world blockers in
// adeater-hooks.js — chrome:// nav, fullscreen/dialog loops) and the page
// performance metrics that feed the popup/side-panel KPIs.

// ---------------------------------------------------------------------------
// Threat reporting — relays a genuine block event to the background worker.
// ---------------------------------------------------------------------------
const _threatLogTs = {};
function reportThreat(type, detail) {
  if (isTrustedDomain()) return;
  stats.threatsBlocked++;
  scheduleReport();
  try {
    chrome.runtime.sendMessage({
      type: "SECURITY_THREAT",
      threatType: type,
      detail: detail,
    }).catch(() => {});
  } catch (e) {}
  // Deduplicate console.warn per type — still counts stats and sends a message
  // for every hit, but avoids spam when a page fires the same hook repeatedly.
  const now = Date.now();
  if (!_threatLogTs[type] || now - _threatLogTs[type] > 10000) {
    _threatLogTs[type] = now;
    // threat logged silently — reportHookThreat dispatches event for listeners
  }
}

// ---------------------------------------------------------------------------
// Performance metrics reporter — feeds the popup / side-panel KPIs.
// ---------------------------------------------------------------------------
function reportPerformanceMetrics() {
  if (!ENABLE_PERF) return;
  try {
    const metrics = {};

    let largestResource = null;
    let largestSize = 0;
    let totalResourceTime = 0;

    try {
      const resources = performance.getEntriesByType("resource");
      for (const res of resources) {
        totalResourceTime += res.duration || 0;
        if ((res.transferSize || 0) > largestSize) {
          largestSize = res.transferSize;
          largestResource = res.name;
        }
      }
      metrics.resourceCount = resources.length;
      metrics.totalResourceLoadTime = totalResourceTime;
    } catch (e) {}

    metrics.largestResource = largestResource;
    metrics.largestResourceSize = largestSize;

    try {
      metrics.domNodeCount = document.querySelectorAll("*").length;
    } catch (e) {
      metrics.domNodeCount = -1;
    }

    try {
      const navEntry = performance.getEntriesByType("navigation")[0];
      if (navEntry) {
        metrics.pageLoadTime = navEntry.loadEventEnd - navEntry.startTime;
      }
    } catch (e) {}

    stats.perfMetrics = metrics;

    try {
      chrome.runtime.sendMessage({
        type: "AD_EATER_PERF",
        metrics: metrics,
      }).catch(() => {});
    } catch (e) {}
  } catch (e) {}
}
