import { useMutation, useQueryClient } from "@tanstack/react-query";
import { sendOtpAPI, verifyOtpAPI, logoutAPI } from "../api/auth.api";

/**
 * Send OTP
 */
export const useSendOtp = () =>
  useMutation({
    mutationFn: sendOtpAPI,
  });

/**
 * Verify OTP
 */
export const useVerifyOtp = () =>
  useMutation({
    mutationFn: verifyOtpAPI,
  });

/**
 * Logout
 */
export const useLogout = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: logoutAPI,
    onSuccess: () => {
      queryClient.clear(); // 🔥 clear all cached APIs
      localStorage.clear();
      window.location.href = "/login";
    },
  });
};