import {useQuery} from "react-query";
import firestore from "@react-native-firebase/firestore";
import {useAuthContext} from "@/contexts/AuthContext";

export const useGetGroceries = () => {
    const { user, profileData } = useAuthContext();

    return useQuery({
        queryKey: ["groceries", user?.uid],
        queryFn: async () => {
            const snapshot = await firestore()
                .collection("Groceries")
                .where("familyId", "==", profileData?.familyId)
                .get();

            return snapshot.docs.map((doc) => {
                const data = doc.data();

                return {
                    id: doc.id,
                    title: data.title,
                    category: data.category,
                    approved: data.approved,
                    bought: data.bought,
                    recurring: data.recurring,
                    frequency: data.frequency,
                    creatorId: data.creatorId
                };
            });
        }
    })
};