import { useMutation, useQueryClient } from '@tanstack/react-query';
import { addressesApi } from '../apis';
import { addressKeys } from '../keys';

/**
 * Makes an address the customer's primary; single-primary enforcement is server-side (the prior
 * primary is cleared atomically). Invalidates the list — never hand-patches — so exactly one
 * card shows the badge after the flip.
 */
export function useSetPrimaryAddress() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => addressesApi.setPrimary(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: addressKeys.lists() });
    },
  });
}
