import {useQuery, useQueryClient} from "@tanstack/react-query";
import firestore from "@react-native-firebase/firestore";
import {ProfileType, useAuthContext} from "@/contexts/AuthContext";
import {useAtomValue} from "jotai";
import {isFamilyViewAtom} from "@/components/pages/calendar/atoms";
import {colorMap} from "@/constants/colorMap";
import {useEffect, useRef} from "react";
import {UserProfile} from "@/hooks/firebase/types/profileTypes";

const createEventHash = (event: any): string => {
    const str = `${event.startDate?.seconds || ''}-${event.endDate?.seconds || ''}-${
        event.title || ''
    }-${event.location || ''}-${event.allDay ? 'true' : 'false'}`;

    let hash = 0;
    for (let i = 0; i < str.length; i++) {
        const char = str.charCodeAt(i);
        hash = ((hash << 5) - hash) + char;
        hash = hash & hash;
    }
    return hash.toString(36);
};

const fetchEvents = async (userId: string, profileData: UserProfile | undefined, isFamilyView: boolean) => {
    const db = firestore();
    const eventsQuery = db.collection("Events");
    let constraints;
    let familyId = profileData?.familyId;

    if (profileData?.userType === ProfileType.FAMILY_DEVICE) {
        constraints = [
            eventsQuery.where("familyId", "==", familyId)
        ];
    } else {
        if (isFamilyView) {
            constraints = [
                eventsQuery.where("familyId", "==", familyId),
                eventsQuery.where("creatorId", "==", userId),
                eventsQuery.where("attendees", "array-contains", userId)
            ];
        } else {
            constraints = [
                eventsQuery.where("creatorId", "==", userId),
                eventsQuery.where("attendees", "array-contains", userId)
            ];
        }
    }

    const snapshots = await Promise.all(constraints.map(query => query.get()));

    const uniqueEvents = new Map();
    const processedHashes = new Set();
    const creatorIds = new Set();

    snapshots.forEach(snapshot => {
        snapshot.docs.forEach(doc => {
            const event = doc.data();
            const hash = createEventHash(event);

            if (!processedHashes.has(hash)) {
                processedHashes.add(hash);
                creatorIds.add(event.creatorId);
                uniqueEvents.set(doc.id, event);
            }
        });
    });

    const creatorIdsArray = Array.from(creatorIds);
    const creatorProfiles = new Map();

    for (let i = 0; i < creatorIdsArray.length; i += 10) {
        const chunk = creatorIdsArray.slice(i, i + 10);
        const profilesSnapshot = await db
            .collection("Profiles")
            .where(firestore.FieldPath.documentId(), "in", chunk)
            .get();

        profilesSnapshot.docs.forEach(doc => {
            creatorProfiles.set(doc.id, doc.data()?.eventColor || colorMap.pink);
        });
    }

    return Array.from(uniqueEvents.entries()).map(([id, event]) => ({
        ...event,
        id,
        start: event.allDay
            ? new Date(new Date(event.startDate.seconds * 1000).setHours(0, 0, 0, 0))
            : new Date(event.startDate.seconds * 1000),
        end: event.allDay
            ? new Date(new Date(event.endDate.seconds * 1000).setHours(0, 0, 0, 0))
            : new Date(event.endDate.seconds * 1000),
        hideHours: event.allDay,
        eventColor: creatorProfiles.get(event.creatorId) || colorMap.pink,
        notes: event.notes
    }));
};

export const useGetEvents = () => {
    const { user, profileData } = useAuthContext();
    const isFamilyView = useAtomValue(isFamilyViewAtom);
    const queryClient = useQueryClient();
    const lastSyncTimestamp = useRef<number>(0);

    useEffect(() => {
        if (!user?.uid || !profileData?.familyId) return;

        const prefetchEvents = async () => {
            await queryClient.prefetchQuery({
                queryKey: ["events", user.uid, false],
                queryFn: () => fetchEvents(user.uid, profileData, false),
                staleTime: 5 * 60 * 1000,
            });

            await queryClient.prefetchQuery({
                queryKey: ["events", user.uid, true],
                queryFn: () => fetchEvents(user.uid, profileData, true),
                staleTime: 5 * 60 * 1000,
            });
        };

        prefetchEvents();

        const unsubscribe = firestore()
            .collection('Households')
            .where("familyId", "==", profileData.familyId)
            .onSnapshot((snapshot) => {
                snapshot.docChanges().forEach((change) => {
                    if (change.type === 'modified') {
                        const data = change.doc.data();
                        if (data?.lastSyncTimestamp) {
                            const newTimestamp = data.lastSyncTimestamp.seconds;
                            if (newTimestamp > lastSyncTimestamp.current) {
                                lastSyncTimestamp.current = newTimestamp;
                                // Invalidate both queries
                                queryClient.invalidateQueries({
                                    queryKey: ["events", user.uid]
                                });
                                queryClient.invalidateQueries({
                                    queryKey: ["notifications"]
                                });
                            }
                        }
                    }
                });
            }, console.error);

        return unsubscribe;
    }, [profileData?.familyId, user?.uid, queryClient]);

    return useQuery({
        queryKey: ["events", user?.uid, isFamilyView],
        queryFn: () => fetchEvents(user?.uid!, profileData, isFamilyView),
        staleTime: 10 * 60 * 1000,
        gcTime: 24 * 60 * 60 * 1000,
        placeholderData: (previousData) => previousData,
        enabled: Boolean(user?.uid),
    });
};