import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { chatApi } from "../api/chat";

export function useChatBookmarks(companyId: string | null, conversationId?: string) {
  const query = useQuery({
    queryKey: ["chat", "bookmarks", companyId, conversationId],
    queryFn: () => chatApi.getBookmarks(companyId!, conversationId),
    enabled: !!companyId,
  });

  return {
    data: query.data,
    isLoading: query.isLoading,
  };
}

export function useToggleBookmark() {
  const queryClient = useQueryClient();

  const mutation = useMutation({
    mutationFn: ({ conversationId, messageId }: { conversationId: string; messageId: string }) =>
      chatApi.toggleBookmark(conversationId, messageId),
    onSuccess: () => {
      void queryClient.invalidateQueries({ queryKey: ["chat", "bookmarks"] });
      void queryClient.invalidateQueries({ queryKey: ["chat", "search"] });
    },
  });

  return {
    toggleBookmark: mutation.mutate,
    isPending: mutation.isPending,
  };
}
