/*
 * integration-signatures.ts -- enrichment registry for Site Tag plugin detections.
 *
 * WHAT THIS FILE DOES:
 *   Provides vendor metadata (name, category, vendor, description) for every
 *   plugin_key that the Site Tag can emit. Keyed by plugin_key string.
 *
 * HOW IT IS USED:
 *   1. POST /api/admin/backfill-detected-integrations -- resolves category and
 *      vendor when upserting DetectedIntegration rows from historical events.
 *   2. GET /api/site-tag-analytics/integrations -- merges description into each
 *      row before returning to the client; no extra DB column needed.
 *   lookupEnrichment(plugin_key) and inferCategoryFromKey(plugin_key) are the
 *   two exported helpers consumed by those routes.
 *
 * WHERE DETECTION HAPPENS:
 *   public/t.source.js (gitignored unminified source; public/t.js is served).
 *   Three registries in that file:
 *     BOOKING_PLUGINS   (~line 5832) -- appointment and form plugins
 *     CHAT_WIDGETS      (~line 6416) -- live chat and messaging widgets
 *     ANALYTICS_PLUGINS (~line 6659) -- analytics, pixels, SEO, CMS, and more
 *   Each registry entry has a detect() function that inspects window globals,
 *   script src patterns, DOM selectors, meta tags, or cookies.
 *
 * HOW TO ADD A NEW DETECTION RULE:
 *   1. Add an entry to the appropriate registry in public/t.source.js (or add a
 *      new entry to ANALYTICS_PLUGINS if it doesn't fit existing registries).
 *   2. Add a matching entry here in ENRICHMENT_REGISTRY keyed by the same
 *      plugin_key string.
 *   3. Run: cp public/t.source.js public/t.js && npm run build:site-tag
 *   4. Bump TAG_VERSION in public/t.js to today's date (YYYY.MM.DD).
 *   See docs/phase-30-site-tag-detection-expansion.md for the full pattern guide.
 */

export type IntegrationCategory =
  | "analytics"
  | "pixel"
  | "seo"
  | "form"
  | "page_builder"
  | "booking"
  | "chat"
  | "reviews"
  | "crm"
  | "cache"
  | "security"
  | "ecommerce"
  | "lead_gen"
  | "other";

export interface IntegrationEnrichment {
  name: string;
  category: IntegrationCategory;
  vendor: string;
  vendorWebsite?: string;
  description?: string;
}

