import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { useAuthToken } from "../hooks/use-root-data";
import { UserProfileUpdate } from "../../src/api/types";
import { createUserManagementApi } from "~/utils/api-client-factory";

export function useServiceGetProfile() {
  const token = useAuthToken();
  const response = useQuery({
    enabled: !!token,
    retry: 1,
    retryDelay: 3000,
    refetchOnWindowFocus: false,
    queryKey: ["getProfileService"],
    queryFn: async () => {
      const userApi = createUserManagementApi(token);
      return await userApi.apiUserV1ProfileList();
    },
  });

  return { ...response };
}

export function useServiceUpdateProfile(profileData: UserProfileUpdate) {
  const token = useAuthToken();
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: async () => {
      const userApi = createUserManagementApi(token);
      return await userApi.apiUserV1ProfilePartialUpdate({ data: profileData });
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["getProfileService"] });
    },
  });
}
