import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { conversationsApi } from '@/api/conversations'

/**
 * Hook for fetching conversations list, optionally filtered by language.
 */
export function useConversations(page = 1, perPage = 20, language?: string) {
  return useQuery({
    queryKey: ['conversations', page, perPage, language ?? '__all__'],
    queryFn: () => conversationsApi.getConversations(page, perPage, language),
  })
}

/**
 * Hook for fetching single conversation with messages
 */
export function useConversation(id: string, language?: string) {
  return useQuery({
    queryKey: ['conversation', id, language],
    queryFn: () => conversationsApi.getConversation(id, language),
    enabled: !!id,
  })
}

/**
 * Hook for deleting conversation
 */
export function useDeleteConversation() {
  const queryClient = useQueryClient()
  
  return useMutation({
    mutationFn: (id: string) => conversationsApi.deleteConversation(id),
    onSuccess: (_, deletedId) => {
      queryClient.invalidateQueries({ queryKey: ['conversations'] })
      queryClient.removeQueries({ queryKey: ['conversation', deletedId] })
    },
  })
}










