
USE fetchmetrics;

DROP TRIGGER IF EXISTS fetchmetrics.before_insert_DOG_Assessment;
DROP TRIGGER IF EXISTS fetchmetrics.before_update_DOG_Assessment;

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

DELIMITER //
CREATE TRIGGER fetchmetrics.before_update_DOG_Assessment
BEFORE UPDATE ON fetchmetrics.DOG_Assessment
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_Audit (
		  id_assessment
        , name_field
        , value_prev
        , value_new
        , id_change_set
    )
    /*
	-- Changed id_assessment_group
	SELECT NEW.id_assessment, 'id_assessment_group', CONVERT(OLD.id_assessment_group, CHAR), CONVERT(NEW.id_assessment_group, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_assessment_group <=> NEW.id_assessment_group)
	UNION
    */
	-- Changed id_assessment_type
	SELECT NEW.id_assessment, 'id_assessment_type', CONVERT(OLD.id_assessment_type, CHAR), CONVERT(NEW.id_assessment_type, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_assessment_type <=> NEW.id_assessment_type)
	UNION
	-- Changed id_weather
	SELECT NEW.id_assessment, 'id_weather', CONVERT(OLD.id_weather, CHAR), CONVERT(NEW.id_weather, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_weather <=> NEW.id_weather)
	UNION
	-- Changed id_lighting_level
	SELECT NEW.id_assessment, 'id_lighting_level', CONVERT(OLD.id_lighting_level, CHAR), CONVERT(NEW.id_lighting_level, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_lighting_level <=> NEW.id_lighting_level)
	UNION
	-- Changed id_location
	SELECT NEW.id_assessment, 'id_location', CONVERT(OLD.id_location, CHAR), CONVERT(NEW.id_location, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_location <=> NEW.id_location)
	UNION
	-- Changed id_user_handler
	SELECT NEW.id_assessment, 'id_user_handler', CONVERT(OLD.id_user_handler, CHAR), CONVERT(NEW.id_user_handler, CHAR), NEW.id_change_set
		WHERE NOT (OLD.id_user_handler <=> NEW.id_user_handler)
	UNION
	-- Changed name
	SELECT NEW.id_assessment, 'name', OLD.name, NEW.name, NEW.id_change_set
		WHERE NOT (OLD.name <=> NEW.name)
	UNION
	-- Changed notes
	SELECT NEW.id_assessment, 'notes', OLD.notes, NEW.notes, NEW.id_change_set
		WHERE NOT (OLD.notes <=> NEW.notes)
	UNION
	-- Changed temperature_celcius
	SELECT NEW.id_assessment, 'temperature_celcius', CONVERT(OLD.temperature_celcius, CHAR), CONVERT(NEW.temperature_celcius, CHAR), NEW.id_change_set
		WHERE NOT (OLD.temperature_celcius <=> NEW.temperature_celcius)
	UNION
	-- Changed difficulty_level
	SELECT NEW.id_assessment, 'difficulty_level', CONVERT(OLD.difficulty_level, CHAR), CONVERT(NEW.difficulty_level, CHAR), NEW.id_change_set
		WHERE NOT (OLD.difficulty_level <=> NEW.difficulty_level)
	UNION
	-- Changed start_on
	SELECT NEW.id_assessment, 'start_on', CONVERT(OLD.start_on, CHAR), CONVERT(NEW.start_on, CHAR), NEW.id_change_set
		WHERE NOT (OLD.start_on <=> NEW.start_on)
	UNION
	-- Changed end_on
	SELECT NEW.id_assessment, 'end_on', CONVERT(OLD.end_on, CHAR), CONVERT(NEW.end_on, CHAR), NEW.id_change_set
		WHERE NOT (OLD.end_on <=> NEW.end_on)
	UNION
	-- Changed active
	SELECT NEW.id_assessment, '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 ;
