import { useQuery } from '@tanstack/react-query'
import { analyticsApi } from '@/api/analytics'

/**
 * Hook for fetching dashboard stats
 */
export function useDashboard(language?: string) {
  return useQuery({
    queryKey: ['dashboard', language],
    queryFn: () => analyticsApi.getDashboard(language),
    refetchInterval: 5 * 60 * 1000,
  })
}

/**
 * Hook for fetching trends data
 */
export function useTrends(period: '7days' | '30days' | '90days' = '7days', language?: string) {
  return useQuery({
    queryKey: ['trends', period, language],
    queryFn: () => analyticsApi.getTrends(period, language),
    staleTime: 5 * 60 * 1000,
  })
}

/**
 * Hook for fetching source analytics - which pages generate leads/conversations
 */
export function useSourceAnalytics(language?: string) {
  return useQuery({
    queryKey: ['analytics', 'sources', language],
    queryFn: () => analyticsApi.getSources(language),
    staleTime: 5 * 60 * 1000,
  })
}

/**
 * Hook for fetching widget analytics - which widgets generate leads/meetings
 */
export function useWidgetAnalytics(botId?: number, language?: string) {
  return useQuery({
    queryKey: ['analytics', 'widgets', botId, language],
    queryFn: () => analyticsApi.getWidgets(botId, language),
    staleTime: 5 * 60 * 1000,
  })
}

/**
 * Hook for fetching funnel analytics - conversion funnel stages
 */
export function useFunnelAnalytics(period: '7days' | '30days' | '90days' = '30days', botId?: number, language?: string) {
  return useQuery({
    queryKey: ['analytics', 'funnel', period, botId, language],
    queryFn: () => analyticsApi.getFunnel(period, botId, language),
    staleTime: 5 * 60 * 1000,
  })
}

/**
 * Hook for fetching bot analytics - comprehensive bot performance
 */
export function useBotAnalytics(language?: string) {
  return useQuery({
    queryKey: ['analytics', 'bots', language],
    queryFn: () => analyticsApi.getBots(language),
    staleTime: 5 * 60 * 1000,
  })
}

export function useCommerceAnalytics(period: '7days' | '30days' | '90days' = '30days', botId?: number, language?: string) {
  return useQuery({
    queryKey: ['analytics', 'commerce', period, botId, language],
    queryFn: () => analyticsApi.getCommerce(period, botId, language),
    staleTime: 60 * 1000,
  })
}
