Skip to content
// Free, instant scoring from the data we already have (no AI, no API calls).
// Each signal adds points; the AI step later refines this.
const FREE_MAIL = ['gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'icloud.com', 'aol.com', 'proton.me', 'protonmail.com', 'live.com', 'msn.com'];
const HIGH_INTENT_SOURCES = ['website form', 'referral', 'google ads', 'booking', 'quote request', 'contact form'];

return $input.all().map(({ json: lead }) => {
  const signals = [];
  let score = 0;
  const add = (points, why) => { score += points; signals.push(`+${points} ${why}`); };

  const domain = lead.email.split('@')[1] ?? '';
  const businessEmail = domain !== '' && !FREE_MAIL.includes(domain);
  const text = `${lead.message} ${lead.company}`.toLowerCase();

  if (businessEmail) add(25, `business email (${domain})`);
  if (lead.phone.length >= 7) add(15, 'phone number');
  if (lead.company || lead.website) add(10, 'company or website given');
  if (lead.message.length >= 40) add(15, 'detailed message');
  if (HIGH_INTENT_SOURCES.includes(lead.source)) add(10, `high-intent source (${lead.source})`);
  if (/\$\s?\d|budget|quote|pricing|price|cost|invest/.test(text)) add(15, 'mentions budget or pricing');
  if (/asap|urgent|this week|today|tomorrow|right away|immediately/.test(text)) add(10, 'urgency');

  return {
    json: {
      ...lead,
      company_domain: businessEmail ? domain : '',
      rule_score: Math.min(score, 100),
      rule_signals: signals,
    },
  };
});