import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { settingsApi } from "../lib/api/settings";

export function useSetting(key: string) {
  return useQuery({
    queryKey: ["setting", key],
    queryFn: () => settingsApi.get(key),
    staleTime: 60_000,
  });
}

export function useSaveSetting() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: ({ key, value }: { key: string; value: string }) =>
      settingsApi.save(key, value),
    onSuccess: (_data, { key }) => qc.invalidateQueries({ queryKey: ["setting", key] }),
  });
}
