import { useQuery } from '@tanstack/react-query';
import { supabase } from '@/lib/supabaseClient';
import { LeaderboardData, TimePeriod } from '@/types/contributor';
import { queryKeys } from '@/lib/queryKeys';

export function useContributorLeaderboard(
  limit: number = 50,
  timePeriod: TimePeriod = 'all_time'
) {
  return useQuery({
    queryKey: queryKeys.analytics.contributorLeaderboard(limit, timePeriod),
    queryFn: async () => {
      const { data, error } = await supabase.rpc('get_contributor_leaderboard', {
        limit_count: limit,
        time_period: timePeriod,
      });

      if (error) throw error;
      
      return data as unknown as LeaderboardData;
    },
    staleTime: 5 * 60 * 1000, // 5 minutes
    refetchInterval: 5 * 60 * 1000, // Refresh every 5 minutes
  });
}
