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

interface UseRidesOptions {
  enabled?: boolean;
}

/**
 * Hook to fetch all rides with caching
 * Loads all rides for client-side filtering
 */
export function useRides({ enabled = true }: UseRidesOptions = {}) {
  return useQuery({
    queryKey: ['rides', 'all'],
    queryFn: async () => {
      const { data, error } = await supabase
        .from('rides')
        .select(`
          *, 
          park:parks!inner(name, slug, location:locations(*)),
          manufacturer:companies!rides_manufacturer_id_fkey(*),
          designer:companies!rides_designer_id_fkey(*)
        `)
        .order('name');

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