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

export const useGetEvents = () => {
    const {user} = useAuthContext()

    return useQuery({
        queryKey: ["events", user?.uid],
        queryFn: async () => {
            const snapshot = await firestore()
                .collection("Events")
                .where("creatorId", "==", user?.uid)
                .get();

            const events: ICalendarEventBase[] = snapshot.docs.map((doc) => {
                const data = doc.data();
                return {
                    title: data.title,
                    start: new Date(data.startDate.seconds * 1000),
                    end: new Date(data.endDate.seconds * 1000),
                    hideHours: data.allDay,
                };
            });

            return events;
        }
    })
}