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

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

  return useQuery<IFeedback[]>({
    queryKey: ["feedbacks", currentUser?.uid],
    queryFn: async () => {
      try {
        const snapshot = await firestore()
          .collection("Feedbacks")
          .where("creatorId", "==", currentUser?.uid)
          .get();

        return snapshot.docs.map((doc) => ({
          ...doc.data(),
        })) as IFeedback[];
      } catch (error) {
        console.error("Error fetching feedbacks:", error);
        throw error;
      }
    },
    enabled: !!currentUser?.uid, // Only run query if we have a user ID
  });
};