// Keyed by plugin_key as emitted by the Site Tag.
export const ENRICHMENT_REGISTRY: Record<string, IntegrationEnrichment> = {
  // ── Confirmed production plugins ──────────────────────────────────────────
  amelia: {
    name: "Amelia",
    category: "booking",
    vendor: "TMS",
    vendorWebsite: "https://wpamelia.com",
    description: "WordPress appointment and event booking",
  },
  chat_fb_messenger: {
    name: "Facebook Messenger",
    category: "chat",
    vendor: "Meta",
    vendorWebsite: "https://www.facebook.com/business",
    description: "Facebook Messenger chat widget",
  },
  birdeye: {
    name: "Birdeye",
    category: "reviews",
    vendor: "Birdeye",
    vendorWebsite: "https://birdeye.com",
    description: "Reputation and customer experience platform",
  },
  deardoc: {
    name: "DearDoc",
    category: "lead_gen",
    vendor: "DearDoc",
    vendorWebsite: "https://www.getdeardoc.com",
    description: "Healthcare patient acquisition platform",
  },

  // ── Analytics and tracking pixels ─────────────────────────────────────────
  ga4: {
    name: "Google Analytics 4",
    category: "analytics",
    vendor: "Google",
    vendorWebsite: "https://analytics.google.com",
    description: "Web and app analytics",
  },
  gtm: {
    name: "Google Tag Manager",
    category: "analytics",
    vendor: "Google",
    vendorWebsite: "https://tagmanager.google.com",
    description: "Tag management system",
  },
  ua_legacy: {
    name: "Universal Analytics",
    category: "analytics",
    vendor: "Google",
    vendorWebsite: "https://analytics.google.com",
    description: "Legacy Google Analytics (Universal Analytics)",
  },
  fb_pixel: {
    name: "Meta Pixel",
    category: "pixel",
    vendor: "Meta",
    vendorWebsite: "https://www.facebook.com/business",
    description: "Conversion tracking and retargeting pixel",
  },
  linkedin_insight: {
    name: "LinkedIn Insight Tag",
    category: "pixel",
    vendor: "LinkedIn",
    vendorWebsite: "https://business.linkedin.com",
    description: "LinkedIn conversion tracking and retargeting",
  },
  tiktok_pixel: {
    name: "TikTok Pixel",
    category: "pixel",
    vendor: "TikTok",
    vendorWebsite: "https://ads.tiktok.com",
    description: "TikTok conversion tracking pixel",
  },
  twitter_pixel: {
    name: "X Pixel",
    category: "pixel",
    vendor: "X (Twitter)",
    vendorWebsite: "https://ads.twitter.com",
    description: "X (Twitter) conversion tracking pixel",
  },
  pinterest_tag: {
    name: "Pinterest Tag",
    category: "pixel",
    vendor: "Pinterest",
    vendorWebsite: "https://ads.pinterest.com",
    description: "Pinterest conversion tracking tag",
  },
  reddit_pixel: {
    name: "Reddit Pixel",
    category: "pixel",
    vendor: "Reddit",
    vendorWebsite: "https://ads.reddit.com",
    description: "Reddit conversion tracking pixel",
  },
  clarity: {
    name: "Microsoft Clarity",
    category: "analytics",
    vendor: "Microsoft",
    vendorWebsite: "https://clarity.microsoft.com",
    description: "Session recording and heatmaps",
  },
  hotjar: {
    name: "Hotjar",
    category: "analytics",
    vendor: "Hotjar",
    vendorWebsite: "https://hotjar.com",
    description: "Session recording, heatmaps, and feedback",
  },
  amplitude: {
    name: "Amplitude",
    category: "analytics",
    vendor: "Amplitude",
    vendorWebsite: "https://amplitude.com",
    description: "Product analytics and behavioral data platform",
  },

  // ── WordPress core ─────────────────────────────────────────────────────────
  wordpress: {
    name: "WordPress",
    category: "seo",
    vendor: "WordPress.org",
    vendorWebsite: "https://wordpress.org",
    description: "WordPress CMS detected via generator meta tag",
  },

  // ── SEO plugins ────────────────────────────────────────────────────────────
  yoast: {
    name: "Yoast SEO",
    category: "seo",
    vendor: "Yoast",
    vendorWebsite: "https://yoast.com",
    description: "WordPress SEO optimization plugin",
  },
  rankmath: {
    name: "Rank Math SEO",
    category: "seo",
    vendor: "Rank Math",
    vendorWebsite: "https://rankmath.com",
    description: "WordPress SEO plugin with schema markup",
  },

  // ── Forms ──────────────────────────────────────────────────────────────────
  formidable: {
    name: "Formidable Forms",
    category: "form",
    vendor: "Strategy11",
    vendorWebsite: "https://formidableforms.com",
    description: "WordPress drag-and-drop form builder with advanced features",
  },
  fluent_forms: {
    name: "Fluent Forms",
    category: "form",
    vendor: "WPManageNinja",
    vendorWebsite: "https://fluentforms.com",
    description: "Lightweight WordPress form builder",
  },
  gravity_forms: {
    name: "Gravity Forms",
    category: "form",
    vendor: "Rocketgenius",
    vendorWebsite: "https://www.gravityforms.com",
    description: "Advanced WordPress form builder",
  },
  wpforms: {
    name: "WPForms",
    category: "form",
    vendor: "WPForms",
    vendorWebsite: "https://wpforms.com",
    description: "Drag-and-drop WordPress form builder",
  },
  contact_form_7: {
    name: "Contact Form 7",
    category: "form",
    vendor: "Takayuki Miyoshi",
    vendorWebsite: "https://contactform7.com",
    description: "Simple flexible WordPress contact forms",
  },
  hubspot: {
    name: "HubSpot",
    category: "crm",
    vendor: "HubSpot",
    vendorWebsite: "https://hubspot.com",
    description: "CRM, forms, and marketing automation",
  },
  hubspot_track: {
    name: "HubSpot Tracking",
    category: "crm",
    vendor: "HubSpot",
    vendorWebsite: "https://hubspot.com",
    description: "HubSpot analytics and tracking script",
  },
  klaviyo: {
    name: "Klaviyo",
    category: "crm",
    vendor: "Klaviyo",
    vendorWebsite: "https://klaviyo.com",
    description: "Email and SMS marketing automation",
  },

  // ── Page builders ──────────────────────────────────────────────────────────
  elementor: {
    name: "Elementor",
    category: "page_builder",
    vendor: "Elementor Ltd",
    vendorWebsite: "https://elementor.com",
    description: "WordPress drag-and-drop page builder",
  },
  divi: {
    name: "Divi",
    category: "page_builder",
    vendor: "Elegant Themes",
    vendorWebsite: "https://www.elegantthemes.com/gallery/divi/",
    description: "WordPress page builder and theme by Elegant Themes",
  },
  beaver_builder: {
    name: "Beaver Builder",
    category: "page_builder",
    vendor: "Beaver Builder",
    vendorWebsite: "https://www.wpbeaverbuilder.com",
    description: "WordPress front-end page builder",
  },
  brizy: {
    name: "Brizy",
    category: "page_builder",
    vendor: "Brizy",
    vendorWebsite: "https://www.brizy.io",
    description: "No-code WordPress page builder",
  },
  bricks: {
    name: "Bricks",
    category: "page_builder",
    vendor: "Bricks",
    vendorWebsite: "https://bricksbuilder.io",
    description: "Visual WordPress theme and page builder",
  },

  // ── E-commerce ────────────────────────────────────────────────────────────
  shopify: {
    name: "Shopify",
    category: "ecommerce",
    vendor: "Shopify",
    vendorWebsite: "https://shopify.com",
    description: "Hosted e-commerce platform",
  },
  edd: {
    name: "Easy Digital Downloads",
    category: "ecommerce",
    vendor: "Sandhills Development",
    vendorWebsite: "https://easydigitaldownloads.com",
    description: "WordPress digital product sales plugin",
  },
  woocommerce: {
    name: "WooCommerce",
    category: "ecommerce",
    vendor: "Automattic",
    vendorWebsite: "https://woocommerce.com",
    description: "WordPress e-commerce platform",
  },

  // ── Booking and scheduling ────────────────────────────────────────────────
  bookly: {
    name: "Bookly",
    category: "booking",
    vendor: "Bookly",
    vendorWebsite: "https://booking-wp-plugin.com",
    description: "WordPress appointment booking plugin",
  },
  bookingpress: {
    name: "BookingPress",
    category: "booking",
    vendor: "Repute InfoSystems",
    vendorWebsite: "https://www.bookingpressplugin.com",
    description: "WordPress appointment booking and scheduling plugin",
  },
  simplybook: {
    name: "SimplyBook.me",
    category: "booking",
    vendor: "SimplyBook.me",
    vendorWebsite: "https://simplybook.me",
    description: "Online booking system for service businesses",
  },
  setmore: {
    name: "Setmore",
    category: "booking",
    vendor: "Setmore",
    vendorWebsite: "https://www.setmore.com",
    description: "Online appointment scheduling software",
  },
  calendly: {
    name: "Calendly",
    category: "booking",
    vendor: "Calendly",
    vendorWebsite: "https://calendly.com",
    description: "Online scheduling and appointment booking",
  },

  // ── Reviews and reputation ────────────────────────────────────────────────
  trustpilot: {
    name: "Trustpilot",
    category: "reviews",
    vendor: "Trustpilot",
    vendorWebsite: "https://www.trustpilot.com",
    description: "Consumer review platform widget",
  },
  google_reviews_widget: {
    name: "Google Reviews Widget",
    category: "reviews",
    vendor: "Various",
    description: "Embedded Google Reviews display widget",
  },
  nicejob: {
    name: "NiceJob",
    category: "reviews",
    vendor: "NiceJob",
    vendorWebsite: "https://nicejob.com",
    description: "Reputation marketing and review generation platform",
  },

  // ── Cache and performance ─────────────────────────────────────────────────
  wp_rocket: {
    name: "WP Rocket",
    category: "cache",
    vendor: "WP Media",
    vendorWebsite: "https://wp-rocket.me",
    description: "WordPress caching and performance plugin",
  },
  litespeed_cache: {
    name: "LiteSpeed Cache",
    category: "cache",
    vendor: "LiteSpeed Technologies",
    vendorWebsite: "https://www.litespeedtech.com",
    description: "WordPress cache plugin for LiteSpeed servers",
  },

  // ── Security ──────────────────────────────────────────────────────────────
  wordfence: {
    name: "Wordfence",
    category: "security",
    vendor: "Defiant",
    vendorWebsite: "https://www.wordfence.com",
    description: "WordPress security plugin with firewall and malware scan",
  },
  sucuri: {
    name: "Sucuri",
    category: "security",
    vendor: "GoDaddy",
    vendorWebsite: "https://sucuri.net",
    description: "Website security platform and WAF",
  },

  // ── Healthcare-specific ───────────────────────────────────────────────────
  patientpop: {
    name: "PatientPop",
    category: "lead_gen",
    vendor: "Tebra",
    vendorWebsite: "https://www.patientpop.com",
    description: "Healthcare practice growth and patient acquisition platform",
  },
  doctible: {
    name: "Doctible",
    category: "lead_gen",
    vendor: "Doctible",
    vendorWebsite: "https://www.doctible.com",
    description: "Patient engagement and online scheduling for healthcare",
  },

  // ── SEO (additional) ─────────────────────────────────────────────────────
  aioseo: {
    name: "All in One SEO",
    category: "seo",
    vendor: "All in One SEO",
    vendorWebsite: "https://aioseo.com",
    description: "WordPress SEO plugin with schema and XML sitemaps",
  },
  seopress: {
    name: "SEOPress",
    category: "seo",
    vendor: "SEOPress",
    vendorWebsite: "https://www.seopress.org",
    description: "WordPress SEO plugin with content analysis",
  },

  // ── CRM and marketing automation (additional) ─────────────────────────────
  activecampaign: {
    name: "ActiveCampaign",
    category: "crm",
    vendor: "ActiveCampaign",
    vendorWebsite: "https://www.activecampaign.com",
    description: "Email marketing and marketing automation platform",
  },
  convertkit: {
    name: "ConvertKit",
    category: "crm",
    vendor: "ConvertKit",
    vendorWebsite: "https://convertkit.com",
    description: "Creator-focused email marketing platform",
  },

  // ── Email / marketing ─────────────────────────────────────────────────────
  mailchimp: {
    name: "Mailchimp",
    category: "crm",
    vendor: "Mailchimp",
    vendorWebsite: "https://mailchimp.com",
    description: "Email marketing and automation",
  },
};

export function lookupEnrichment(plugin_key: string): IntegrationEnrichment | null {
  return ENRICHMENT_REGISTRY[plugin_key.toLowerCase()] ?? null;
}

// Derives a best-guess category from the plugin_key prefix when enrichment
// is unavailable. Site Tag uses structured prefixes for its detections.
export function inferCategoryFromKey(plugin_key: string): IntegrationCategory {
  const key = plugin_key.toLowerCase();
  if (key.startsWith("chat_")) return "chat";
  if (key.startsWith("pixel_")) return "pixel";
  if (key.startsWith("form_")) return "form";
  if (key.startsWith("seo_")) return "seo";
  if (key.startsWith("analytics_")) return "analytics";
  if (key.startsWith("booking_")) return "booking";
  if (key.startsWith("reviews_")) return "reviews";
  if (key.startsWith("crm_")) return "crm";
  if (key.startsWith("ecommerce_")) return "ecommerce";
  if (key.startsWith("lead_")) return "lead_gen";
  return "other";
}
