import { toast } from "sonner";
import { useRouter } from "next/navigation";
import { signOut } from "next-auth/react";
import { useMutation } from "@tanstack/react-query";

export const useLogout = () => {
  const router = useRouter();

  const mutation = useMutation({
    mutationFn: async () => {
      await signOut({ redirect: false });
    },
    onSuccess: () => {
      toast.success("Logged out successfully");
      router.refresh();
      router.push("/sign-in");
    },
    onError: () => {
      toast.error("Failed to logout");
    },
  });

  return mutation;
};
