DROP PROCEDURE IF EXISTS add_outfit_in_change_log;;
CREATE PROCEDURE add_outfit_in_change_log(IN updated_outfit_id INT(11))
READS SQL DATA
BEGIN
    INSERT INTO outfits_change_log
        SET outfit_id = updated_outfit_id
        ON DUPLICATE KEY UPDATE outfit_id = updated_outfit_id;
END;;

DROP TRIGGER IF EXISTS after_outfits_update;;
CREATE TRIGGER after_outfits_update
    AFTER UPDATE ON outfits
    FOR EACH ROW 
BEGIN
    CALL add_outfit_in_change_log(NEW.outfit_id);
END;;

DROP TRIGGER IF EXISTS after_outfits_insert;;
CREATE TRIGGER after_outfits_insert 
    AFTER INSERT ON outfits
    FOR EACH ROW 
BEGIN
    CALL add_outfit_in_change_log(NEW.outfit_id);
END;;

DROP TRIGGER IF EXISTS after_outfit_products_update;;
CREATE TRIGGER after_outfit_products_update
    AFTER UPDATE ON outfit_products
    FOR EACH ROW 
BEGIN
    CALL add_outfit_in_change_log(NEW.outfit_id);
END;;

DROP TRIGGER IF EXISTS after_outfit_products_insert;;
CREATE TRIGGER after_outfit_products_insert 
    AFTER INSERT ON outfit_products
    FOR EACH ROW 
BEGIN
    CALL add_outfit_in_change_log(NEW.outfit_id);
END;;

DROP TRIGGER IF EXISTS after_outfit_product_option_values_update;;
CREATE TRIGGER after_outfit_product_option_values_update
    AFTER UPDATE ON outfit_product_option_values
    FOR EACH ROW 
BEGIN
    CALL add_outfit_in_change_log(NEW.outfit_id);
END;;

DROP TRIGGER IF EXISTS after_outfit_product_option_values_insert;;
CREATE TRIGGER after_outfit_product_option_values_insert 
    AFTER INSERT ON outfit_product_option_values
    FOR EACH ROW 
BEGIN
    CALL add_outfit_in_change_log(NEW.outfit_id);
END;;
