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

/**
 * Soft-deactivates or reactivates a variant (`set_active`) — **never a hard delete**. A deactivated
 * variant is unbookable and drops out of search. The offerings list shows inactive rows, so this
 * hook drives both the deactivate-with-confirm action and the reactivate affordance on an inactive
 * row. On success it invalidates the offerings list so the row flips to its new visual state.
 */
export function useSetVariantActive() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, isActive }: { id: number; isActive: boolean }) => catalogApi.setVariantActive(id, isActive),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: catalogKeys.myVariantsLists() });
    },
  });
}
