import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/Service";
import { CreateReviewType } from "../types/ReviewTypes";

export const useGetFood = (id: string) => {
  return useQuery({
    queryKey: ["food", id],
    queryFn: () => api.getFood(id),
  });
};

export const useToggleFavorite = () => {
  return useMutation({
    mutationFn: api.toggleFavorite,
  });
};

export const useGetFoodReviews = (foodId: string) => {
  return useQuery({
    queryKey: ["food-reviews", foodId],
    queryFn: () => api.getFoodReviews(foodId),
    enabled: !!foodId,
  });
};

export const useCreateReview = (foodId: string) => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (params: CreateReviewType) => api.createReview(params),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["food-reviews", foodId] });
    },
  });
};
