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

interface ModulesResponse {
  enabled: string[];
}

/**
 * Hook to check if modules are enabled
 * Uses public endpoint, cached for performance
 */
export function useModules() {
  const { data, isLoading } = useQuery<ModulesResponse>({
    queryKey: ['modules-enabled'],
    queryFn: async () => {
      const response = await api.get('/modules/enabled');
      return response || { enabled: [] };
    },
    staleTime: 5 * 60 * 1000, // Cache for 5 minutes
  });

  const isEnabled = (moduleId: string): boolean => {
    return data?.enabled?.includes(moduleId) ?? false;
  };

  return {
    enabledModules: data?.enabled ?? [],
    isEnabled,
    isLoading,
  };
}
