import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { getMemory, saveMemory } from "@/lib/tauri";

export function useMemory(connectionId: string | null) {
  return useQuery({
    queryKey: ["memory", connectionId],
    queryFn: () => getMemory(connectionId!),
    enabled: !!connectionId,
    staleTime: 5_000,
  });
}

export function useSaveMemory() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ connectionId, content }: { connectionId: string; content: string }) =>
      saveMemory(connectionId, content),
    onSuccess: (_data, vars) => {
      queryClient.invalidateQueries({ queryKey: ["memory", vars.connectionId] });
    },
  });
}
