import { useMutation, useQueryClient } from "@tanstack/react-query";
import firestore from "@react-native-firebase/firestore";
import { useAuthContext } from "@/contexts/AuthContext";
import { IGrocery } from "@/hooks/firebase/types/groceryData";

export const useCreateGrocery = () => {
    const { user: currentUser, profileData } = useAuthContext();
    const queryClient = useQueryClient();
    const groceriesKey = ["groceries", currentUser?.uid];

    return useMutation({
        mutationFn: (groceryData: Partial<IGrocery>) => {
            console.log("Call")
            const newDoc = firestore().collection('Groceries').doc();
            return firestore()
                .collection("Groceries")
                .add({
                    ...groceryData,
                    id: newDoc.id,
                    familyId: profileData?.familyId,
                    creatorId: currentUser?.uid
                });
        },
        onSettled: () => {
            return queryClient.invalidateQueries({
                queryKey: groceriesKey,
            });
        }
    });
};