
USE fetchmetrics;

DROP TRIGGER IF EXISTS fetchmetrics.before_insert_DOG_Distraction;
DROP TRIGGER IF EXISTS fetchmetrics.before_update_DOG_Distraction;

DELIMITER //
CREATE TRIGGER fetchmetrics.before_insert_DOG_Distraction
BEFORE INSERT ON fetchmetrics.DOG_Distraction
FOR EACH ROW
BEGIN
	SET NEW.created_on := IFNULL(NEW.created_on, NOW());
END //
DELIMITER ;

DELIMITER //
CREATE TRIGGER fetchmetrics.before_update_DOG_Distraction
BEFORE UPDATE ON fetchmetrics.DOG_Distraction
FOR EACH ROW
BEGIN
	IF OLD.id_change_set <=> NEW.id_change_set THEN
		SIGNAL SQLSTATE '45000'
        SET MESSAGE_TEXT = 'New change Set ID must be provided.';
    END IF;
    
    INSERT INTO fetchmetrics.DOG_Distraction_Audit (
		  id_distraction
        , name_field
        , value_prev
        , value_new
        , id_change_set
    )
    /*
	-- Changed id_assessment
	SELECT NEW.id_distraction, 'id_assessment', CONVERT(OLD.id_assessment, CHAR), CONVERT(NEW.id_assessment, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_assessment <=> NEW.id_assessment)
	UNION
    */
	-- Changed id_assessment_command_link
	SELECT NEW.id_distraction, 'id_assessment_command_link', CONVERT(OLD.id_assessment_command_link, CHAR), CONVERT(NEW.id_assessment_command_link, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_assessment_command_link <=> NEW.id_assessment_command_link)
	UNION
	-- Changed id_distraction_type
	SELECT NEW.id_distraction, 'id_distraction_type', CONVERT(OLD.id_distraction_type, CHAR), CONVERT(NEW.id_distraction_type, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_distraction_type <=> NEW.id_distraction_type)
	UNION
	-- Changed id_intensity_level_emotional
	SELECT NEW.id_distraction, 'id_intensity_level_emotional', CONVERT(OLD.id_intensity_level_emotional, CHAR), CONVERT(NEW.id_intensity_level_emotional, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_intensity_level_emotional <=> NEW.id_intensity_level_emotional)
	UNION
	-- Changed id_intensity_level_scent
	SELECT NEW.id_distraction, 'id_intensity_level_scent', CONVERT(OLD.id_intensity_level_scent, CHAR), CONVERT(NEW.id_intensity_level_scent, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_intensity_level_scent <=> NEW.id_intensity_level_scent)
	UNION
	-- Changed id_intensity_level_sight
	SELECT NEW.id_distraction, 'id_intensity_level_sight', CONVERT(OLD.id_intensity_level_sight, CHAR), CONVERT(NEW.id_intensity_level_sight, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_intensity_level_sight <=> NEW.id_intensity_level_sight)
	UNION
	-- Changed id_intensity_level_sound
	SELECT NEW.id_distraction, 'id_intensity_level_sound', CONVERT(OLD.id_intensity_level_sound, CHAR), CONVERT(NEW.id_intensity_level_sound, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_intensity_level_sound <=> NEW.id_intensity_level_sound)
	UNION
	-- Changed id_intensity_level_touch
	SELECT NEW.id_distraction, 'id_intensity_level_touch', CONVERT(OLD.id_intensity_level_touch, CHAR), CONVERT(NEW.id_intensity_level_touch, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_intensity_level_touch <=> NEW.id_intensity_level_touch)
	UNION
	-- Changed quantity
	SELECT NEW.id_distraction, 'quantity', CONVERT(OLD.quantity, CHAR), CONVERT(NEW.quantity, CHAR), NEW.id_change_set
		WHERE NOT (OLD.quantity <=> NEW.quantity)
	UNION
	-- Changed proximity_metres
	SELECT NEW.id_distraction, 'proximity_metres', CONVERT(OLD.proximity_metres, CHAR), CONVERT(NEW.proximity_metres, CHAR), NEW.id_change_set
		WHERE NOT (OLD.proximity_metres <=> NEW.proximity_metres)
	UNION
	-- Changed notes
	SELECT NEW.id_distraction, 'notes', OLD.notes, NEW.notes, NEW.id_change_set
		WHERE NOT (OLD.notes <=> NEW.notes)
	UNION
	-- Changed active
	SELECT NEW.id_distraction, 'active', CONVERT(CONVERT(OLD.active, SIGNED), CHAR), CONVERT(CONVERT(NEW.active, SIGNED), CHAR), NEW.id_change_set
		WHERE NOT (OLD.active <=> NEW.active)
    ;
END //
DELIMITER ;
