The basic idea is:
Block equipment changing (disable_items) -- very important! Store all the equipment data you want to keep for the gear to enchant (i.e. location, item ID, cards, refines...). Find a free card slot for the enchantment. Generate a random item ID for the enchantment. Delete the old gear (delequip), then use getitem2 to create a new piece of equipment slotted with the enchantment.