
USE fetchmetrics;

DROP TRIGGER IF EXISTS fetchmetrics.before_insert_DOG_Assessment_Command_Link;
DROP TRIGGER IF EXISTS fetchmetrics.before_update_DOG_Assessment_Command_Link;

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

DELIMITER //
CREATE TRIGGER fetchmetrics.before_update_DOG_Assessment_Command_Link
BEFORE UPDATE ON fetchmetrics.DOG_Assessment_Command_Link
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_Assessment_Command_Link_Audit (
		  id_link
        , name_field
        , value_prev
        , value_new
        , id_change_set
    )
	-- Changed id_assessment
	SELECT NEW.id_link, '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_command
	SELECT NEW.id_link, 'id_command', CONVERT(OLD.id_command, CHAR), CONVERT(NEW.id_command, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_command <=> NEW.id_command)
    /*
	UNION
	-- Changed id_command_modality
	SELECT NEW.id_link, 'id_command_modality', CONVERT(OLD.id_command_modality, CHAR), CONVERT(NEW.id_command_modality, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_command_modality <=> NEW.id_command_modality)
	UNION
	-- Changed id_bribe
	SELECT NEW.id_link, 'id_bribe', CONVERT(OLD.id_bribe, CHAR), CONVERT(NEW.id_bribe, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_bribe <=> NEW.id_bribe)
    */
	UNION
	-- Changed id_reinforcement_schedule
	SELECT NEW.id_link, 'id_reinforcement_schedule', CONVERT(OLD.id_reinforcement_schedule, CHAR), CONVERT(NEW.id_reinforcement_schedule, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_reinforcement_schedule <=> NEW.id_reinforcement_schedule)
	UNION
	-- Changed distance_from_handler_metres
	SELECT NEW.id_link, 'distance_from_handler_metres', CONVERT(OLD.distance_from_handler_metres, CHAR), CONVERT(NEW.distance_from_handler_metres, CHAR), NEW.id_change_set
		WHERE NOT (OLD.distance_from_handler_metres <=> NEW.distance_from_handler_metres)
	UNION
	-- Changed is_in_sight_of_handler
	SELECT NEW.id_link, 'is_in_sight_of_handler', CONVERT(CONVERT(OLD.is_in_sight_of_handler, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_in_sight_of_handler, SIGNED), CHAR), NEW.id_change_set
		WHERE NOT (OLD.is_in_sight_of_handler <=> NEW.is_in_sight_of_handler)
	UNION
	-- Changed is_in_scent_range_of_handler
	SELECT NEW.id_link, 'is_in_scent_range_of_handler', CONVERT(CONVERT(OLD.is_in_scent_range_of_handler, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_in_scent_range_of_handler, SIGNED), CHAR), NEW.id_change_set
		WHERE NOT (OLD.is_in_scent_range_of_handler <=> NEW.is_in_scent_range_of_handler)
	UNION
	-- Changed is_in_hearing_range_of_handler
	SELECT NEW.id_link, 'is_in_hearing_range_of_handler', CONVERT(CONVERT(OLD.is_in_hearing_range_of_handler, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_in_hearing_range_of_handler, SIGNED), CHAR), NEW.id_change_set
		WHERE NOT (OLD.is_in_hearing_range_of_handler <=> NEW.is_in_hearing_range_of_handler)
	UNION
	-- Changed is_on_lead
	SELECT NEW.id_link, 'is_on_lead', CONVERT(CONVERT(OLD.is_on_lead, SIGNED), CHAR), CONVERT(CONVERT(NEW.is_on_lead, SIGNED), CHAR), NEW.id_change_set
		WHERE NOT (OLD.is_on_lead <=> NEW.is_on_lead)
	UNION
	-- Changed trial_count
	SELECT NEW.id_link, 'trial_count', CONVERT(OLD.trial_count, CHAR), CONVERT(NEW.trial_count, CHAR), NEW.id_change_set
		WHERE NOT (OLD.trial_count <=> NEW.trial_count)
	UNION
	-- Changed active
	SELECT NEW.id_link, '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 ;
