/**
 * Business OAuth Settings Hooks
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { getBusinessOAuthSettings, updateBusinessOAuthSettings } from '../api/business';
import { BusinessOAuthSettings, BusinessOAuthSettingsResponse } from '../types';

/**
 * Hook to get business OAuth settings and available providers
 */
export const useBusinessOAuthSettings = () => {
  return useQuery<BusinessOAuthSettingsResponse>({
    queryKey: ['businessOAuthSettings'],
    queryFn: getBusinessOAuthSettings,
    staleTime: 5 * 60 * 1000, // 5 minutes
  });
};

/**
 * Hook to update business OAuth settings
 */
export const useUpdateBusinessOAuthSettings = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (settings: Partial<BusinessOAuthSettings>) =>
      updateBusinessOAuthSettings(settings),
    onSuccess: (data) => {
      // Update the cached data
      queryClient.setQueryData(['businessOAuthSettings'], data);
    },
  });
};
