import { useUserProfileStore } from "../context/zustand-store/userStore";
import { useApiMutation } from "../utils/useApiRequest";

export const useFetchProfile = () => {
  const setUserProfile = useUserProfileStore((s) => s.setUserProfile);

  const mutationProfile = useApiMutation({
    api: "users/my-profile",
    method: "get",
    disableBackdrop: true,
  });

  const getProfile = async () => {
    try {
      const response = await mutationProfile.mutateAsync({});
      // Handle different response structures
      let profileData = response;
      
      // If response has a data property, use it
      if (response?.data) {
        profileData = response.data;
      }
      // If response has a user property, use it
      else if (response?.user) {
        profileData = response.user;
      }
      
      if (profileData && typeof profileData === "object") {
        setUserProfile(profileData);
      }
    } catch (error) {
      console.error("Error fetching profile:", error);
    }
  };

  return { getProfile };
};


