We implemented once a API which just creates a code and stores it with the order-item, so you know inside of the order, what code for which gift card. So I recommend to implement an observer for `order_item_save_before` which checks for the product and whether there is NO id, if it is the case, than generate a new code and add it as product option, so it is automatically visible everywhere