/**
 * React Query hooks for managing business OAuth credentials
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { getBusinessOAuthCredentials, updateBusinessOAuthCredentials } from '../api/business';
import { BusinessOAuthCredentialsResponse } from '../types';

/**
 * Fetch business OAuth credentials
 */
export const useBusinessOAuthCredentials = () => {
  return useQuery<BusinessOAuthCredentialsResponse>({
    queryKey: ['businessOAuthCredentials'],
    queryFn: getBusinessOAuthCredentials,
    retry: false, // Don't retry on 404
    staleTime: 5 * 60 * 1000, // 5 minutes
  });
};

/**
 * Update business OAuth credentials
 */
export const useUpdateBusinessOAuthCredentials = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: {
      credentials?: Record<string, { client_id?: string; client_secret?: string }>;
      useCustomCredentials?: boolean;
    }) => updateBusinessOAuthCredentials(data),
    onSuccess: (data) => {
      queryClient.setQueryData(['businessOAuthCredentials'], data);
    },
  });
};
