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

export const useGetNotes = () => {
  const { user: currentUser } = useAuthContext();

  return useQuery<IBrainDump[]>({
    queryKey: ["braindumps", currentUser?.uid],
    queryFn: async () => {
      try {
        const snapshot = await firestore()
          .collection("BrainDumps")
          .where("creatorId", "==", currentUser?.uid)
          .orderBy("updatedAt", "desc")
          .get();

        return snapshot.docs.map((doc) => ({
          ...doc.data(),
        })) as IBrainDump[];
      } catch (error) {
        console.error("Error fetching braindumps:", error);
        throw error;
      }
    },
    enabled: !!currentUser?.uid,
  });
};
