import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { fetchListings, deleteListing, deleteAllListings } from '@/lib/api/listings'

export function useListings(limit = 100) {
  return useQuery({
    queryKey: ['listings', limit],
    queryFn: () => fetchListings(limit),
    refetchInterval: 10_000,
  })
}

export function useDeleteListing() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: deleteListing,
    onSuccess: () => qc.invalidateQueries({ queryKey: ['listings'] }),
  })
}

export function useDeleteAllListings() {
  const qc = useQueryClient()
  return useMutation({
    mutationFn: deleteAllListings,
    onSuccess: () => qc.invalidateQueries({ queryKey: ['listings'] }),
  })
}
