// AI Service Domain Registry
// Maps domains to their service names for AI visibility tracking

export const AI_DOMAINS = {
  // ChatGPT / OpenAI
  'chatgpt.com': 'ChatGPT',
  'chat.openai.com': 'ChatGPT',
  'openai.com': 'ChatGPT',

  // Microsoft Copilot
  'copilot.microsoft.com': 'Copilot Cloud',
  'copilot.cloud.microsoft': 'Copilot Cloud',
  'github.copilot.com': 'Copilot Cloud',
  'copilot.github.com': 'Copilot Cloud',

  // Google Gemini
  'gemini.google.com': 'Gemini',
  'bard.google.com': 'Gemini',
  'aistudio.google.com': 'Gemini',

  // Grok / xAI
  'grok.com': 'Grok',
  'grok.x.ai': 'Grok',
  'x.ai': 'Grok',

  // Perplexity
  'perplexity.ai': 'Perplexity',
  'www.perplexity.ai': 'Perplexity',

  // Claude / Anthropic
  'claude.ai': 'Claude',
  'console.anthropic.com': 'Claude',

  // Meta AI
  'meta.ai': 'Meta AI',
  'www.meta.ai': 'Meta AI',

  // Mistral
  'chat.mistral.ai': 'Mistral',
  'mistral.ai': 'Mistral',

  // Hugging Face
  'huggingface.co': 'Hugging Face',

  // DeepSeek
  'chat.deepseek.com': 'DeepSeek',
  'deepseek.com': 'DeepSeek',

  // Poe
  'poe.com': 'Poe',

  // You.com
  'you.com': 'You.com',

  // Cohere
  'coral.cohere.com': 'Cohere',
  'dashboard.cohere.com': 'Cohere',

  // Jasper
  'app.jasper.ai': 'Jasper',

  // Cursor / AI coding
  'cursor.sh': 'Cursor',

  // Replit AI
  'replit.com': 'Replit'
};

/**
 * Check if a hostname matches a known AI service domain.
 * Handles www. prefixes and subdomain matching.
 * @param {string} hostname - e.g. "www.chatgpt.com"
 * @returns {{ domain: string, service: string } | null}
 */
export function matchAIDomain(hostname) {
  if (!hostname) return null;

  const host = hostname.toLowerCase();

  // Direct match
  if (AI_DOMAINS[host]) {
    return { domain: host, service: AI_DOMAINS[host] };
  }

  // Try without www.
  const noWww = host.replace(/^www\./, '');
  if (AI_DOMAINS[noWww]) {
    return { domain: noWww, service: AI_DOMAINS[noWww] };
  }

  // Try parent domain (e.g. "chat.openai.com" → "openai.com")
  const parts = host.split('.');
  if (parts.length > 2) {
    const parent = parts.slice(1).join('.');
    if (AI_DOMAINS[parent]) {
      return { domain: parent, service: AI_DOMAINS[parent] };
    }
  }

  return null;
}
