/**
 * Detects the intent behind a command palette query.
 * Returns one of: 'question' | 'keyword' | 'page' | 'empty'
 */

const QUESTION_WORDS = [
  'how', 'why', 'what', 'when', 'where', 'who', 'which',
  'fix', 'help', 'troubleshoot', 'resolve', 'debug', 'diagnose',
]

const PAGE_NAMES = [
  'dashboard', 'home',
  'flows', 'trees', 'all flows',
  'sessions', 'history',
  'analytics', 'reports',
  'settings', 'account', 'profile',
  'admin', 'administration', 'users',
  'assistant', 'ai', 'copilot', 'flowpilot',
  'scripts', 'script generator',
  'kb', 'knowledge base', 'kb accelerator',
  'library', 'step library',
]

export type PaletteIntent = 'question' | 'keyword' | 'page' | 'empty'

export function detectIntent(query: string): PaletteIntent {
  const trimmed = query.trim()

  if (!trimmed) {
    return 'empty'
  }

  const lower = trimmed.toLowerCase()

  // Check if it matches a known page name
  if (PAGE_NAMES.some(p => lower === p || lower.startsWith(p + ' ') || lower.endsWith(' ' + p))) {
    return 'page'
  }

  // Check for question indicators:
  // - Contains a question mark
  if (lower.includes('?')) {
    return 'question'
  }

  // - Starts with a question word
  const firstWord = lower.split(/\s+/)[0]
  if (QUESTION_WORDS.includes(firstWord)) {
    return 'question'
  }

  // - 5 or more words
  const wordCount = trimmed.split(/\s+/).length
  if (wordCount >= 5) {
    return 'question'
  }

  return 'keyword'
}
