/**
 * Forensic Intelligence Client
 * Connects to Zeq Forensic Suite (port 8042) for AI response analysis.
 * 20 HF scoring functions for accuracy, bias, manipulation, credibility.
 *
 * @version 1.287.4
 * @license CC BY 4.0 — Zeq. H / Zeq 1.287 Hz Limited
 */
const ForensicClient = (() => {
  'use strict';

  const API_BASE = 'http://localhost/services/forensic';

  // ── Analyze AI response text ──────────────────────────────────────────
  async function analyze(text, options = {}) {
    try {
      const response = await fetch(`${API_BASE}/analyze`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          text,
          domains: options.domains || [],
          operators: options.operators || [],
          depth: options.depth || 'standard',
        }),
      });
      if (!response.ok) throw new Error(`Forensic: ${response.status}`);
      return await response.json();
    } catch (e) {
      console.warn('[Zeq Forensic]', e.message);
      return null;
    }
  }

  // ── Quick score (lightweight analysis) ────────────────────────────────
  async function quickScore(text) {
    try {
      const response = await fetch(`${API_BASE}/score`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ text }),
      });
      if (!response.ok) return null;
      return await response.json();
    } catch {
      return null;
    }
  }

  // ── Health check ──────────────────────────────────────────────────────
  async function isAvailable() {
    try {
      const res = await fetch(`${API_BASE}/health`, { signal: AbortSignal.timeout(2000) });
      return res.ok;
    } catch {
      return false;
    }
  }

  return { analyze, quickScore, isAvailable };
})();
