 
USE fetchmetrics;

DROP PROCEDURE IF EXISTS fetchmetrics.p_dog_calc_distraction;

DELIMITER //
CREATE PROCEDURE fetchmetrics.p_dog_calc_distraction (
      IN a_guid BINARY(36)
    , IN a_id_user INT
    , IN a_get_all_distraction BIT
	, IN a_get_inactive_distraction BIT
	, IN a_ids_distraction TEXT
	, IN a_notes_distraction TEXT
    , IN a_min_quantity_distraction INT
    , IN a_max_quantity_distraction INT
    , IN a_min_proximity_metres_distraction FLOAT
    , IN a_max_proximity_metres_distraction FLOAT
    , IN a_get_all_distraction_type BIT
	, IN a_get_inactive_distraction_type BIT
	, IN a_ids_distraction_type TEXT
    , IN a_names_distraction_type TEXT
    , IN a_get_all_intensity_level_emotional BIT
	, IN a_get_inactive_intensity_level_emotional BIT
	, IN a_ids_intensity_level_emotional TEXT
    , IN a_names_intensity_level_emotional TEXT
    , IN a_get_all_intensity_level_scent BIT
	, IN a_get_inactive_intensity_level_scent BIT
	, IN a_ids_intensity_level_scent TEXT
    , IN a_names_intensity_level_scent TEXT
    , IN a_get_all_intensity_level_sight BIT
	, IN a_get_inactive_intensity_level_sight BIT
	, IN a_ids_intensity_level_sight TEXT
    , IN a_names_intensity_level_sight TEXT
    , IN a_get_all_intensity_level_sound BIT
	, IN a_get_inactive_intensity_level_sound BIT
	, IN a_ids_intensity_level_sound TEXT
    , IN a_names_intensity_level_sound TEXT
    , IN a_get_all_intensity_level_touch BIT
	, IN a_get_inactive_intensity_level_touch BIT
	, IN a_ids_intensity_level_touch TEXT
    , IN a_names_intensity_level_touch TEXT
    
    , IN a_get_all_AC_Link BIT
	, IN a_get_inactive_AC_Link BIT
	, IN a_ids_AC_Link TEXT
    , IN a_min_distance_from_handler_metres_AC_Link FLOAT
    , IN a_max_distance_from_handler_metres_AC_Link FLOAT
    , IN a_value_is_in_sight_of_handler_AC_Link BIT
    , IN a_value_is_in_scent_range_of_handler_AC_Link BIT
    , IN a_value_is_in_hearing_range_of_handler_AC_Link BIT
    , IN a_value_is_on_lead_AC_Link BIT
    , IN a_min_trial_count_AC_Link FLOAT
    , IN a_max_trial_count_AC_Link FLOAT

    , IN a_get_all_assessment BIT
	, IN a_get_inactive_assessment BIT
	, IN a_ids_assessment TEXT
    , IN a_notes_assessment TEXT
    , IN a_min_temperature_assessment DECIMAL(5, 2)
    , IN a_max_temperature_assessment DECIMAL(5, 2)
    , IN a_get_all_assessment_type BIT
	, IN a_get_inactive_assessment_type BIT
	, IN a_ids_assessment_type TEXT
    , IN a_names_assessment_type TEXT
    , IN a_get_all_weather BIT
	, IN a_get_inactive_weather BIT
	, IN a_ids_weather TEXT
    , IN a_names_weather TEXT
    , IN a_get_all_lighting_level BIT
	, IN a_get_inactive_lighting_level BIT
	, IN a_ids_lighting_level TEXT
    , IN a_names_lighting_level TEXT
    , IN a_get_all_location BIT
	, IN a_get_inactive_location BIT
	, IN a_ids_location TEXT
    , IN a_names_location TEXT
    , IN a_get_all_user_handler BIT
    , IN a_get_inactive_user_handler BIT
	, IN a_ids_user_handler TEXT
    -- , IN a_auth0_ids_user_handler TEXT
    , IN a_names_user_handler TEXT
    , IN a_emails_user_handler TEXT

    , IN a_get_all_command_category BIT
	, IN a_get_inactive_command_category BIT
	, IN a_ids_command_category TEXT
    , IN a_names_command_category TEXT
    , IN a_get_all_command BIT
	, IN a_get_inactive_command BIT
	, IN a_ids_command TEXT
    , IN a_names_command TEXT
    , IN a_hand_signal_default_descriptions_command TEXT
    , IN a_notes_command TEXT

    , IN a_get_all_reinforcement_schedule BIT
	, IN a_get_inactive_reinforcement_schedule BIT
	, IN a_ids_reinforcement_schedule TEXT
    , IN a_names_reinforcement_schedule TEXT

    , IN a_require_all_id_search_filters_met BIT
    , IN a_require_any_id_search_filters_met BIT
    , IN a_require_all_non_id_search_filters_met BIT
    , IN a_require_any_non_id_search_filters_met BIT
    , IN a_show_errors BIT
    , IN a_debug BIT
)
BEGIN
    DECLARE v_can_view BIT;
    DECLARE v_code_type_error_bad_data VARCHAR(100);
    DECLARE v_code_type_error_no_permission VARCHAR(100);
    DECLARE v_has_filter_distraction_id BIT;
    DECLARE v_has_filter_distraction_notes BIT;
    DECLARE v_has_filter_distraction_quantity_min BIT;
    DECLARE v_has_filter_distraction_quantity_max BIT;
    DECLARE v_has_filter_distraction_proximity_metres_min BIT;
    DECLARE v_has_filter_distraction_proximity_metres_max BIT;
    DECLARE v_id_access_level_view INT;
    DECLARE v_id_minimum INT;
    DECLARE v_id_permission_dog_view INT;
    DECLARE v_id_type_error_bad_data INT;
    DECLARE v_id_type_error_no_permission INT;
    DECLARE v_time_start TIMESTAMP(6);
    
    DECLARE exit handler for SQLEXCEPTION
    BEGIN
        GET DIAGNOSTICS CONDITION 1
            @sqlstate = RETURNED_SQLSTATE
            , @errno = MYSQL_ERRNO
            , @text = MESSAGE_TEXT
		;
        
        ROLLBACK;
        
		CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error_Calc_Distraction (
			  id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
			, id_type INT
			, code VARCHAR(250) NOT NULL
			, msg TEXT NOT NULL
		);

        INSERT INTO tmp_Msg_Error_Calc_Distraction (
			  id_type
            , code
            , msg
		)
        SELECT 
			  MET.id_type
            , @errno
            , @text
		FROM fetchmetrics.CORE_Msg_Error_Type MET
        WHERE MET.code = 'MYSQL_ERROR'
		;
        
        SELECT 
            t_ERROR.id_error
            , t_ERROR.id_type
            , t_ERROR.code
            , ERROR_TYPE.name
            , ERROR_TYPE.description
            , ERROR_TYPE.is_breaking_error
            , ERROR_TYPE.background_colour
            , ERROR_TYPE.text_colour
            , t_ERROR.msg
        FROM tmp_Msg_Error_Calc_Distraction t_ERROR
        INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
        ;
        
		DROP TABLE IF EXISTS tmp_Msg_Error_Calc_Distraction;
    END;

    SET v_time_start := CURRENT_TIMESTAMP(6);
    SET v_code_type_error_bad_data := 'BAD_DATA';
    SET v_code_type_error_no_permission := 'NO_PERMISSION';
    SET v_id_type_error_bad_data := (SELECT ERROR_TYPE.id_type FROM fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_bad_data LIMIT 1);
    SET v_id_type_error_no_permission := (SELECT ERROR_TYPE.id_type FROM fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_no_permission LIMIT 1);
    SET v_id_permission_dog_view := (SELECT PERMISSION.id_permission FROM fetchmetrics.DOG_Permission PERMISSION WHERE PERMISSION.code = 'DOG_VIEW' LIMIT 1);
    SET v_id_access_level_view := (SELECT ACCESS_LEVEL.id_access_level FROM fetchmetrics.DOG_Access_Level ACCESS_LEVEL WHERE ACCESS_LEVEL.code = 'VIEW' LIMIT 1);
    
    
    CALL fetchmetrics.p_core_validate_guid ( a_guid );

    SET a_id_user := IFNULL(a_id_user, 0);
    SET a_get_all_distraction := IFNULL(a_get_all_distraction, 0);
    SET a_get_inactive_distraction := IFNULL(a_get_inactive_distraction, 0);
    SET a_ids_distraction := TRIM(IFNULL(a_ids_distraction, ''));
    SET a_notes_distraction := TRIM(IFNULL(a_notes_distraction, ''));
    /*
    , IN a_min_quantity_distraction INT
    , IN a_max_quantity_distraction INT
    , IN a_min_proximity_metres_distraction FLOAT
    , IN a_max_proximity_metres_distraction FLOAT
    */
    -- Foreign key filters corected in their Calc Stored Procedures
    SET a_require_all_id_search_filters_met := IFNULL(a_require_all_id_search_filters_met, 1);
    SET a_require_any_id_search_filters_met := IFNULL(a_require_any_id_search_filters_met, 1);
    SET a_require_all_non_id_search_filters_met := IFNULL(a_require_all_non_id_search_filters_met, 0);
    SET a_require_any_non_id_search_filters_met := IFNULL(a_require_any_non_id_search_filters_met, 1);
    SET a_show_errors := IFNULL(a_show_errors, 0);
    SET a_debug := IFNULL(a_debug, 0);
    
    IF a_debug = 1 THEN
		SELECT 
              a_guid
			, a_id_user
            , a_get_all_distraction
            , a_get_inactive_distraction
            , a_ids_distraction
            , a_notes_distraction
            , a_min_quantity_distraction
            , a_max_quantity_distraction
            , a_min_proximity_metres_distraction
            , a_max_proximity_metres_distraction
            , a_get_all_distraction_type
            , a_get_inactive_distraction_type
            , a_ids_distraction_type
            , a_names_distraction_type
            , a_get_all_intensity_level_emotional
            , a_get_inactive_intensity_level_emotional
            , a_ids_intensity_level_emotional
            , a_names_intensity_level_emotional 
            , a_get_all_intensity_level_scent
            , a_get_inactive_intensity_level_scent
            , a_ids_intensity_level_scent
            , a_names_intensity_level_scent
            , a_get_all_intensity_level_sight
            , a_get_inactive_intensity_level_sight
            , a_ids_intensity_level_sight
            , a_names_intensity_level_sight
            , a_get_all_intensity_level_sound
            , a_get_inactive_intensity_level_sound
            , a_ids_intensity_level_sound
            , a_names_intensity_level_sound
            , a_get_all_intensity_level_touch
            , a_get_inactive_intensity_level_touch
            , a_ids_intensity_level_touch
            , a_names_intensity_level_touch

            , a_get_all_AC_link
            , a_get_inactive_AC_link
            , a_ids_AC_link
            , a_min_distance_from_handler_metres_AC_Link
            , a_max_distance_from_handler_metres_AC_Link
            , a_value_is_in_sight_of_handler_AC_Link
            , a_value_is_in_scent_range_of_handler_AC_Link
            , a_value_is_in_hearing_range_of_handler_AC_Link
            , a_value_is_on_lead_AC_Link
            , a_min_trial_count_AC_Link
            , a_max_trial_count_AC_Link
            
            , a_get_all_assessment
            , a_get_inactive_assessment
            , a_ids_assessment
            , a_notes_assessment
            , a_min_temperature_assessment
            , a_max_temperature_assessment
            , a_get_all_assessment_type
            , a_get_inactive_assessment_type
            , a_ids_assessment_type
            , a_names_assessment_type
            , a_get_all_weather
            , a_get_inactive_weather
            , a_ids_weather
            , a_names_weather
            , a_get_all_lighting_level
            , a_get_inactive_lighting_level
            , a_ids_lighting_level
            , a_names_lighting_level
            , a_get_all_location
            , a_get_inactive_location
            , a_ids_location
            , a_names_location
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            -- , a_auth0_ids_user_handler
            , a_names_user_handler
            , a_emails_user_handler
            
            , a_get_all_command_category
            , a_get_inactive_command_category
            , a_ids_command_category
            , a_names_command_category
            , a_get_all_command
            , a_get_inactive_command
            , a_ids_command
            , a_names_command
            , a_hand_signal_default_descriptions_command
            , a_notes_command
            
            , a_get_all_reinforcement_schedule
            , a_get_inactive_reinforcement_schedule
            , a_ids_reinforcement_schedule
            , a_names_reinforcement_schedule

            , a_require_all_id_search_filters_met
            , a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met
            , a_require_any_non_id_search_filters_met
            , a_show_errors
            , a_debug
		;

        SELECT
              v_id_type_error_bad_data
            , v_id_type_error_no_permission
            , v_id_permission_dog_view
            , v_time_start
        ;
    END IF;

    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Notes_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Distraction_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Touch_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Sound_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Sight_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Scent_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Emotional_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Distraction_Type_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Assessment_Command_Link_Calc_Distraction;

    CREATE TEMPORARY TABLE tmp_Assessment_Command_Link_Calc_Distraction (
		  id_link INT NOT NULL
        , id_assessment INT
        , id_command INT
        , id_reinforcement_schedule INT
        , distance_from_handler_metres FLOAT
        , is_in_sight_of_handler BIT
        , is_in_scent_range_of_handler BIT
        , is_in_hearing_range_of_handler BIT
        , is_on_lead BIT
        , trial_count INT
        , active BIT
        , temperature_celcius DECIMAL(5, 2)
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );

    CREATE TEMPORARY TABLE tmp_Distraction_Type_Calc_Distraction (
		  id_type INT NOT NULL
        , code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );

    CREATE TEMPORARY TABLE tmp_Intensity_Level_Emotional_Calc_Distraction (
		  id_intensity_level INT NOT NULL
        , code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );

    CREATE TEMPORARY TABLE tmp_Intensity_Level_Scent_Calc_Distraction (
		  id_intensity_level INT NOT NULL
        , code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );

    CREATE TEMPORARY TABLE tmp_Intensity_Level_Sight_Calc_Distraction (
		  id_intensity_level INT NOT NULL
        , code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );

    CREATE TEMPORARY TABLE tmp_Intensity_Level_Sound_Calc_Distraction (
		  id_intensity_level INT NOT NULL
        , code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );

    CREATE TEMPORARY TABLE tmp_Intensity_Level_Touch_Calc_Distraction (
		  id_intensity_level INT NOT NULL
        , code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );

    CREATE TEMPORARY TABLE tmp_Distraction_Calc_Distraction (
		  id_distraction INT NOT NULL
        , id_assessment_command_link INT
        , id_distraction_type INT
        , id_intensity_level_emotional INT
        , id_intensity_level_scent INT
        , id_intensity_level_sight INT
        , id_intensity_level_sound INT
        , id_intensity_level_touch INT
        , quantity INT
        , proximity_metres FLOAT
    --    , difficulty_level DOUBLE
        , notes TEXT
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );
    
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error_Calc_Distraction (
          id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
		, id_type INT
		, code VARCHAR(250) NOT NULL
        , msg TEXT NOT NULL
	);
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Id_Calc_Distraction (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Id_Calc_Distraction;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Notes_Calc_Distraction (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Notes_Calc_Distraction;
    
    SET v_has_filter_distraction_id := CASE WHEN a_ids_distraction <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_distraction_notes := CASE WHEN a_notes_distraction <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_distraction_quantity_min := NOT ISNULL(a_min_quantity_distraction);
    SET v_has_filter_distraction_quantity_max := NOT ISNULL(a_max_quantity_distraction);
    SET v_has_filter_distraction_proximity_metres_min := NOT ISNULL(a_min_proximity_metres_distraction);
    SET v_has_filter_distraction_proximity_metres_max := NOT ISNULL(a_max_proximity_metres_distraction);

    -- Call Calc Assessment Command Link
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_AC_link
                , a_get_inactive_AC_link
                , a_ids_AC_link
                , a_min_distance_from_handler_metres_AC_Link
                , a_max_distance_from_handler_metres_AC_Link
                , a_value_is_in_sight_of_handler_AC_Link
                , a_value_is_in_scent_range_of_handler_AC_Link
                , a_value_is_in_hearing_range_of_handler_AC_Link
                , a_value_is_on_lead_AC_Link
                , a_min_trial_count_AC_Link
                , a_max_trial_count_AC_Link
                
                , a_get_all_assessment
                , a_get_inactive_assessment
                , a_ids_assessment
                , a_notes_assessment
                , a_min_temperature_assessment
                , a_max_temperature_assessment
                , a_get_all_assessment_type
                , a_get_inactive_assessment_type
                , a_ids_assessment_type
                , a_names_assessment_type
                , a_get_all_weather
                , a_get_inactive_weather
                , a_ids_weather
                , a_names_weather
                , a_get_all_lighting_level
                , a_get_inactive_lighting_level
                , a_ids_lighting_level
                , a_names_lighting_level
                , a_get_all_location
                , a_get_inactive_location
                , a_ids_location
                , a_names_location
                , a_get_all_user_handler
                , a_get_inactive_user_handler
                , a_ids_user_handler
                -- , a_auth0_ids_user_handler
                , a_names_user_handler
                , a_emails_user_handler
                
                , a_get_all_command_category
                , a_get_inactive_command_category
                , a_ids_command_category
                , a_names_command_category
                , a_get_all_command
                , a_get_inactive_command
                , a_ids_command
                , a_names_command
                , a_hand_signal_default_descriptions_command
                , a_notes_command
                
                , a_get_all_reinforcement_schedule
                , a_get_inactive_reinforcement_schedule
                , a_ids_reinforcement_schedule
                , a_names_reinforcement_schedule

                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_assessment_command_link (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_AC_link
            , a_get_inactive_AC_link
            , a_ids_AC_link
            , a_min_distance_from_handler_metres_AC_Link
            , a_max_distance_from_handler_metres_AC_Link
            , a_value_is_in_sight_of_handler_AC_Link
            , a_value_is_in_scent_range_of_handler_AC_Link
            , a_value_is_in_hearing_range_of_handler_AC_Link
            , a_value_is_on_lead_AC_Link
            , a_min_trial_count_AC_Link
            , a_max_trial_count_AC_Link
            
            , a_get_all_assessment
            , a_get_inactive_assessment
            , a_ids_assessment
            , a_notes_assessment
            , a_min_temperature_assessment
            , a_max_temperature_assessment
            , a_get_all_assessment_type
            , a_get_inactive_assessment_type
            , a_ids_assessment_type
            , a_names_assessment_type
            , a_get_all_weather
            , a_get_inactive_weather
            , a_ids_weather
            , a_names_weather
            , a_get_all_lighting_level
            , a_get_inactive_lighting_level
            , a_ids_lighting_level
            , a_names_lighting_level
            , a_get_all_location
            , a_get_inactive_location
            , a_ids_location
            , a_names_location
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            -- , a_auth0_ids_user_handler
            , a_names_user_handler
            , a_emails_user_handler
            
            , a_get_all_command_category
            , a_get_inactive_command_category
            , a_ids_command_category
            , a_names_command_category
            , a_get_all_command
            , a_get_inactive_command
            , a_ids_command
            , a_names_command
            , a_hand_signal_default_descriptions_command
            , a_notes_command
            
            , a_get_all_reinforcement_schedule
            , a_get_inactive_reinforcement_schedule
            , a_ids_reinforcement_schedule
            , a_names_reinforcement_schedule

            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
            , a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        IF a_debug = 1 THEN
            SELECT COUNT(*) FROM fetchmetrics.DOG_Assessment_Command_Link_Temp;
            SELECT * FROM fetchmetrics.DOG_Assessment_Command_Link_Temp;
        END IF;
        
        INSERT INTO tmp_Assessment_Command_Link_Calc_Distraction (
              id_link
            , id_assessment
            , id_command
            , id_reinforcement_schedule
            , distance_from_handler_metres
            , is_in_sight_of_handler
            , is_in_scent_range_of_handler
            , is_in_hearing_range_of_handler
            , is_on_lead
            , trial_count
            , active
            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              ASSESSMENT_COMMAND_LINK_T.id_link
            , ASSESSMENT_COMMAND_LINK_T.id_assessment
            , ASSESSMENT_COMMAND_LINK_T.id_command
            , ASSESSMENT_COMMAND_LINK_T.id_reinforcement_schedule
            , ASSESSMENT_COMMAND_LINK_T.distance_from_handler_metres
            , ASSESSMENT_COMMAND_LINK_T.is_in_sight_of_handler
            , ASSESSMENT_COMMAND_LINK_T.is_in_scent_range_of_handler
            , ASSESSMENT_COMMAND_LINK_T.is_in_hearing_range_of_handler
            , ASSESSMENT_COMMAND_LINK_T.is_on_lead
            , ASSESSMENT_COMMAND_LINK_T.trial_count
            , ASSESSMENT_COMMAND_LINK_T.active
            , ASSESSMENT_COMMAND_LINK_T.does_meet_id_filters
            , ASSESSMENT_COMMAND_LINK_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Assessment_Command_Link_Temp ASSESSMENT_COMMAND_LINK_T
        WHERE ASSESSMENT_COMMAND_LINK_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT COUNT(*) FROM tmp_Assessment_Command_Link_Calc_Distraction;
            SELECT * FROM tmp_Assessment_Command_Link_Calc_Distraction;
        END IF;
    END IF;

    -- Call Calc Distraction Type
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_distraction_type -- a_get_all_distraction_type
                , a_get_inactive_distraction_type -- a_get_inactive_distraction_type
                , a_ids_distraction_type -- a_ids_distraction_type
                , a_names_distraction_type -- a_names_distraction_type
                , a_get_all_user_handler
                , a_get_inactive_user_handler
                , a_ids_user_handler
                , a_names_user_handler
                , a_emails_user_handler
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_distraction_type (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_distraction_type -- a_get_all_distraction_type
            , a_get_inactive_distraction_type -- a_get_inactive_distraction_type
            , a_ids_distraction_type -- a_ids_distraction_type
            , a_names_distraction_type -- a_names_distraction_type
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            , a_names_user_handler
            , a_emails_user_handler
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Distraction_Type_Calc_Distraction (
              id_type
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              DISTRACTION_TYPE_T.id_type
            , DISTRACTION_TYPE_T.code
            , DISTRACTION_TYPE_T.name
            , DISTRACTION_TYPE_T.active

            , DISTRACTION_TYPE_T.does_meet_id_filters
            , DISTRACTION_TYPE_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Distraction_Type_Temp DISTRACTION_TYPE_T
        WHERE DISTRACTION_TYPE_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Distraction_Type_Calc_Distraction;
        END IF;
    END IF;

    -- Call Calc Intensity Level Emotional
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_intensity_level_emotional -- a_get_all_intensity_level_emotional
                , a_get_inactive_intensity_level_emotional -- a_get_inactive_intensity_level_emotional
                , a_ids_intensity_level_emotional -- a_ids_intensity_level_emotional
                , a_names_intensity_level_emotional -- a_names_intensity_level_emotional
                , a_get_all_user_handler
                , a_get_inactive_user_handler
                , a_ids_user_handler
                , a_names_user_handler
                , a_emails_user_handler
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_distraction_intensity_level (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_intensity_level_emotional -- a_get_all_intensity_level_emotional
            , a_get_inactive_intensity_level_emotional -- a_get_inactive_intensity_level_emotional
            , a_ids_intensity_level_emotional -- a_ids_intensity_level_emotional
            , a_names_intensity_level_emotional -- a_names_intensity_level_emotional
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            , a_names_user_handler
            , a_emails_user_handler
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Intensity_Level_Emotional_Calc_Distraction (
              id_intensity_level
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              INTENSITY_LEVEL_EMOTIONAL_T.id_intensity_level
            , INTENSITY_LEVEL_EMOTIONAL_T.code
            , INTENSITY_LEVEL_EMOTIONAL_T.name
            , INTENSITY_LEVEL_EMOTIONAL_T.active

            , INTENSITY_LEVEL_EMOTIONAL_T.does_meet_id_filters
            , INTENSITY_LEVEL_EMOTIONAL_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Distraction_Intensity_Level_Temp INTENSITY_LEVEL_EMOTIONAL_T
        WHERE INTENSITY_LEVEL_EMOTIONAL_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Intensity_Level_Emotional_Calc_Distraction;
        END IF;

        CALL fetchmetrics.p_dog_clear_calc_distraction_intensity_level (
            a_guid
            , 0 -- a_debug
        );
    END IF;

    -- Call Calc Intensity Level Scent
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_intensity_level_scent -- a_get_all_intensity_level_scent
                , a_get_inactive_intensity_level_scent -- a_get_inactive_intensity_level_scent
                , a_ids_intensity_level_scent -- a_ids_intensity_level_scent
                , a_names_intensity_level_scent -- a_names_intensity_level_scent
                , a_get_all_user_handler
                , a_get_inactive_user_handler
                , a_ids_user_handler
                , a_names_user_handler
                , a_emails_user_handler
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_distraction_intensity_level (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_intensity_level_scent -- a_get_all_intensity_level_scent
            , a_get_inactive_intensity_level_scent -- a_get_inactive_intensity_level_scent
            , a_ids_intensity_level_scent -- a_ids_intensity_level_scent
            , a_names_intensity_level_scent -- a_names_intensity_level_scent
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            , a_names_user_handler
            , a_emails_user_handler
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Intensity_Level_Scent_Calc_Distraction (
              id_intensity_level
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              INTENSITY_LEVEL_SCENT_T.id_intensity_level
            , INTENSITY_LEVEL_SCENT_T.code
            , INTENSITY_LEVEL_SCENT_T.name
            , INTENSITY_LEVEL_SCENT_T.active

            , INTENSITY_LEVEL_SCENT_T.does_meet_id_filters
            , INTENSITY_LEVEL_SCENT_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Distraction_Intensity_Level_Temp INTENSITY_LEVEL_SCENT_T
        WHERE INTENSITY_LEVEL_SCENT_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Intensity_Level_Scent_Calc_Distraction;
        END IF;

        CALL fetchmetrics.p_dog_clear_calc_distraction_intensity_level (
            a_guid
            , 0 -- a_debug
        );
    END IF;

    -- Call Calc Intensity Level Sight
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_intensity_level_sight -- a_get_all_intensity_level_sight
                , a_get_inactive_intensity_level_sight -- a_get_inactive_intensity_level_sight
                , a_ids_intensity_level_sight -- a_ids_intensity_level_sight
                , a_names_intensity_level_sight -- a_names_intensity_level_sight
                , a_get_all_user_handler
                , a_get_inactive_user_handler
                , a_ids_user_handler
                , a_names_user_handler
                , a_emails_user_handler
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_distraction_intensity_level (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_intensity_level_sight -- a_get_all_intensity_level_sight
            , a_get_inactive_intensity_level_sight -- a_get_inactive_intensity_level_sight
            , a_ids_intensity_level_sight -- a_ids_intensity_level_sight
            , a_names_intensity_level_sight -- a_names_intensity_level_sight
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            , a_names_user_handler
            , a_emails_user_handler
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Intensity_Level_Sight_Calc_Distraction (
              id_intensity_level
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              INTENSITY_LEVEL_SIGHT_T.id_intensity_level
            , INTENSITY_LEVEL_SIGHT_T.code
            , INTENSITY_LEVEL_SIGHT_T.name
            , INTENSITY_LEVEL_SIGHT_T.active

            , INTENSITY_LEVEL_SIGHT_T.does_meet_id_filters
            , INTENSITY_LEVEL_SIGHT_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Distraction_Intensity_Level_Temp INTENSITY_LEVEL_SIGHT_T
        WHERE INTENSITY_LEVEL_SIGHT_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Intensity_Level_Sight_Calc_Distraction;
        END IF;

        CALL fetchmetrics.p_dog_clear_calc_distraction_intensity_level (
            a_guid
            , 0 -- a_debug
        );
    END IF;

    -- Call Calc Intensity Level Sound
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_intensity_level_sound -- a_get_all_intensity_level_sound
                , a_get_inactive_intensity_level_sound -- a_get_inactive_intensity_level_sound
                , a_ids_intensity_level_sound -- a_ids_intensity_level_sound
                , a_names_intensity_level_sound -- a_names_intensity_level_sound
                , a_get_all_user_handler
                , a_get_inactive_user_handler
                , a_ids_user_handler
                , a_names_user_handler
                , a_emails_user_handler
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_distraction_intensity_level (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_intensity_level_sound -- a_get_all_intensity_level_sound
            , a_get_inactive_intensity_level_sound -- a_get_inactive_intensity_level_sound
            , a_ids_intensity_level_sound -- a_ids_intensity_level_sound
            , a_names_intensity_level_sound -- a_names_intensity_level_sound
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            , a_names_user_handler
            , a_emails_user_handler
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Intensity_Level_Sound_Calc_Distraction (
              id_intensity_level
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              INTENSITY_LEVEL_SOUND_T.id_intensity_level
            , INTENSITY_LEVEL_SOUND_T.code
            , INTENSITY_LEVEL_SOUND_T.name
            , INTENSITY_LEVEL_SOUND_T.active

            , INTENSITY_LEVEL_SOUND_T.does_meet_id_filters
            , INTENSITY_LEVEL_SOUND_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Distraction_Intensity_Level_Temp INTENSITY_LEVEL_SOUND_T
        WHERE INTENSITY_LEVEL_SOUND_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Intensity_Level_Sound_Calc_Distraction;
        END IF;

        CALL fetchmetrics.p_dog_clear_calc_distraction_intensity_level (
            a_guid
            , 0 -- a_debug
        );
    END IF;

    -- Call Calc Intensity Level Touch
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_intensity_level_touch -- a_get_all_intensity_level_touch
                , a_get_inactive_intensity_level_touch -- a_get_inactive_intensity_level_touch
                , a_ids_intensity_level_touch -- a_ids_intensity_level_touch
                , a_names_intensity_level_touch -- a_names_intensity_level_touch
                , a_get_all_user_handler
                , a_get_inactive_user_handler
                , a_ids_user_handler
                , a_names_user_handler
                , a_emails_user_handler
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_distraction_intensity_level (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_intensity_level_touch -- a_get_all_intensity_level_touch
            , a_get_inactive_intensity_level_touch -- a_get_inactive_intensity_level_touch
            , a_ids_intensity_level_touch -- a_ids_intensity_level_touch
            , a_names_intensity_level_touch -- a_names_intensity_level_touch
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            , a_names_user_handler
            , a_emails_user_handler
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Intensity_Level_Touch_Calc_Distraction (
              id_intensity_level
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              INTENSITY_LEVEL_TOUCH_T.id_intensity_level
            , INTENSITY_LEVEL_TOUCH_T.code
            , INTENSITY_LEVEL_TOUCH_T.name
            , INTENSITY_LEVEL_TOUCH_T.active

            , INTENSITY_LEVEL_TOUCH_T.does_meet_id_filters
            , INTENSITY_LEVEL_TOUCH_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Distraction_Intensity_Level_Temp INTENSITY_LEVEL_TOUCH_T
        WHERE INTENSITY_LEVEL_TOUCH_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Intensity_Level_Touch_Calc_Distraction;
        END IF;

        CALL fetchmetrics.p_dog_clear_calc_distraction_intensity_level (
            a_guid
            , 0 -- a_debug
        );
    END IF;

    -- Distractions
    IF v_has_filter_distraction_id = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_ids_distraction, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Id_Calc_Distraction (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CAST(SPLIT_T.substring AS DECIMAL(10,0)) AS as_int
		FROM fetchmetrics.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = a_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL fetchmetrics.p_core_clear_split( a_guid );
	END IF;

    IF v_has_filter_distraction_notes = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_notes_distraction, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Notes_Calc_Distraction (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CAST(SPLIT_T.substring AS DECIMAL(10,0)) AS as_int
		FROM fetchmetrics.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = a_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL fetchmetrics.p_core_clear_split( a_guid );
	END IF;
    
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
		IF EXISTS (
			SELECT * 
            FROM tmp_Split_Id_Calc_Distraction t_SPLIT_ID
            LEFT JOIN fetchmetrics.DOG_Distraction DISTRACTION ON t_SPLIT_ID.as_int = DISTRACTION.id_distraction 
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(DISTRACTION.id_distraction)
				OR  (
					DISTRACTION.active = 0
					AND a_get_inactive_distraction = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error_Calc_Distraction (
				  id_type
		        , code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Distraction IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_ID.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Id_Calc_Distraction t_SPLIT_ID
			LEFT JOIN fetchmetrics.DOG_Distraction DISTRACTION ON t_SPLIT_ID.as_int = DISTRACTION.id_distraction
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(DISTRACTION.id_distraction)
				OR  (
					DISTRACTION.active = 0
					AND a_get_inactive_distraction = 0
				)
			;
        /* Don't error on notes not found
		ELSEIF EXISTS ()
        */
		ELSE
			INSERT INTO tmp_Distraction_Calc_Distraction (
				  id_distraction
                , id_assessment_command_link
                , id_distraction_type
                , id_intensity_level_emotional
                , id_intensity_level_scent
                , id_intensity_level_sight
                , id_intensity_level_sound
                , id_intensity_level_touch
                , quantity
                , proximity_metres
                , notes
                , active
                , does_meet_id_filters
                , does_meet_non_id_filters
			)
            WITH 
                Distraction_Id_Filter AS (
                    SELECT DISTRACTION.id_distraction
                    FROM tmp_Split_Id_Calc_Distraction t_SPLIT_ID
                    INNER JOIN fetchmetrics.DOG_Distraction DISTRACTION ON t_SPLIT_ID.as_int = DISTRACTION.id_distraction
                )
                , Distraction_Notes_Filter AS (
                    SELECT DISTRACTION.id_distraction
                    FROM tmp_Split_Notes_Calc_Distraction t_SPLIT_NOTES
                    INNER JOIN fetchmetrics.DOG_Distraction DISTRACTION ON DISTRACTION.notes LIKE CONCAT('%', t_SPLIT_NOTES.substring, '%')
                    WHERE 
                            t_SPLIT_NOTES.substring IS NOT NULL
                        AND t_SPLIT_NOTES.substring <> ''
                )
                , Distraction_Filters AS (
                    SELECT 
                        DISTRACTION_COMBINED.id_distraction
                        , MAX(DISTRACTION_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                        , MAX(DISTRACTION_COMBINED.does_meet_notes_filter) AS does_meet_notes_filter
                    FROM (
                        SELECT 
                            DISTRACTION_ID_FILTER.id_distraction 
                            , 1 AS does_meet_id_filter
                            , 0 AS does_meet_notes_filter
                        FROM Distraction_Id_Filter DISTRACTION_ID_FILTER
                        UNION
                        SELECT 
                            DISTRACTION_NOTES_FILTER.id_distraction 
                            , 0 AS does_meet_id_filter
                            , 1 AS does_meet_notes_filter
                        FROM Distraction_Notes_Filter DISTRACTION_NOTES_FILTER
                    ) DISTRACTION_COMBINED
                    GROUP BY DISTRACTION_COMBINED.id_distraction
                )
            SELECT 
                  DISTRACTION.id_distraction
                , DISTRACTION.id_assessment_command_link
                , DISTRACTION.id_distraction_type
                , DISTRACTION.id_intensity_level_emotional
                , DISTRACTION.id_intensity_level_scent
                , DISTRACTION.id_intensity_level_sight
                , DISTRACTION.id_intensity_level_sound
                , DISTRACTION.id_intensity_level_touch
                , DISTRACTION.quantity
                , DISTRACTION.proximity_metres
                , DISTRACTION.notes
                , DISTRACTION.active
                , CASE WHEN 
                        v_has_filter_distraction_id = 0 
                    OR  DISTRACTION_FILTERS.does_meet_id_filter = 1
                THEN 1 ELSE 0 END AS does_meet_id_filters
                , CASE WHEN 
                        (
                            v_has_filter_distraction_notes = 0
                        AND v_has_filter_distraction_quantity_min = 0
                        AND v_has_filter_distraction_quantity_max = 0
                        AND v_has_filter_distraction_proximity_metres_min = 0
                        AND v_has_filter_distraction_proximity_metres_max = 0
                    )
                    OR  DISTRACTION_FILTERS.does_meet_notes_filter = 1
                    OR  (
                            v_has_filter_distraction_quantity_min = 0
                        OR  (
                                v_has_filter_distraction_quantity_min = 1
                            AND DISTRACTION.quantity >= v_has_filter_distraction_quantity_min
                        )
                    )
                    OR  (
                            v_has_filter_distraction_quantity_max = 0
                        OR  (
                                v_has_filter_distraction_quantity_max = 1
                            AND DISTRACTION.quantity <= v_has_filter_distraction_quantity_max
                        )
                    )
                    OR  (
                            v_has_filter_distraction_proximity_metres_min = 0
                        OR  (
                                v_has_filter_distraction_proximity_metres_min = 1
                            AND DISTRACTION.proximity_metres >= v_has_filter_distraction_proximity_metres_min
                        )
                    )
                    OR  (
                            v_has_filter_distraction_proximity_metres_max = 0
                        OR  (
                                v_has_filter_distraction_proximity_metres_max = 1
                            AND DISTRACTION.proximity_metres <= v_has_filter_distraction_proximity_metres_max
                        )
                    )
                THEN 1 ELSE 0 END AS does_meet_non_id_filters
            FROM fetchmetrics.DOG_Distraction DISTRACTION
            LEFT JOIN Distraction_Filters DISTRACTION_FILTERS ON DISTRACTION.id_distraction = DISTRACTION_FILTERS.id_distraction
            WHERE 
                (
                        a_get_all_distraction = 1
                    OR  (
                            v_has_filter_distraction_id = 1
                        AND DISTRACTION_FILTERS.does_meet_id_filter = 1
                    )
                    OR  (
                            v_has_filter_distraction_notes = 1
                        AND DISTRACTION_FILTERS.does_meet_notes_filter = 1
                    )
                    OR  (
                            v_has_filter_distraction_quantity_min = 1
                        AND DISTRACTION.quantity >= v_has_filter_distraction_quantity_min
                    )
                    OR  (
                            v_has_filter_distraction_quantity_max = 1
                        AND DISTRACTION.quantity <= v_has_filter_distraction_quantity_max
                    )
                    OR  (
                            v_has_filter_distraction_proximity_metres_min = 1
                        AND DISTRACTION.proximity_metres >= v_has_filter_distraction_proximity_metres_min
                    )
                    OR  (
                            v_has_filter_distraction_proximity_metres_max = 1
                        AND DISTRACTION.proximity_metres <= v_has_filter_distraction_proximity_metres_max
                    )
                )
                AND (
                        a_get_inactive_distraction = 1
                    OR  DISTRACTION.active = 1
                )
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split_Id_Calc_Distraction;
    DELETE FROM tmp_Split_Notes_Calc_Distraction;

    IF a_debug = 1 THEN
        SELECT 'After get Distractions';
        SELECT * FROM tmp_Distraction_Calc_Distraction;
    END IF;

    -- Filter records
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        DELETE t_DISTRACTION
        FROM tmp_Distraction_Calc_Distraction t_DISTRACTION
        LEFT JOIN tmp_Assessment_Command_Link_Calc_Distraction t_ASSESSMENT_COMMAND_LINK ON t_DISTRACTION.id_assessment_command_link = t_ASSESSMENT_COMMAND_LINK.id_link
        LEFT JOIN tmp_Distraction_Type_Calc_Distraction t_DISTRACTION_TYPE ON t_DISTRACTION.id_distraction_type = t_DISTRACTION_TYPE.id_type
        LEFT JOIN tmp_Intensity_Level_Emotional_Calc_Distraction t_INTENSITY_LEVEL_EMOTIONAL ON t_DISTRACTION.id_intensity_level_emotional = t_INTENSITY_LEVEL_EMOTIONAL.id_intensity_level
        LEFT JOIN tmp_Intensity_Level_Scent_Calc_Distraction t_INTENSITY_LEVEL_SCENT ON t_DISTRACTION.id_intensity_level_scent = t_INTENSITY_LEVEL_SCENT.id_intensity_level
        LEFT JOIN tmp_Intensity_Level_Sight_Calc_Distraction t_INTENSITY_LEVEL_SIGHT ON t_DISTRACTION.id_intensity_level_sight = t_INTENSITY_LEVEL_SIGHT.id_intensity_level
        LEFT JOIN tmp_Intensity_Level_Sound_Calc_Distraction t_INTENSITY_LEVEL_SOUND ON t_DISTRACTION.id_intensity_level_sound = t_INTENSITY_LEVEL_SOUND.id_intensity_level
        LEFT JOIN tmp_Intensity_Level_Touch_Calc_Distraction t_INTENSITY_LEVEL_TOUCH ON t_DISTRACTION.id_intensity_level_touch = t_INTENSITY_LEVEL_TOUCH.id_intensity_level
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND (
                        t_DISTRACTION.does_meet_id_filters = 0
                    OR  IFNULL(t_ASSESSMENT_COMMAND_LINK.does_meet_id_filters, 1) = 0
                    OR  IFNULL(t_DISTRACTION_TYPE.does_meet_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_EMOTIONAL.does_meet_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_SCENT.does_meet_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_SIGHT.does_meet_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_SOUND.does_meet_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_TOUCH.does_meet_id_filters, 1) = 0
                )
            )
            OR  (
                    a_require_all_non_id_search_filters_met = 1
                AND (
                        t_DISTRACTION.does_meet_non_id_filters = 0
                    OR  IFNULL(t_ASSESSMENT_COMMAND_LINK.does_meet_non_id_filters, 1) = 0
                    OR  IFNULL(t_DISTRACTION_TYPE.does_meet_non_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_EMOTIONAL.does_meet_non_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_SCENT.does_meet_non_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_SIGHT.does_meet_non_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_SOUND.does_meet_non_id_filters, 1) = 0
                    OR  IFNULL(t_INTENSITY_LEVEL_TOUCH.does_meet_non_id_filters, 1) = 0
                )
            )
            OR  (
                a_require_any_id_search_filters_met = 1
                AND t_DISTRACTION.does_meet_id_filters = 0
                AND IFNULL(t_ASSESSMENT_COMMAND_LINK.does_meet_id_filters, 1) = 0
                AND IFNULL(t_DISTRACTION_TYPE.does_meet_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_EMOTIONAL.does_meet_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_SCENT.does_meet_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_SIGHT.does_meet_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_SOUND.does_meet_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_TOUCH.does_meet_id_filters, 1) = 0
            )
            OR  (
                a_require_any_non_id_search_filters_met = 1
                AND t_DISTRACTION.does_meet_non_id_filters = 0
                AND IFNULL(t_ASSESSMENT_COMMAND_LINK.does_meet_non_id_filters, 1) = 0
                AND IFNULL(t_DISTRACTION_TYPE.does_meet_non_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_EMOTIONAL.does_meet_non_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_SCENT.does_meet_non_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_SIGHT.does_meet_non_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_SOUND.does_meet_non_id_filters, 1) = 0
                AND IFNULL(t_INTENSITY_LEVEL_TOUCH.does_meet_non_id_filters, 1) = 0
            )
        ;
    END IF;

    IF a_debug = 1 THEN
        SELECT 'After filter Distractions';
        SELECT * FROM tmp_Assessment_Command_Link_Calc_Distraction;
        SELECT * FROM tmp_Distraction_Type_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Emotional_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Scent_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Sight_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Sound_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Touch_Calc_Distraction;
        SELECT * FROM tmp_Distraction_Calc_Distraction;
    END IF;


    -- Permissions
    IF a_debug = 1 THEN
		SELECT 
			  a_guid -- a_guid
            , 0 -- get_all_user
            , 0 -- get_inactive_user
            , a_id_user -- ids_user
            , '' -- a_auth0_ids_user
            , '' -- a_names_user
            , '' -- a_emails_user
            , 1 -- a_require_all_id_search_filters_met
            , 1 -- a_require_any_id_search_filters_met
            , 0 -- a_require_all_non_id_search_filters_met
            , 0 -- a_require_any_non_id_search_filters_met
            , v_id_permission_dog_view -- ids_permission
            , v_id_access_level_view -- ids_access_level
            , 0 -- a_show_errors
			, 0 -- a_debug
        ;
    END IF;
    
	CALL fetchmetrics.p_dog_calc_user_access(
          a_guid -- a_guid
        , 0 -- get_all_user
        , 0 -- get_inactive_user
        , a_id_user -- ids_user
        , '' -- a_auth0_ids_user
        , '' -- a_names_user
        , '' -- a_emails_user
        , 1 -- a_require_all_id_search_filters_met
        , 1 -- a_require_any_id_search_filters_met
        , 0 -- a_require_all_non_id_search_filters_met
        , 0 -- a_require_any_non_id_search_filters_met
        , v_id_permission_dog_view -- ids_permission
        , v_id_access_level_view -- ids_access_level
        , 0 -- a_show_errors
        , 0 -- a_debug
    );
	
    SELECT
          IFNULL(CALC_USER_T.has_access, 0)
    INTO
          v_can_view
    FROM fetchmetrics.DOG_Calc_User_Access_Temp CALC_USER_T
    WHERE CALC_USER_T.GUID = a_guid
    LIMIT 1
    ;
    
    IF a_debug = 1 THEN
        SELECT v_can_view;
    END IF;
    
    IF (v_can_view = 0) THEN
		DELETE t_ME
        FROM tmp_Msg_Error_Calc_Distraction t_ME
        WHERE t_ME.id_type <> v_id_type_error_no_permission
        ;
		INSERT INTO tmp_Msg_Error_Calc_Distraction (
			  id_type
			, code
            , msg
		)
		VALUES (
			  v_id_type_error_no_permission
            , v_code_type_error_no_permission
			, 'You do not have permission to view Dogs and Button Icons.'
		)
        ;
    END IF;
	
	CALL fetchmetrics.p_dog_clear_calc_user_access(
		a_guid
        , 0 -- a_debug
	);

    IF a_debug = 1 THEN
        SELECT 'Before non-permitted data deletion';
        SELECT * FROM tmp_Assessment_Command_Link_Calc_Distraction;
        SELECT * FROM tmp_Distraction_Type_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Emotional_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Scent_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Sight_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Sound_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Touch_Calc_Distraction;
        SELECT * FROM tmp_Distraction_Calc_Distraction;
    END IF;
    
    IF EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Distraction t_ERROR INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Assessment_Command_Link_Calc_Distraction;
            SELECT * FROM tmp_Distraction_Type_Calc_Distraction;
            SELECT * FROM tmp_Intensity_Level_Emotional_Calc_Distraction;
            SELECT * FROM tmp_Intensity_Level_Scent_Calc_Distraction;
            SELECT * FROM tmp_Intensity_Level_Sight_Calc_Distraction;
            SELECT * FROM tmp_Intensity_Level_Sound_Calc_Distraction;
            SELECT * FROM tmp_Intensity_Level_Touch_Calc_Distraction;
            SELECT * FROM tmp_Distraction_Calc_Distraction;
        END IF;

        DELETE FROM tmp_Intensity_Level_Touch_Calc_Distraction;
        DELETE FROM tmp_Intensity_Level_Sound_Calc_Distraction;
        DELETE FROM tmp_Intensity_Level_Sight_Calc_Distraction;
        DELETE FROM tmp_Intensity_Level_Scent_Calc_Distraction;
        DELETE FROM tmp_Intensity_Level_Emotional_Calc_Distraction;
        DELETE FROM tmp_Distraction_Type_Calc_Distraction;
        DELETE FROM tmp_Assessment_Command_Link_Calc_Distraction;
        DELETE FROM tmp_Distraction_Calc_Distraction;
    END IF;

    IF a_debug = 1 THEN
        SELECT 'After non-permitted data deletion';
    END IF;

    -- Outputs
	START TRANSACTION;
        -- Distractions
        INSERT INTO fetchmetrics.DOG_Distraction_Temp (
            guid
            , id_distraction
            , id_assessment_command_link
            , id_distraction_type
            , id_intensity_level_emotional
            , id_intensity_level_scent
            , id_intensity_level_sight
            , id_intensity_level_sound
            , id_intensity_level_touch
            , quantity
            , proximity_metres
            , notes
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT 
            a_guid
            , t_DISTRACTION.id_distraction
            , t_DISTRACTION.id_assessment_command_link
            , t_DISTRACTION.id_distraction_type
            , t_DISTRACTION.id_intensity_level_emotional
            , t_DISTRACTION.id_intensity_level_scent
            , t_DISTRACTION.id_intensity_level_sight
            , t_DISTRACTION.id_intensity_level_sound
            , t_DISTRACTION.id_intensity_level_touch
            , t_DISTRACTION.quantity
            , t_DISTRACTION.proximity_metres
            , t_DISTRACTION.notes
            , t_DISTRACTION.active

            , t_DISTRACTION.does_meet_id_filters
            , t_DISTRACTION.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Distraction DISTRACTION
        INNER JOIN tmp_Distraction_Calc_Distraction t_DISTRACTION ON DISTRACTION.id_distraction = t_DISTRACTION.id_distraction
        ;
    COMMIT;

    -- Errors
    IF a_show_errors = 1 THEN
        SELECT 
            t_ERROR.id_error
            , t_ERROR.id_type
            , t_ERROR.code
            , ERROR_TYPE.name
            , ERROR_TYPE.description
            , ERROR_TYPE.is_breaking_error
            , ERROR_TYPE.background_colour
            , ERROR_TYPE.text_colour
            , t_ERROR.msg
        FROM tmp_Msg_Error_Calc_Distraction t_ERROR
        INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
        ;
    END IF;

    IF a_debug = 1 AND v_can_view = 1 THEN
        SELECT * FROM tmp_Assessment_Calc_Distraction;
        SELECT * FROM tmp_Distraction_Type_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Emotional_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Scent_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Sight_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Sound_Calc_Distraction;
        SELECT * FROM tmp_Intensity_Level_Touch_Calc_Distraction;
        SELECT * FROM tmp_Distraction_Calc_Distraction;
    END IF;

    CALL fetchmetrics.p_dog_clear_calc_assessment_command_link (
        a_guid
        , 0 -- a_debug
    );

    CALL fetchmetrics.p_dog_clear_calc_distraction_type (
        a_guid
        , 0 -- a_debug
    );

    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Notes_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Distraction_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Touch_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Sound_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Sight_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Scent_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Intensity_Level_Emotional_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Distraction_Type_Calc_Distraction;
    DROP TEMPORARY TABLE IF EXISTS tmp_Assessment_Command_Link_Calc_Distraction;
    
    IF a_debug = 1 THEN
		CALL fetchmetrics.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*

CALL fetchmetrics.p_dog_calc_distraction (
      'slobbery                            ' -- a_guid
	, 1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_distraction
	, 0                                     	-- a_get_inactive_distraction
	, ''                                    	-- a_ids_distraction
    , ''                                        -- a_notes_distraction
	, NULL                                     	-- a_min_quantity_distraction
	, NULL                                     	-- a_max_quantity_distraction
	, NULL                                     	-- a_min_proximity_metres_distraction
	, NULL                                     	-- a_max_proximity_metres_distraction

    , 1                                     	-- a_get_all_distraction_type
	, 0                                     	-- a_get_inactive_distraction_type
	, ''                                    	-- a_ids_distraction_type
    , ''                                        -- a_names_distraction_type

    , 1                                     	-- a_get_all_intensity_level_emotional
	, 0                                     	-- a_get_inactive_intensity_level_emotional
	, ''                                    	-- a_ids_intensity_level_emotional
    , ''                                        -- a_names_intensity_level_emotional

    , 1                                     	-- a_get_all_intensity_level_scent
	, 0                                     	-- a_get_inactive_intensity_level_scent
	, ''                                    	-- a_ids_intensity_level_scent
    , ''                                        -- a_names_intensity_level_scent

    , 1                                     	-- a_get_all_intensity_level_sight
	, 0                                     	-- a_get_inactive_intensity_level_sight
	, ''                                    	-- a_ids_intensity_level_sight
    , ''                                        -- a_names_intensity_level_sight

    , 1                                     	-- a_get_all_intensity_level_sound
	, 0                                     	-- a_get_inactive_intensity_level_sound
	, ''                                    	-- a_ids_intensity_level_sound
    , ''                                        -- a_names_intensity_level_sound

    , 1                                     	-- a_get_all_intensity_level_touch
	, 0                                     	-- a_get_inactive_intensity_level_touch
	, ''                                    	-- a_ids_intensity_level_touch
    , ''                                        -- a_names_intensity_level_touch

    , 1                                     	-- a_get_all_AC_link
	, 0                                     	-- a_get_inactive_AC_link
	, ''                                    	-- a_ids_AC_link
	, NULL                                     	-- a_min_distance_from_handler_metres_AC_Link
	, NULL                                     	-- a_max_distance_from_handler_metres_AC_Link
	, NULL                                     	-- a_value_is_in_sight_of_handler_AC_Link
	, NULL                                     	-- a_value_is_in_scent_range_of_handler_AC_Link
	, NULL                                     	-- a_value_is_in_hearing_range_of_handler_AC_Link
	, NULL                                     	-- a_value_is_on_lead_AC_Link
	, NULL                                     	-- a_min_trial_count_AC_Link
	, NULL                                     	-- a_max_trial_count_AC_Link

    , 1                                     	-- a_get_all_assessment
	, 0                                     	-- a_get_inactive_assessment
	, ''                                    	-- a_ids_assessment
    , ''                                        -- a_notes_assessment
	, NULL                                     	-- a_min_temperature_assessment
	, NULL                                     	-- a_max_temperature_assessment
    , 1                                     	-- a_get_all_assessment_type
	, 0                                     	-- a_get_inactive_assessment_type
	, ''                                    	-- a_ids_assessment_type
    , ''                                        -- a_names_assessment_type
    , 1                                     	-- a_get_all_weather
	, 0                                     	-- a_get_inactive_weather
	, ''                                    	-- a_ids_weather
    , ''                                        -- a_names_weather
    , 1                                     	-- a_get_all_lighting_level
	, 0                                     	-- a_get_inactive_lighting_level
	, ''                                    	-- a_ids_lighting_level
    , ''                                        -- a_names_lighting_level
    , 1                                     	-- a_get_all_location
	, 0                                     	-- a_get_inactive_location
	, ''                                    	-- a_ids_location
    , ''                                        -- a_names_location
    , 1                                     	-- a_get_all_user_handler
	, 0                                     	-- a_get_inactive_user_handler
	, ''                                    	-- a_ids_user_handler
    -- , IN a_auth0_ids_user_handler TEXT
    , ''                                        -- a_names_user_handler
    , ''                                        -- a_emails_user_handler


    , 1                                     	-- a_get_all_command_category
	, 0                                     	-- a_get_inactive_command_category
	, ''                                    	-- a_ids_command_category
    , ''                                        -- a_names_command_category
    , 1                                     	-- a_get_all_command
	, 0                                     	-- a_get_inactive_command
	, ''                                    	-- a_ids_command
    , ''                                        -- a_names_command
    , ''                                        -- a_hand_signal_default_descriptions_command
    , ''                                        -- a_notes_command

    , 1                                     	-- a_get_all_reinforcement_schedule
	, 0                                     	-- a_get_inactive_reinforcement_schedule
	, ''                                    	-- a_ids_reinforcement_schedule
    , ''                                        -- a_names_reinforcement_schedule

    , 0                                         -- a_require_all_id_search_filters_met
    , 0                                         -- a_require_any_id_search_filters_met
    , 0                                         -- a_require_all_non_id_search_filters_met
    , 0                                         -- a_require_any_non_id_search_filters_met
    , 0                                         -- a_show_errors
    , 0                                     	-- a_debug
);

SELECT *
FROM fetchmetrics.DOG_Distraction_Temp
;

CALL fetchmetrics.p_dog_clear_calc_distraction (
	'slobbery                            ' -- a_guid
    , 1 -- debug
);

-- DELETE FROM fetchmetrics.DOG_Distraction_Temp;

*/
