import {useQuery} from "@tanstack/react-query";
import firestore from "@react-native-firebase/firestore";
import {useAuthContext} from "@/contexts/AuthContext";
import {UserProfile} from "@/hooks/firebase/types/profileTypes";

export const useGetFamilyMembers = (excludeSelf?: boolean) => {
    const {profileData, user} = useAuthContext()

    return useQuery({
        queryKey: ["familyMembers", user?.uid],
        queryFn: async (): Promise<undefined | UserProfile[]> => {
            const snapshot = await firestore()
                .collection("Profiles")
                .where("familyId", "==", profileData?.familyId)
                .get();

            if (excludeSelf) {
                return snapshot.docs.map((doc) => {
                    let documentData = doc.data();

                    return {
                        ...documentData,
                        uid: doc.id
                    }
                }).filter((doc) => doc.id !== user?.uid) as UserProfile[];
            }

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

                return {
                    ...documentData,
                    uid: doc.id
                }
            }) as UserProfile[];
        }
    })
}