import {useAuthContext} from "@/contexts/AuthContext";
import {useMutation} from "@tanstack/react-query";
import firestore from "@react-native-firebase/firestore";
import auth, {FirebaseAuthTypes} from "@react-native-firebase/auth";

export const useDeleteUser = () => {
    const {user: currentUser} = useAuthContext();

    return useMutation({
        mutationKey: ["deleteUser"],
        mutationFn: async ({customUser}: { customUser?: FirebaseAuthTypes.User }) => {
            const user = currentUser ?? customUser;

            if (user) {
                try {
                    await firestore()
                        .collection("Profiles")
                        .doc(user.uid)
                        .delete();

                    await auth().currentUser?.delete();

                    await auth().signOut();

                    console.log("User deleted and signed out successfully");
                } catch (e) {
                    console.error("Error deleting user:", e);
                }
            }
        },
    });
};