import { useMutation } from "@tanstack/react-query";
import { supabase } from "../lib/supabase";
import { FeedbackData } from "../pages/feedback";
import { useUser } from "../providers/UserStoreProvider";

// Create new feedback
export const useCreateFeedback = () => {
  const user = useUser();
  const { mutate, isSuccess, isPending } = useMutation({
    mutationFn: async ({ fd_type, message }: FeedbackData) => {
      const { error } = await supabase.from("feedbacks").insert({
        fd_type,
        message,
        user_id: user?.id ?? "",
      });
      if (error) throw error;
    },
    onSuccess: () => {
      // Do nothing
    },
  });

  return { createFeedback: mutate, isSuccess, isPending };
};
