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

/**
 * Hook to fetch featured parks (top rated and most rides)
 */
export function useFeaturedParks() {
  const topRated = useQuery({
    queryKey: queryKeys.homepage.featuredParks.topRated(),
    queryFn: async () => {
      const { data, error } = await supabase
        .from('parks')
        .select(`
          *,
          location:locations(*),
          operator:companies!parks_operator_id_fkey(*)
        `)
        .order('average_rating', { ascending: false })
        .limit(3);

      if (error) throw error;
      return data || [];
    },
    staleTime: 10 * 60 * 1000, // 10 minutes - featured parks change rarely
    gcTime: 30 * 60 * 1000,
    refetchOnWindowFocus: false,
  });

  const mostRides = useQuery({
    queryKey: queryKeys.homepage.featuredParks.mostRides(),
    queryFn: async () => {
      const { data, error } = await supabase
        .from('parks')
        .select(`
          *,
          location:locations(*),
          operator:companies!parks_operator_id_fkey(*)
        `)
        .order('ride_count', { ascending: false })
        .limit(3);

      if (error) throw error;
      return data || [];
    },
    staleTime: 10 * 60 * 1000, // 10 minutes
    gcTime: 30 * 60 * 1000,
    refetchOnWindowFocus: false,
  });

  return {
    topRated: {
      data: topRated.data,
      isLoading: topRated.isLoading,
      error: topRated.error,
    },
    mostRides: {
      data: mostRides.data,
      isLoading: mostRides.isLoading,
      error: mostRides.error,
    },
  };
}
