export interface AboveFoldCompositionResult {
  hasCta: boolean;
  hasPhoneNumber: boolean;
  hasHours: boolean;
  hasPrice: boolean;
  hasDemo: boolean;
  score: number;
  verticalNotes: string[];
}

// Vertical-specific weights for above-fold elements
const VERTICAL_WEIGHTS: Record<
  string,
  { cta: number; phone: number; hours: number; price: number; demo: number }
> = {
  "healthcare-medical": { cta: 20, phone: 30, hours: 25, price: 5, demo: 20 },
  "dental": { cta: 20, phone: 30, hours: 25, price: 5, demo: 20 },
  "legal": { cta: 25, phone: 35, hours: 20, price: 5, demo: 15 },
  "home-services": { cta: 25, phone: 35, hours: 15, price: 10, demo: 15 },
  "automotive": { cta: 20, phone: 25, hours: 15, price: 25, demo: 15 },
  "restaurant": { cta: 15, phone: 20, hours: 35, price: 20, demo: 10 },
  "saas": { cta: 30, phone: 10, hours: 5, price: 20, demo: 35 },
  "ecommerce": { cta: 30, phone: 10, hours: 5, price: 40, demo: 15 },
  "default": { cta: 25, phone: 20, hours: 15, price: 20, demo: 20 },
};

const CTA_PATTERNS = [
  /\b(?:get\s+(?:a\s+)?(?:free\s+)?quote|book\s+(?:now|appointment|a\s+call)|schedule|contact\s+us|request|sign\s+up|get\s+started|start\s+(?:free\s+)?trial|try\s+(?:for\s+)?free|buy\s+now|shop\s+now|order\s+now)\b/i,
  /<(?:button|a)[^>]*(?:class|id)=["'][^"']*(?:cta|hero|primary|btn-primary|action|callout)[^"']*["'][^>]*>/i,
];

const PHONE_PATTERNS = [
  /(?:\+1[\s.-]?)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}/,
  /href=["']tel:/i,
];

const HOURS_PATTERNS = [
  /\b(?:open|hours?|mon|tue|wed|thu|fri|sat|sun|monday|tuesday|wednesday|thursday|friday|saturday|sunday)\b.{0,60}(?:\d{1,2}(?::\d{2})?\s*(?:am|pm))/i,
  /\b(?:open\s+)?24\s*(?:\/|hours?|7)/i,
];

const PRICE_PATTERNS = [
  /\$\d+(?:\.\d{2})?(?:\s*\/\s*(?:mo|month|yr|year))?/i,
  /\bstarting\s+(?:at|from)\s+\$\d+/i,
  /\bfree\s+(?:plan|tier|forever|shipping)\b/i,
  /\bpricing\b/i,
];

const DEMO_PATTERNS = [
  /\b(?:request|book|schedule|watch|get)\s+(?:a\s+)?(?:free\s+)?demo\b/i,
  /\bfree\s+(?:consultation|assessment|evaluation|audit)\b/i,
];

function detectInAboveFold(html: string, patterns: RegExp[]): boolean {
  // Approximate above-fold as first ~4000 chars of the body content
  const aboveFold = html.slice(0, 4000);
  return patterns.some((p) => p.test(aboveFold));
}

export function analyzeMobileAboveFoldComposition(
  domHtml: string,
  vertical: string,
): AboveFoldCompositionResult {
  const weights = VERTICAL_WEIGHTS[vertical] ?? VERTICAL_WEIGHTS["default"];

  const hasCta = detectInAboveFold(domHtml, CTA_PATTERNS);
  const hasPhoneNumber = detectInAboveFold(domHtml, PHONE_PATTERNS);
  const hasHours = detectInAboveFold(domHtml, HOURS_PATTERNS);
  const hasPrice = detectInAboveFold(domHtml, PRICE_PATTERNS);
  const hasDemo = detectInAboveFold(domHtml, DEMO_PATTERNS);

  const total = weights.cta + weights.phone + weights.hours + weights.price + weights.demo;
  const earned =
    (hasCta ? weights.cta : 0) +
    (hasPhoneNumber ? weights.phone : 0) +
    (hasHours ? weights.hours : 0) +
    (hasPrice ? weights.price : 0) +
    (hasDemo ? weights.demo : 0);

  const score = total > 0 ? Math.round((earned / total) * 100) : 0;

  const verticalNotes: string[] = [];
  if (!hasCta) verticalNotes.push("No clear call-to-action found in the visible area on mobile");
  if (!hasPhoneNumber && weights.phone >= 20)
    verticalNotes.push("Phone number not prominently displayed above the fold");
  if (!hasHours && weights.hours >= 20)
    verticalNotes.push("Business hours not visible above the fold");
  if (!hasDemo && weights.demo >= 25)
    verticalNotes.push("Demo or free trial prompt not visible above the fold");

  return { hasCta, hasPhoneNumber, hasHours, hasPrice, hasDemo, score, verticalNotes };
}
