import { useQuery } from '@tanstack/react-query';
import { supabase } from '@/integrations/supabase/client';
import { queryKeys } from '@/lib/queryKeys';

/**
 * Hook to fetch photo count for an entity
 */
export function usePhotoCount(entityType: string, entityId: string | undefined, enabled = true) {
  return useQuery({
    queryKey: ['photos', 'count', entityType, entityId || ''] as const,
    queryFn: async () => {
      if (!entityId) return 0;

      const { count, error } = await supabase
        .from('photos')
        .select('id', { count: 'exact', head: true })
        .eq('entity_type', entityType)
        .eq('entity_id', entityId);

      if (error) throw error;
      return count || 0;
    },
    enabled: enabled && !!entityId,
    staleTime: 10 * 60 * 1000, // 10 minutes - photo counts change rarely
    gcTime: 20 * 60 * 1000,
    refetchOnWindowFocus: false,
  });
}
