/**
 * Attributes React Query Hooks
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { getAttributes, convertAttribute } from '../services/attributes';
import type { ConvertAttributeRequest } from '../types/attribute';

/**
 * Get all attributes
 */
export function useAttributes() {
  return useQuery({
    queryKey: ['attributes'],
    queryFn: getAttributes,
    staleTime: 60000, // 1 minute
  });
}

/**
 * Convert local attribute to global
 */
export function useConvertAttribute() {
  const queryClient = useQueryClient();
  
  return useMutation({
    mutationFn: (request: ConvertAttributeRequest) => convertAttribute(request),
    onSuccess: () => {
      // Invalidate attributes query to refresh list
      queryClient.invalidateQueries({ queryKey: ['attributes'] });
      // Also invalidate variations to refresh table
      queryClient.invalidateQueries({ queryKey: ['variations'] });
    },
  });
}
