import { useUser } from "@/contexts";
import { API_ENDPOINTS, userService } from "@/lib/api";
import { useMutation } from "@tanstack/react-query";
import { useMemo } from "react";

export const useLoginQuery = (callback?: () => void) => {
  const mutationKey = useMemo(() => [API_ENDPOINTS.USER.LOGIN], []);
  const { setAuthState } = useUser();
  return useMutation({
    mutationKey,
    mutationFn: userService.login,
    onSuccess: (response) => {
      setAuthState(response.data);
      console.log(response);

      if (callback) {
        callback();
      }
    },
  });
};

export const useLogoutQuery = () => {
  const { logout } = useUser();
  const mutationKey = useMemo(() => [API_ENDPOINTS.USER.LOGOUT], []);

  return useMutation({
    mutationKey,
    mutationFn: () => userService.logout(),
    onSuccess: () => {
      logout();
    },
  });
};
