import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import * as scoresApi from '../api/scores';
import { useToast } from '../components/ui/Toast';

export function useRankings() {
  return useQuery({
    queryKey: ['rankings'],
    queryFn: () => scoresApi.getRankings(),
  });
}

export function useUpdateWeights() {
  const qc = useQueryClient();
  const { addToast } = useToast();

  return useMutation({
    mutationFn: (weights: Record<string, number>) => scoresApi.updateWeights(weights),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: ['rankings'] });
      addToast('success', 'Weights updated successfully');
    },
    onError: (error: Error) => {
      addToast('error', error.message || 'Failed to update weights');
    },
  });
}
