export type SearchSourceMode =
  | 'mixed'
  | 'threads'
  | 'brave'
  | 'crawler'
  | 'threads_crawler'

export const SEARCH_SOURCE_OPTIONS: Array<{
  id: SearchSourceMode
  title: string
  hint: string
}> = [
  {
    id: 'threads_crawler',
    title: '爬蟲優先（省 API）',
    hint: '有 Chrome Session 時先爬蟲，結果不足再呼叫 Threads API',
  },
  {
    id: 'mixed',
    title: '混合（建議）',
    hint: '爬蟲優先 → Threads API → Web Search 補洞',
  },
  {
    id: 'threads',
    title: '僅 Threads API',
    hint: '每個標籤最多 1 次官方搜尋，不需 Chrome',
  },
  {
    id: 'crawler',
    title: '僅爬蟲',
    hint: 'Playwright 關鍵字搜尋，需同步 Chrome Session；有則顯示藍勾／粉絲數',
  },
  {
    id: 'brave',
    title: '僅 Web Search',
    hint: 'Brave / Exa 網搜 Threads 貼文，需 Search API key',
  },
]

export function searchSourceModeLabel(mode: string | undefined) {
  const found = SEARCH_SOURCE_OPTIONS.find((item) => item.id === mode)
  if (found) return found.title
  if (mode === 'threads_brave') return '混合（建議）'
  if (mode === 'brave_crawler') return '爬蟲優先（省 API）'
  return '混合（建議）'
}

export function normalizeSearchSourceMode(mode: string | undefined): SearchSourceMode {
  if (
    mode === 'threads' ||
    mode === 'brave' ||
    mode === 'mixed' ||
    mode === 'crawler' ||
    mode === 'threads_crawler'
  ) {
    return mode
  }
  if (mode === 'threads_brave') return 'mixed'
  if (mode === 'brave_crawler') return 'threads_crawler'
  return 'mixed'
}

export function searchSourceModeToApi(mode: SearchSourceMode): string {
  return mode
}