mergeInto(LibraryManager.library, {
  // Function to add or update user profile data
  AddDataToFirestore: function (walletPtr, jsonPtr) {
    var wallet = UTF8ToString(walletPtr);
    var jsonData = UTF8ToString(jsonPtr);
    var data = JSON.parse(jsonData);

    window.db.collection("users").doc(wallet).collection("ProfileData").doc("Main").set(data)
      .then(function () {
        console.log("Data successfully written!");
      })
      .catch(function (error) {
        console.error("Error writing document: ", error);
      });
  },

 // Function to retrieve data from Firestore
 GetUserProfileData: function (walletPtr) {
    var wallet = UTF8ToString(walletPtr);

    // Ensure the Firestore instance is accessible
    if (typeof window.db === 'undefined') {
      console.error("Firestore is not initialized.");
      return;
    }

    window.db
      .collection("users")
      .doc(wallet)
      .collection("ProfileData")
      .doc("Main")
      .get()
      .then(function (doc) {
        if (doc.exists) {
          var data = doc.data();
          var jsonData = JSON.stringify(data);
          if (typeof web3UnityInstance !== 'undefined') {
            web3UnityInstance.SendMessage("FirebaseManager", "OnUserProfileDataReceived", jsonData);
          } else {
            console.error("web3UnityInstance is undefined.");
          }
        } else {
          console.log("No such document!");
        }
      })
      .catch(function (error) {
        console.error("Error getting document:", error);
      });
  }
});
