import { useQuery } from '@tanstack/react-query';
import apiClient from '@services/api';

/**
 * Custom hook for accessing branding settings
 * Uses React Query's caching to prevent multiple redundant API calls
 * 
 * @returns {Object} Query result with branding settings
 */
export const useBrandingSettings = () => {
  return useQuery({
    queryKey: ['branding-settings'],
    queryFn: async () => {
      try {
        const response = await apiClient.get('/settings/branding');
        
        // Convert the array of settings into an object for easier access
        const settings = {};
        if (Array.isArray(response.data)) {
          response.data.forEach(setting => {
            settings[setting.key] = setting.value;
          });
        } else {
          // If response is already an object, use it directly
          return response.data;
        }
        
        return settings;
      } catch (error) {
        console.error('Error fetching branding settings:', error);
        return null;
      }
    },
    staleTime: 5 * 60 * 1000, // 5 minutes cache
  });
};

export default useBrandingSettings;