import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { addHistoryEntry, getHistory, clearHistory } from "@/lib/tauri";
import type { HistoryEntry } from "@/types";

export function useHistory(connectionId?: string, search?: string) {
  return useQuery({
    queryKey: ["history", connectionId, search],
    queryFn: () => getHistory({ connectionId, search }),
  });
}

export function useAddHistory() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (entry: HistoryEntry) => addHistoryEntry(entry),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["history"] });
    },
  });
}

export function useClearHistory() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: () => clearHistory(),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["history"] });
    },
  });
}
