import { useMutation, useQueryClient } from '@tanstack/react-query';
import { adminApi } from '../apis';
import { adminKeys } from '../keys';

/**
 * Update one config row (audited server-side). On success invalidate the whole config sub-tree — both the
 * list and the change-history drawer — so the new value + the new history row show without a manual refresh.
 * A config change is **not** retroactive (the confirmation copy says so); the client never re-prices.
 */
export function useUpdatePlatformConfig() {
  const queryClient = useQueryClient();
  return useMutation<void, unknown, { key: string; value: string }>({
    mutationFn: ({ key, value }) => adminApi.updateConfig(key, value),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: adminKeys.config() });
    },
  });
}
