import { useQuery } from '@tanstack/react-query';
import { supabase } from '@/integrations/supabase/client';
import { queryKeys } from '@/lib/queryKeys';
import type { EntityComparisons } from '@/types/database-analytics';

export function useEntityComparisons() {
  return useQuery({
    queryKey: queryKeys.analytics.entityComparisons(),
    queryFn: async () => {
      const { data, error } = await supabase.rpc('get_entity_comparisons');

      if (error) {
        throw error;
      }

      return data as unknown as EntityComparisons;
    },
    staleTime: 15 * 60 * 1000, // 15 minutes
    refetchInterval: 10 * 60 * 1000, // Auto-refetch every 10 minutes
  });
}
