import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { getAppSettings, saveAppSettings, getMcpStatus } from "@/lib/tauri";
import type { AppSettings } from "@/types";

export function useAppSettings() {
  return useQuery({
    queryKey: ["app-settings"],
    queryFn: getAppSettings,
    staleTime: Infinity,
  });
}

export function useSaveAppSettings() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (settings: AppSettings) => saveAppSettings(settings),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["app-settings"] });
      queryClient.invalidateQueries({ queryKey: ["mcp-status"] });
    },
  });
}

export function useMcpStatus() {
  return useQuery({
    queryKey: ["mcp-status"],
    queryFn: getMcpStatus,
    refetchInterval: 5000,
  });
}
