/**
 * Probabilistic AI Attribution Model
 * ────────────────────────────────────
 * Identifies PROBABLE AI-referred sessions from behavioral signals
 * even when the referrer is unknown/direct. Patent-pending system.
 *
 * AI visitors exhibit distinct behavioral patterns vs organic:
 * - Land deeper (on specific content pages, not homepage)
 * - Read more deliberately (higher scroll depth, slower velocity)
 * - Dwell longer (validating what the LLM told them)
 * - Copy text more often (comparing to LLM response)
 * - Convert faster (fewer pages before conversion)
 * - Navigate linearly (content → contact, no bouncing)
 */

export interface AiSessionData {
  landingPage: string;
  landingDepth: number;
  maxScroll: number;
  dwellTime: number;
  firstInteractionAt: number;
  textSelected: boolean;
  pagesViewed: number;
  rageClicks: number;
  returnVisitor: boolean;
  formSubmitted: boolean;
  phoneClicks: number;
  searchTerms: string[];
  referrer: string | null;
}

export interface AiProbabilityResult {
  score: number;
  confidence: "high" | "medium" | "low" | "none";
  signals: string[];
  channel: "probable_llmo" | "probable_geo" | null;
}

export function calculateAiProbability(data: AiSessionData): AiProbabilityResult {
  let score = 0;
  const signals: string[] = [];

  // Deep landing (AI visitors land on specific content, not homepage)
  if (data.landingDepth >= 2) {
    score += 15;
    signals.push("deep_landing");
  } else if (data.landingDepth === 1) {
    score += 5;
  }

  // High scroll depth (reading the cited content)
  if (data.maxScroll > 70) {
    score += 12;
    signals.push("high_scroll");
  } else if (data.maxScroll > 50) {
    score += 5;
  }

  // Long dwell time (reading to validate LLM response)
  if (data.dwellTime > 45000) {
    score += 10;
    signals.push("long_dwell");
  } else if (data.dwellTime > 20000) {
    score += 4;
  }

  // Delayed first interaction (reading before clicking)
  if (data.firstInteractionAt > 5000) {
    score += 8;
    signals.push("delayed_interaction");
  }

  // Text selection (comparing content to LLM response)
  if (data.textSelected) {
    score += 10;
    signals.push("text_selected");
  }

  // Linear navigation (no bouncing around)
  if (data.pagesViewed >= 2 && data.rageClicks === 0) {
    score += 8;
    signals.push("linear_navigation");
  }

  // Fast conversion path
  if ((data.formSubmitted || data.phoneClicks > 0) && data.pagesViewed <= 3) {
    score += 15;
    signals.push("fast_conversion");
  }

  // New visitor on deep page with no referrer = likely AI
  if (!data.returnVisitor && data.landingDepth >= 2 && !data.referrer) {
    score += 10;
    signals.push("new_deep_direct");
  }

  // On-site search (validating what LLM told them)
  if (data.searchTerms.length > 0) {
    score += 8;
    signals.push("on_site_search");
  }

  const confidence = score >= 60 ? "high" as const
    : score >= 35 ? "medium" as const
    : score >= 15 ? "low" as const
    : "none" as const;

  const channel = score >= 35 ? "probable_llmo" as const : null;

  return { score: Math.min(score, 100), confidence, signals, channel };
}

export function calculateEngagementScore(data: {
  pagesViewed: number;
  dwellTime: number;
  maxScroll: number;
  converted: boolean;
}): number {
  const score =
    (data.pagesViewed * 15) +
    (Math.min(data.dwellTime / 1000, 300) / 60 * 10) +
    (data.maxScroll * 0.5) +
    (data.converted ? 30 : 0);
  return Math.min(Math.round(score), 100);
}
