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

export interface CoasterStat {
  id: string;
  ride_id: string;
  stat_name: string;
  stat_value: number;
  unit?: string | null;
  category?: string | null;
  description?: string | null;
  display_order: number;
  created_at: string;
}

export function useCoasterStats(rideId: string | undefined) {
  return useQuery({
    queryKey: ['coaster-stats', rideId],
    queryFn: async () => {
      if (!rideId) return [];
      
      const { data, error} = await supabase
        .from('ride_coaster_stats')
        .select('*')
        .eq('ride_id', rideId)
        .order('display_order');
      
      if (error) throw error;
      
      return (data || []).map((stat) => ({
        id: stat.id,
        ride_id: stat.ride_id,
        stat_name: stat.stat_name,
        stat_value: stat.stat_value,
        unit: stat.unit || null,
        category: stat.category || null,
        description: stat.description || null,
        display_order: stat.display_order,
        created_at: stat.created_at,
      })) as CoasterStat[];
    },
    enabled: !!rideId
  });
}
