import { useMutation, useQueryClient } from '@tanstack/react-query';
import { catalogApi } from '../apis';
import { catalogKeys } from '../keys';
import type { CreateVariantInput } from '../types';

/**
 * Creates a priced variant, then invalidates the nurse's offerings list. A duplicate identical
 * listing returns `409` — surfaced via `mutation.error` so the builder shows the inline
 * duplicate-listing warning (never a generic toast). A missing required dimension / bad price
 * returns `400`, likewise surfaced inline.
 */
export function useCreateVariant() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (input: CreateVariantInput) => catalogApi.createVariant(input),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: catalogKeys.myVariantsLists() });
    },
  });
}
