import {useMutation, useQueryClient} from "react-query";
import {UserProfile} from "@/hooks/firebase/types/profileTypes";
import functions from '@react-native-firebase/functions';
import {ProfileType, useAuthContext} from "@/contexts/AuthContext";

export const useCreateSubUser = () => {
    const queryClient = useQueryClient()
    const { profileType } = useAuthContext()

    return useMutation({
        mutationKey: ["createSubUser"],
        mutationFn: async ({email, ...userProfile}: { email: string } & UserProfile) => {
            if(profileType === ProfileType.PARENT) {
                return await functions().httpsCallable("createSubUser")({email, ...userProfile})
            } else {
                throw Error("Can't create sub-users as a non-parent.")
            }
        },
        onSuccess: () => {
            queryClient.invalidateQueries({queryKey: ["getChildrenByParentId"]})
        }
    });
}