import analytics from './index'
import { SEARCH_EVENTS } from '../../constants/events'

/**
 * 搜索行为追踪器
 */
class SearchTracker {
  constructor() {
    // 当前搜索会话
    this.currentSearch = null
    // 搜索开始时间
    this.searchStartTime = null
  }

  /**
   * 搜索框聚焦
   * @param {string} pagePath - 页面路径
   */
  onSearchFocus(pagePath = '') {
    analytics.track(SEARCH_EVENTS.SEARCH_FOCUS, {
      page_path: pagePath,
    })
  }

  /**
   * 执行搜索
   * @param {object} params - 搜索参数
   */
  onSearch(params) {
    const {
      query,
      type = 'keyword',
      source = 'input', // input/suggestion/history/hot
    } = params

    this.currentSearch = {
      query,
      type,
      source,
    }
    this.searchStartTime = Date.now()

    console.log('[SearchTracker] Search started:', query)
  }

  /**
   * 搜索结果返回
   * @param {object} result - 搜索结果
   */
  onSearchResult(result) {
    if (!this.currentSearch) return

    const searchTime = this.searchStartTime
      ? Date.now() - this.searchStartTime
      : 0

    const eventData = {
      search_query: this.currentSearch.query,
      search_type: this.currentSearch.type,
      search_source: this.currentSearch.source,
      result_count: result.total || result.list?.length || 0,
      search_time: searchTime,
      has_results: (result.total || result.list?.length || 0) > 0,
    }

    analytics.track(SEARCH_EVENTS.SEARCH_EXECUTED, eventData)

    // 如果无结果，额外上报
    if (!eventData.has_results) {
      analytics.track(SEARCH_EVENTS.SEARCH_NO_RESULTS, {
        search_query: this.currentSearch.query,
        search_type: this.currentSearch.type,
      })
    }

    console.log('[SearchTracker] Search completed:', eventData)
  }

  /**
   * 点击搜索结果
   * @param {object} item - 点击的结果项
   * @param {number} position - 结果位置
   */
  onResultClick(item, position) {
    if (!this.currentSearch) return

    analytics.track(SEARCH_EVENTS.SEARCH_RESULT_CLICKED, {
      search_query: this.currentSearch.query,
      result_id: item.id,
      result_title: item.title,
      result_position: position,
      result_type: item.type || 'news',
    })
  }

  /**
   * 点击搜索建议
   * @param {string} suggestion - 建议词
   * @param {number} position - 位置
   */
  onSuggestionClick(suggestion, position) {
    analytics.track(SEARCH_EVENTS.SEARCH_SUGGESTION_CLICKED, {
      suggestion_text: suggestion,
      suggestion_position: position,
    })

    // 设置为新的搜索
    this.onSearch({
      query: suggestion,
      type: 'keyword',
      source: 'suggestion',
    })
  }

  /**
   * 点击热门搜索
   * @param {string} keyword - 热门词
   * @param {number} position - 位置
   */
  onHotSearchClick(keyword, position) {
    analytics.track(SEARCH_EVENTS.HOT_SEARCH_CLICKED, {
      keyword: keyword,
      position: position,
    })

    // 设置为新的搜索
    this.onSearch({
      query: keyword,
      type: 'keyword',
      source: 'hot',
    })
  }

  /**
   * 点击搜索历史
   * @param {string} keyword - 历史词
   * @param {number} position - 位置
   */
  onHistoryClick(keyword, position) {
    analytics.track(SEARCH_EVENTS.SEARCH_HISTORY_CLICKED, {
      keyword: keyword,
      position: position,
    })

    // 设置为新的搜索
    this.onSearch({
      query: keyword,
      type: 'keyword',
      source: 'history',
    })
  }

  /**
   * 清空搜索历史
   */
  onHistoryClear() {
    analytics.track(SEARCH_EVENTS.SEARCH_HISTORY_CLEARED, {})
  }

  /**
   * 重置搜索会话
   */
  reset() {
    this.currentSearch = null
    this.searchStartTime = null
  }
}

// 导出单例
export default new SearchTracker()