import logger from "../../debugConfig"

interface SearXNGResult {
  title?: string
  url?: string
  content?: string
}

export interface SearchResult {
  title: string;
  url: string;
  content?: string;
}

export class SearchService {
  static async search(query: string): Promise<SearchResult[]> {
    if (!query.trim()) {
      return []
    }

    try {
      const searchParams = new URLSearchParams({
        q: query,
        format: 'json',
        language: 'en',
        time_range: 'None',
        safesearch: '0',
        engines: 'google,bing,duckduckgo',
      })

      const url = `/searxng-api/search?${searchParams.toString()}`
      logger.debug('search-service', "Search URL: ", url)

      const response = await fetch(url, {
        method: 'GET',
        headers: {
          'Accept': 'application/json',
        },
      })
      logger.debug('search-service', "Search response: ", response)

      if (!response.ok) {
        throw new Error(`Search failed with status: ${response.status}`)
      }

      const data = await response.json()

      logger.debug('search-service', "Search data: ", data)

      return (data.results || []).map((result: SearXNGResult) => ({
        title: result.title || '',
        url: result.url || '',
        content: result.content || ''
      }))
    } catch (error) {
      console.error('Search error:', error)
      throw error
    }
  }
} 