import { useUser } from "@/contexts";
import { API_ENDPOINTS, IUser, profileService } from "@/lib/api";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useMemo } from "react";
import { toast } from "react-toastify";
export const useProfileQuery = () => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.PROFILE.READ, "CURRENT_USER_PROFILE"],
    [],
  );
  return useQuery({
    queryKey,
    queryFn: () => profileService.getProfile(),
  });
};
export const useUpdateProfileQuery = () => {
  const { syncCurrentUser } = useUser();
  const queryClient = useQueryClient();
  const mutationKey = useMemo(
    () => [API_ENDPOINTS.PROFILE.UPDATE, "UPDATE_USER_PROFILE"],
    [],
  );
  return useMutation({
    mutationKey,
    mutationFn: profileService.updateProfile,
    onSuccess: (response: IUser) => {
      toast.success("Profile updated successfully");
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.PROFILE.READ, "CURRENT_USER_PROFILE"],
      });
      syncCurrentUser(response);
    },
  });
};
