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

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

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

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

                return {...data, timestamp: new Date(data.timestamp.seconds * 1000 + data.timestamp.nanoseconds / 1e6)} as {
                    creatorId: string,
                    familyId: string,
                    content: string,
                    eventId: string,
                    timestamp: Date,
                };
            });
        }
    })
};