 
USE fetchmetrics;

DROP PROCEDURE IF EXISTS fetchmetrics.p_dog_calc_command;

DELIMITER //
CREATE PROCEDURE fetchmetrics.p_dog_calc_command (
      IN a_guid BINARY(36)
    , IN a_id_user INT
    , 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_user BIT
    , IN a_get_inactive_user BIT
    , IN a_ids_user TEXT
    , IN a_names_user TEXT
    , IN a_emails_user 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_command_category_id BIT;
    DECLARE v_has_filter_command_category_name BIT;
    DECLARE v_has_filter_command_hand_signal_default_description BIT;
    DECLARE v_has_filter_command_id BIT;
    DECLARE v_has_filter_command_name BIT;
    DECLARE v_has_filter_command_notes BIT;
    DECLARE v_id_access_level_view INT;
    DECLARE v_id_minimum INT;
    DECLARE v_id_permission_command_view INT;
    DECLARE v_id_type_error_bad_data INT;
    DECLARE v_id_type_error_no_permission INT;
    DECLARE v_is_super_user BIT;
    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_Command (
			  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_Command (
			  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_Command 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_Command;
    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_command_view := (SELECT PERMISSION.id_permission FROM fetchmetrics.DOG_Permission PERMISSION WHERE PERMISSION.code = 'COMMAND_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_command_category := IFNULL(a_get_all_command_category, 0);
    SET a_get_inactive_command_category := IFNULL(a_get_inactive_command_category, 0);
    SET a_ids_command_category := TRIM(IFNULL(a_ids_command_category, ''));
    SET a_names_command_category := TRIM(IFNULL(a_names_command_category, ''));
    SET a_get_all_command := IFNULL(a_get_all_command, 0);
    SET a_get_inactive_command := IFNULL(a_get_inactive_command, 0);
    SET a_ids_command := TRIM(IFNULL(a_ids_command, ''));
    SET a_names_command := TRIM(IFNULL(a_names_command, ''));
    SET a_hand_signal_default_descriptions_command := TRIM(IFNULL(a_hand_signal_default_descriptions_command, ''));
    SET a_notes_command := TRIM(IFNULL(a_notes_command, ''));
    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_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_user
            , a_get_inactive_user
            , a_ids_user
            , a_names_user
            , a_emails_user
            , 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_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Hand_Signal_Default_Description_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command_Category_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Calc_User_Access_Calc_Command;
    
	CREATE TEMPORARY TABLE tmp_Calc_User_Access_Calc_Command (
		  id_temp INT PRIMARY KEY AUTO_INCREMENT NOT NULL
		, id_user INT
        , id_role INT
		, id_permission_required INT NOT NULL
		, priority_access_level_required INT NOT NULL
        , is_super_user BIT
		, priority_access_level_user INT
		, has_access BIT
		, can_view BIT
		, can_edit BIT
		, can_admin BIT

        , does_meet_id_filters BIT
        , does_meet_non_id_filters BIT
	);
    
    CREATE TEMPORARY TABLE tmp_Command_Category_Calc_Command (
		  id_command_category INT NOT NULL
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );
    
    CREATE TEMPORARY TABLE tmp_Command_Calc_Command (
		  id_command INT NOT NULL
		, id_command_category INT NOT NULL
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
        , does_command_category_already_exist_in_temp_table BIT NOT NULL
    );
    
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error_Calc_Command (
          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_Command (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Id_Calc_Command;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Name_Calc_Command (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Name_Calc_Command;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Hand_Signal_Default_Description_Calc_Command (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Hand_Signal_Default_Description_Calc_Command;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Notes_Calc_Command (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Notes_Calc_Command;
    
    SET v_has_filter_command_category_id = CASE WHEN a_ids_command_category <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_command_category_name = CASE WHEN a_names_command_category <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_command_id = CASE WHEN a_ids_command <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_command_name = CASE WHEN a_names_command <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_command_hand_signal_default_description = CASE WHEN a_hand_signal_default_descriptions_command <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_command_notes = CASE WHEN a_notes_command <> '' THEN 1 ELSE 0 END;


    -- 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_command_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_command_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)
        , IFNULL(CALC_USER_T.is_super_user, 0)
    INTO
          v_can_view
        , v_is_super_user
    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
            , v_is_super_user
        ;
    END IF;
    
    IF (v_can_view = 0) THEN
		DELETE t_ME
        FROM tmp_Msg_Error_Calc_Command t_ME
        WHERE t_ME.id_type <> v_id_type_error_no_permission
        ;
		INSERT INTO tmp_Msg_Error_Calc_Command (
			  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 Commands.'
		)
        ;
    END IF;
	
	CALL fetchmetrics.p_dog_clear_calc_user_access(
		a_guid
        , 0 -- a_debug
	);


    -- Users
    IF a_debug = 1 THEN
		SELECT 
			  a_guid -- guid
            , 0 -- get_all_user
            , 0 -- get_inactive_user
            , a_ids_user -- ids_user
            , '' -- a_auth0_ids_user
            , a_names_user -- a_names_user
            , a_emails_user -- a_emails_user
            , 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
            , v_id_permission_command_view -- ids_permission
            , v_id_access_level_view -- ids_access_level
            , 0 -- a_show_errors
			, a_debug -- a_debug
        ;
    END IF;
    
	CALL fetchmetrics.p_dog_calc_user_access(
          a_guid -- a_guid
        , 0 -- get_all_user
        , 0 -- get_inactive_user
        , a_ids_user -- ids_user
        , '' -- a_auth0_ids_user
        , a_names_user -- a_names_user
        , a_emails_user -- a_emails_user
        , 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
        , v_id_permission_command_view -- ids_permission
        , v_id_access_level_view -- ids_access_level
        , 0 -- a_show_errors
        , a_debug -- a_debug
    );
	
    INSERT INTO tmp_Calc_User_Access_Calc_Command (
        id_user
        , id_role
		, id_permission_required
		, priority_access_level_required
        , is_super_user
		, priority_access_level_user
		, has_access
		, can_view
		, can_edit
		, can_admin
        , does_meet_id_filters
        , does_meet_non_id_filters
    )
    SELECT
        CALC_USER_T.id_user
        , CALC_USER_T.id_role
		, CALC_USER_T.id_permission_required
		, CALC_USER_T.priority_access_level_required
        , CALC_USER_T.is_super_user
		, CALC_USER_T.priority_access_level_user
		, CALC_USER_T.has_access
		, CALC_USER_T.can_view
		, CALC_USER_T.can_edit
		, CALC_USER_T.can_admin
        , CALC_USER_T.does_meet_id_filters
        , CALC_USER_T.does_meet_non_id_filters
    FROM fetchmetrics.DOG_Calc_User_Access_Temp CALC_USER_T
    WHERE CALC_USER_T.GUID = v_guid
    LIMIT 1
    ;

    IF a_debug = 1 THEN
        SELECT * FROM tmp_Calc_User_Access_Calc_Command;
    END IF;
	
	CALL fetchmetrics.p_dog_clear_calc_user_access (
		a_guid
        , 0 -- a_debug
	);


    -- Command Categories
    IF v_has_filter_command_category_id = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_ids_command_category, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Id_Calc_Command (
			  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_command_category_name = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_names_command_category, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Name_Calc_Command (
			  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_Command 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_Command t_SPLIT_ID
            LEFT JOIN fetchmetrics.DOG_Command_Category COMMAND_CATEGORY ON t_SPLIT_ID.as_int = COMMAND_CATEGORY.id_command_category 
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(COMMAND_CATEGORY.id_command_category)
				OR  (
					COMMAND_CATEGORY.active = 0
					AND a_get_inactive_command_category = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error_Calc_Command (
				  id_type
		        , code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Command IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_ID.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Id_Calc_Command t_SPLIT_ID
			LEFT JOIN fetchmetrics.DOG_Command_Category COMMAND_CATEGORY ON t_SPLIT_ID.as_int = COMMAND_CATEGORY.id_command_category
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(COMMAND_CATEGORY.id_command_category)
				OR  (
					COMMAND_CATEGORY.active = 0
					AND a_get_inactive_command_category = 0
				)
			;
        /* Don't error on names not found
        */
		ELSE
			INSERT INTO tmp_Command_Category_Calc_Command (
				  id_command_category
                , does_meet_id_filters
                , does_meet_non_id_filters
			)
            WITH 
                Command_Category_Id_Filter AS (
                    SELECT COMMAND_CATEGORY.id_command_category
                    FROM tmp_Split_Id_Calc_Command t_SPLIT_ID
                    RIGHT JOIN fetchmetrics.DOG_Command_Category COMMAND_CATEGORY ON t_SPLIT_ID.as_int = COMMAND_CATEGORY.id_command_category
                    WHERE t_SPLIT_ID.as_int IS NOT NULL
                )
                , Command_Category_Name_Filter AS (
                    SELECT COMMAND_CATEGORY.id_command_category
                    FROM tmp_Split_Name_Calc_Command t_SPLIT_NAME
                    RIGHT JOIN fetchmetrics.DOG_Command_Category COMMAND_CATEGORY ON COMMAND_CATEGORY.name LIKE CONCAT('%', t_SPLIT_NAME.substring, '%')
                    WHERE 
                            t_SPLIT_NAME.substring IS NOT NULL
                        AND t_SPLIT_NAME.substring <> ''
                )
                , Command_Category_Filters AS (
                    SELECT 
                          COMMAND_CATEGORY_COMBINED.id_command_category
                        , MAX(IFNULL(COMMAND_CATEGORY_COMBINED.does_meet_id_filter, 0)) AS does_meet_id_filter
                        , MAX(IFNULL(COMMAND_CATEGORY_COMBINED.does_meet_name_filter, 0)) AS does_meet_name_filter
                    FROM (
                        SELECT 
                              COMMAND_CATEGORY_ID_FILTER.id_command_category 
                            , 1 AS does_meet_id_filter
                            , 0 AS does_meet_name_filter
                        FROM Command_Category_Id_Filter COMMAND_CATEGORY_ID_FILTER
                        UNION
                        SELECT 
                              COMMAND_CATEGORY_NAME_FILTER.id_command_category 
                            , 0 AS does_meet_id_filter
                            , 1 AS does_meet_name_filter
                        FROM Command_Category_Name_Filter COMMAND_CATEGORY_NAME_FILTER
                    ) COMMAND_CATEGORY_COMBINED
                    GROUP BY COMMAND_CATEGORY_COMBINED.id_command_category
                )
			SELECT 
				  COMMAND_CATEGORY.id_command_category
                , CASE WHEN 
                        v_has_filter_command_category_id = 0 
                    OR  COMMAND_CATEGORY_FILTERS.does_meet_id_filter = 1
                THEN 1 ELSE 0 END AS does_meet_id_filters
                , CASE WHEN 
                        (
                            v_has_filter_command_category_name = 0
                    )
                    OR COMMAND_CATEGORY_FILTERS.does_meet_name_filter = 1
                THEN 1 ELSE 0 END AS does_meet_non_id_filters
			FROM fetchmetrics.DOG_Command_Category COMMAND_CATEGORY
            LEFT JOIN Command_Category_Filters COMMAND_CATEGORY_FILTERS ON COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY_FILTERS.id_command_category
            LEFT JOIN demo.DOG_Command_Category_User_Link COMMAND_CATEGORY_USER_LINK ON COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY_USER_LINK.id_command_category
            LEFT JOIN tmp_Calc_User_Access_Calc_Command t_USER ON COMMAND_CATEGORY_USER_LINK.id_user = t_USER.id_user
			WHERE 
                (
					    a_get_all_command_category = 1
                    OR  (
                            v_has_filter_command_category_id = 1
                        AND COMMAND_CATEGORY_FILTERS.does_meet_id_filter = 1
					)
					OR  (
                            v_has_filter_command_category_name = 1
                        AND COMMAND_CATEGORY_FILTERS.does_meet_name_filter = 1
					)
				)
                AND (
                        v_is_super_user = 1
                    OR  t_USER.id_user IS NOT NULL
                )
				AND (
					    a_get_inactive_command_category = 1
					OR  COMMAND_CATEGORY.active = 1
				)
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split_Id_Calc_Command;
    DELETE FROM tmp_Split_Name_Calc_Command;

    -- Commands
    IF v_has_filter_command_id = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_ids_command, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Id_Calc_Command (
			  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_command_name = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_names_command, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Name_Calc_Command (
			  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_command_hand_signal_default_description = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_hand_signal_default_descriptions_command, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Hand_Signal_Default_Description_Calc_Command (
			  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_command_notes = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_notes_command, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Notes_Calc_Command (
			  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_Command 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_Command t_SPLIT_ID
            LEFT JOIN fetchmetrics.DOG_Command COMMAND ON t_SPLIT_ID.as_int = COMMAND.id_command 
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(COMMAND.id_command)
				OR  (
					COMMAND.active = 0
					AND a_get_inactive_command = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error_Calc_Command (
				  id_type
		        , code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Command IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_ID.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Id_Calc_Command t_SPLIT_ID
			LEFT JOIN fetchmetrics.DOG_Command COMMAND ON t_SPLIT_ID.as_int = COMMAND.id_command
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(COMMAND.id_command)
				OR  (
					COMMAND.active = 0
					AND a_get_inactive_command = 0
				)
			;
        /* Don't error on names, hand signals, or notes not found
		ELSEIF EXISTS ()
        */
		ELSE
            IF a_debug = 1 THEN
                SELECT 'Command Filters';
                WITH 
                    Command_Id_Filter AS (
                        SELECT COMMAND.id_command
                        FROM tmp_Split_Id_Calc_Command t_SPLIT_ID
                        INNER JOIN fetchmetrics.DOG_Command COMMAND ON t_SPLIT_ID.as_int = COMMAND.id_command
                    )
                    , Command_Name_Filter AS (
                        SELECT COMMAND.id_command
                        FROM tmp_Split_Name_Calc_Command t_SPLIT_NAME
                        INNER JOIN fetchmetrics.DOG_Command COMMAND ON COMMAND.name LIKE CONCAT('%', t_SPLIT_NAME.substring, '%')
                        WHERE 
                                t_SPLIT_NAME.substring IS NOT NULL
                            AND t_SPLIT_NAME.substring <> ''
                    )
                    , Command_Hand_Signal_Default_Description_Filter AS (
                        SELECT COMMAND.id_command
                        FROM tmp_Split_Hand_Signal_Default_Description_Calc_Command t_SPLIT_HSDD
                        INNER JOIN fetchmetrics.DOG_Command COMMAND ON COMMAND.hand_signal_default_description LIKE CONCAT('%', t_SPLIT_HSDD.substring, '%')
                        WHERE 
                                t_SPLIT_HSDD.substring IS NOT NULL
                            AND t_SPLIT_HSDD.substring <> ''
                    )
                    , Command_Notes_Filter AS (
                        SELECT COMMAND.id_command
                        FROM tmp_Split_Notes_Calc_Command t_SPLIT_NOTES
                        INNER JOIN fetchmetrics.DOG_Command COMMAND ON COMMAND.notes LIKE CONCAT('%', t_SPLIT_NOTES.substring, '%')
                        WHERE 
                                t_SPLIT_NOTES.substring IS NOT NULL
                            AND t_SPLIT_NOTES.substring <> ''
                    )
                    , Command_Filters AS (
                        SELECT 
                            COMMAND_COMBINED.id_command
                            , MAX(COMMAND_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                            , MAX(COMMAND_COMBINED.does_meet_name_filter) AS does_meet_name_filter
                            , MAX(COMMAND_COMBINED.does_meet_hand_signal_default_description_filter) AS does_meet_hand_signal_default_description_filter
                            , MAX(COMMAND_COMBINED.does_meet_notes_filter) AS does_meet_notes_filter
                        FROM (
                            SELECT 
                                COMMAND_ID_FILTER.id_command 
                                , 1 AS does_meet_id_filter
                                , 0 AS does_meet_name_filter
                                , 0 AS does_meet_hand_signal_default_description_filter
                                , 0 AS does_meet_notes_filter
                            FROM Command_Id_Filter COMMAND_ID_FILTER
                            UNION
                            SELECT 
                                COMMAND_NAME_FILTER.id_command 
                                , 0 AS does_meet_id_filter
                                , 1 AS does_meet_name_filter
                                , 0 AS does_meet_hand_signal_default_description_filter
                                , 0 AS does_meet_notes_filter
                            FROM Command_Name_Filter COMMAND_NAME_FILTER
                            UNION
                            SELECT 
                                COMMAND_HSDD_FILTER.id_command 
                                , 0 AS does_meet_id_filter
                                , 0 AS does_meet_name_filter
                                , 1 AS does_meet_hand_signal_default_description_filter
                                , 0 AS does_meet_notes_filter
                            FROM Command_Hand_Signal_Default_Description_Filter COMMAND_HSDD_FILTER
                            UNION
                            SELECT 
                                COMMAND_NOTES_FILTER.id_command 
                                , 0 AS does_meet_id_filter
                                , 0 AS does_meet_name_filter
                                , 0 AS does_meet_hand_signal_default_description_filter
                                , 1 AS does_meet_notes_filter
                            FROM Command_Notes_Filter COMMAND_NOTES_FILTER
                        ) COMMAND_COMBINED
                        GROUP BY COMMAND_COMBINED.id_command
                    )
                SELECT 
                      COMMAND.id_command
                    , COMMAND.id_command_category
                    , CASE WHEN 
                            v_has_filter_command_id = 0 
                        OR  COMMAND_FILTERS.does_meet_id_filter = 1
                    THEN 1 ELSE 0 END AS does_meet_id_filters
                    , CASE WHEN 
                            (
                                v_has_filter_command_name = 0
                            AND v_has_filter_command_hand_signal_default_description = 0
                            AND v_has_filter_command_notes = 0
                        )
                        OR  COMMAND_FILTERS.does_meet_name_filter = 1
                        OR  COMMAND_FILTERS.does_meet_hand_signal_default_description_filter = 1
                        OR  COMMAND_FILTERS.does_meet_notes_filter = 1
                    THEN 1 ELSE 0 END AS does_meet_non_id_filters
                    , CASE WHEN t_COMMAND_CATEGORY.id_command_category IS NOT NULL THEN 1 ELSE 0 END AS does_command_category_already_exist_in_temp_table
                FROM fetchmetrics.DOG_Command COMMAND
                INNER JOIN tmp_Command_Category_Calc_Command t_COMMAND_CATEGORY ON COMMAND.id_command_category = t_COMMAND_CATEGORY.id_command_category
                LEFT JOIN Command_Filters COMMAND_FILTERS ON COMMAND.id_command = COMMAND_FILTERS.id_command
                LEFT JOIN demo.DOG_Command_Category_User_Link COMMAND_CATEGORY_USER_LINK ON t_COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY_USER_LINK.id_command_category
                LEFT JOIN tmp_Calc_User_Access_Calc_Command t_USER ON COMMAND_CATEGORY_USER_LINK.id_user = t_USER.id_user
                WHERE 
                    (
                            a_get_all_command = 1
                        OR  (
                                v_has_filter_command_id = 1
                            AND COMMAND_FILTERS.does_meet_id_filter = 1
                        )
                        OR  (
                                v_has_filter_command_name = 1
                            AND COMMAND_FILTERS.does_meet_name_filter = 1
                        )
                        OR  (
                                v_has_filter_command_hand_signal_default_description = 1
                            AND COMMAND_FILTERS.does_meet_hand_signal_default_description_filter = 1
                        )
                        OR  (
                                v_has_filter_command_notes = 1
                            AND COMMAND_FILTERS.does_meet_notes_filter = 1
                        )
                    )
                AND (
                        v_is_super_user = 1
                    OR  t_USER.id_user IS NOT NULL
                )
                    AND (
                            a_get_inactive_command = 1
                        OR  COMMAND.active = 1
                    )
                ;
            END IF;
			INSERT INTO tmp_Command_Calc_Command (
				  id_command
                , id_command_category
                , does_meet_id_filters
                , does_meet_non_id_filters
                , does_command_category_already_exist_in_temp_table
			)
            WITH 
                  Command_Id_Filter AS (
                    SELECT COMMAND.id_command
                    FROM tmp_Split_Id_Calc_Command t_SPLIT_ID
                    INNER JOIN fetchmetrics.DOG_Command COMMAND ON t_SPLIT_ID.as_int = COMMAND.id_command
                )
                , Command_Name_Filter AS (
                    SELECT COMMAND.id_command
                    FROM tmp_Split_Name_Calc_Command t_SPLIT_NAME
                    INNER JOIN fetchmetrics.DOG_Command COMMAND ON COMMAND.name LIKE CONCAT('%', t_SPLIT_NAME.substring, '%')
                    WHERE 
                            t_SPLIT_NAME.substring IS NOT NULL
                        AND t_SPLIT_NAME.substring <> ''
                )
                , Command_Hand_Signal_Default_Description_Filter AS (
                    SELECT COMMAND.id_command
                    FROM tmp_Split_Hand_Signal_Default_Description_Calc_Command t_SPLIT_HSDD
                    INNER JOIN demo.DOG_Command COMMAND ON COMMAND.hand_signal_default_description LIKE CONCAT('%', t_SPLIT_HSDD.substring, '%')
                    WHERE 
                            t_SPLIT_HSDD.substring IS NOT NULL
                        AND t_SPLIT_HSDD.substring <> ''
                )
                , Command_Notes_Filter AS (
                    SELECT COMMAND.id_command
                    FROM tmp_Split_Notes_Calc_Command t_SPLIT_NOTES
                    INNER JOIN demo.DOG_Command COMMAND ON COMMAND.notes LIKE CONCAT('%', t_SPLIT_NOTES.substring, '%')
                    WHERE 
                            t_SPLIT_NOTES.substring IS NOT NULL
                        AND t_SPLIT_NOTES.substring <> ''
                )
                , Command_Filters AS (
                    SELECT 
                          COMMAND_COMBINED.id_command
                        , MAX(COMMAND_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                        , MAX(COMMAND_COMBINED.does_meet_name_filter) AS does_meet_name_filter
                        , MAX(COMMAND_COMBINED.does_meet_hand_signal_default_description_filter) AS does_meet_hand_signal_default_description_filter
                        , MAX(COMMAND_COMBINED.does_meet_notes_filter) AS does_meet_notes_filter
                    FROM (
                        SELECT 
                              COMMAND_ID_FILTER.id_command 
                            , 1 AS does_meet_id_filter
                            , 0 AS does_meet_name_filter
                            , 0 AS does_meet_hand_signal_default_description_filter
                            , 0 AS does_meet_notes_filter
                        FROM Command_Id_Filter COMMAND_ID_FILTER
                        UNION
                        SELECT 
                              COMMAND_NAME_FILTER.id_command 
                            , 0 AS does_meet_id_filter
                            , 1 AS does_meet_name_filter
                            , 0 AS does_meet_hand_signal_default_description_filter
                            , 0 AS does_meet_notes_filter
                        FROM Command_Name_Filter COMMAND_NAME_FILTER
                        UNION
                        SELECT 
                              COMMAND_HSDD_FILTER.id_command 
                            , 0 AS does_meet_id_filter
                            , 0 AS does_meet_name_filter
                            , 1 AS does_meet_hand_signal_default_description_filter
                            , 0 AS does_meet_notes_filter
                        FROM Command_Hand_Signal_Default_Description_Filter COMMAND_HSDD_FILTER
                        UNION
                        SELECT 
                              COMMAND_NOTES_FILTER.id_command 
                            , 0 AS does_meet_id_filter
                            , 0 AS does_meet_name_filter
                            , 0 AS does_meet_hand_signal_default_description_filter
                            , 1 AS does_meet_notes_filter
                        FROM Command_Notes_Filter COMMAND_NOTES_FILTER
                    ) COMMAND_COMBINED
                    GROUP BY COMMAND_COMBINED.id_command
                )
            SELECT 
                  COMMAND.id_command
                , COMMAND.id_command_category
                , CASE WHEN 
                        v_has_filter_command_id = 0 
                    OR  IFNULL(COMMAND_FILTERS.does_meet_id_filter, 0) = 1
                THEN 1 ELSE 0 END AS does_meet_id_filters
                , CASE WHEN 
                        (
                            v_has_filter_command_name = 0
                        AND v_has_filter_command_hand_signal_default_description = 0
                        AND v_has_filter_command_notes = 0
                    )
                    OR  IFNULL(COMMAND_FILTERS.does_meet_name_filter, 0) = 1
                    OR  IFNULL(COMMAND_FILTERS.does_meet_hand_signal_default_description_filter, 0) = 1
                    OR  IFNULL(COMMAND_FILTERS.does_meet_notes_filter, 0) = 1
                THEN 1 ELSE 0 END AS does_meet_non_id_filters
                , CASE WHEN t_COMMAND_CATEGORY.id_command_category IS NOT NULL THEN 1 ELSE 0 END AS does_command_category_already_exist_in_temp_table
            FROM fetchmetrics.DOG_Command COMMAND
            INNER JOIN tmp_Command_Category_Calc_Command t_COMMAND_CATEGORY ON COMMAND.id_command_category = t_COMMAND_CATEGORY.id_command_category
            LEFT JOIN Command_Filters COMMAND_FILTERS ON COMMAND.id_command = COMMAND_FILTERS.id_command
            LEFT JOIN demo.DOG_Command_Category_User_Link COMMAND_CATEGORY_USER_LINK ON t_COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY_USER_LINK.id_command_category
            LEFT JOIN tmp_Calc_User_Access_Calc_Command t_USER ON COMMAND_CATEGORY_USER_LINK.id_user = t_USER.id_user
            WHERE 
                (
                        a_get_all_command = 1
                    OR  (
                            v_has_filter_command_id = 1
                        AND COMMAND_FILTERS.does_meet_id_filter = 1
                    )
                    OR  (
                            v_has_filter_command_name = 1
                        AND COMMAND_FILTERS.does_meet_name_filter = 1
                    )
                    OR  (
                            v_has_filter_command_hand_signal_default_description = 1
                        AND COMMAND_FILTERS.does_meet_hand_signal_default_description_filter = 1
                    )
                    OR  (
                            v_has_filter_command_notes = 1
                        AND COMMAND_FILTERS.does_meet_notes_filter = 1
                    )
                )
                AND (
                        v_is_super_user = 1
                    OR  t_USER.id_user IS NOT NULL
                )
                AND (
                        a_get_inactive_command = 1
                    OR  COMMAND.active = 1
                )
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split_Id_Calc_Command;
    DELETE FROM tmp_Split_Name_Calc_Command;
    DELETE FROM tmp_Split_Hand_Signal_Default_Description_Calc_Command;
    DELETE FROM tmp_Split_Notes_Calc_Command;

    IF a_debug = 1 THEN
        SELECT 'After get Commands and Categories';
        SELECT * FROM tmp_Command_Category_Calc_Command;
        SELECT * FROM tmp_Command_Calc_Command;
    END IF;
    
    -- Missing Categories
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Command 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_require_all_id_search_filters_met = 0 
            AND a_require_all_non_id_search_filters_met = 0
        THEN
            INSERT INTO tmp_Command_Category_Calc_Command (
                  id_command_category
                , does_meet_id_filters
                , does_meet_non_id_filters
            )
            SELECT DISTINCT
                  COMMAND_CATEGORY.id_command_category
                , 0 AS does_meet_id_filters
                , 0 AS does_meet_non_id_filters
            FROM fetchmetrics.DOG_Command_Category COMMAND_CATEGORY
            INNER JOIN tmp_Command_Calc_Command t_COMMAND 
                ON  COMMAND_CATEGORY.id_command_category = t_COMMAND.id_command_category
                AND t_COMMAND.does_command_category_already_exist_in_temp_table = 0
            ;
        END IF;
    END IF;

    -- Filter records
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Command 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_COMMAND
        FROM tmp_Command_Calc_Command t_COMMAND
        LEFT JOIN tmp_Command_Category_Calc_Command t_COMMAND_CATEGORY ON t_COMMAND.id_command_category = t_COMMAND_CATEGORY.id_command_category
        LEFT JOIN demo.DOG_Command_Category_User_Link COMMAND_CATEGORY_USER_LINK ON t_COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY_USER_LINK.id_command_category
        LEFT JOIN tmp_Calc_User_Access_Calc_Command t_USER ON DOG_USER_LINK.id_user = t_USER.id_user
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND (
                        t_COMMAND.does_meet_id_filters = 0
                    OR  t_COMMAND_CATEGORY.does_meet_id_filters = 0
                    OR  IFNULL(t_USER.does_meet_id_filters, 0) = 0
                )
            )
            OR  (
                    a_require_all_non_id_search_filters_met = 1
                AND (
                        t_COMMAND.does_meet_non_id_filters = 0
                    OR  t_COMMAND_CATEGORY.does_meet_non_id_filters = 0
                    OR  IFNULL(t_USER.does_meet_non_id_filters, 0) = 0
                )
            )
            OR  (
                a_require_any_id_search_filters_met = 1
                AND t_COMMAND.does_meet_id_filters = 0
                AND t_COMMAND_CATEGORY.does_meet_id_filters = 0
                AND IFNULL(t_USER.does_meet_id_filters, 0) = 0
            )
            OR  (
                a_require_any_non_id_search_filters_met = 1
                AND t_COMMAND.does_meet_non_id_filters = 0
                AND t_COMMAND_CATEGORY.does_meet_non_id_filters = 0
                AND IFNULL(t_USER.does_meet_non_id_filters, 0) = 0
            )
        ;

        IF a_debug = 1 THEN
            WITH 
                Category_And_Best_Command AS (
                    SELECT 
                        t_COMMAND.id_command_category
                        , MAX(IFNULL(t_COMMAND.does_meet_id_filters, 0)) AS does_meet_id_filters
                        , MAX(IFNULL(t_COMMAND.does_meet_non_id_filters, 0)) AS does_meet_non_id_filters
                    FROM tmp_Command_Calc_Command t_COMMAND 
                    GROUP BY t_COMMAND.id_command_category
                )
            SELECT *
            FROM tmp_Command_Category_Calc_Command t_COMMAND_CATEGORY
            LEFT JOIN Category_And_Best_Command CATEGORY_BEST ON t_COMMAND_CATEGORY.id_command_category = CATEGORY_BEST.id_command_category
            LEFT JOIN demo.DOG_Command_Category_User_Link COMMAND_CATEGORY_USER_LINK ON t_COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY_USER_LINK.id_command_category
            LEFT JOIN tmp_Calc_User_Access_Calc_Command t_USER ON DOG_USER_LINK.id_user = t_USER.id_user
            WHERE
                    (
                        a_require_all_id_search_filters_met = 1
                    AND (
                            IFNULL(CATEGORY_BEST.does_meet_id_filters, 0) = 0
                        OR  t_COMMAND_CATEGORY.does_meet_id_filters = 0
                        OR  IFNULL(t_USER.does_meet_id_filters, 0) = 0
                    )
                )
                OR  (
                        a_require_all_non_id_search_filters_met = 1
                    AND (
                            IFNULL(CATEGORY_BEST.does_meet_non_id_filters, 0) = 0
                        OR  t_COMMAND_CATEGORY.does_meet_non_id_filters = 0
                        OR  IFNULL(t_USER.does_meet_non_id_filters, 0) = 0
                    )
                )
                OR  (
                    a_require_any_id_search_filters_met = 1
                    AND IFNULL(CATEGORY_BEST.does_meet_id_filters, 0) = 0
                    AND t_COMMAND_CATEGORY.does_meet_id_filters = 0
                    AND IFNULL(t_USER.does_meet_id_filters, 0) = 0
                )
                OR  (
                    a_require_any_non_id_search_filters_met = 1
                    AND IFNULL(CATEGORY_BEST.does_meet_non_id_filters, 0) = 0
                    AND t_COMMAND_CATEGORY.does_meet_non_id_filters = 0
                    AND IFNULL(t_USER.does_meet_non_id_filters, 0) = 0
                )
            ;
        END IF;

        WITH 
            Category_And_Best_Command AS (
                SELECT 
                      t_COMMAND.id_command_category
                    , MAX(IFNULL(t_COMMAND.does_meet_id_filters, 0)) AS does_meet_id_filters
                    , MAX(IFNULL(t_COMMAND.does_meet_non_id_filters, 0)) AS does_meet_non_id_filters
                FROM tmp_Command_Calc_Command t_COMMAND
                GROUP BY t_COMMAND.id_command_category
            )
        DELETE t_COMMAND_CATEGORY
        FROM tmp_Command_Category_Calc_Command t_COMMAND_CATEGORY
        LEFT JOIN Category_And_Best_Command CATEGORY_BEST ON t_COMMAND_CATEGORY.id_command_category = CATEGORY_BEST.id_command_category
            LEFT JOIN demo.DOG_Command_Category_User_Link COMMAND_CATEGORY_USER_LINK ON t_COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY_USER_LINK.id_command_category
            LEFT JOIN tmp_Calc_User_Access_Calc_Command t_USER ON DOG_USER_LINK.id_user = t_USER.id_user
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND (
                        IFNULL(CATEGORY_BEST.does_meet_id_filters, 0) = 0
                    OR  t_COMMAND_CATEGORY.does_meet_id_filters = 0
                    OR  IFNULL(t_USER.does_meet_id_filters, 0) = 0
                )
            )
            OR  (
                    a_require_all_non_id_search_filters_met = 1
                AND (
                        IFNULL(CATEGORY_BEST.does_meet_non_id_filters, 0) = 0
                    OR  t_COMMAND_CATEGORY.does_meet_non_id_filters = 0
                    OR  IFNULL(t_USER.does_meet_non_id_filters, 0) = 0
                )
            )
            OR  (
                a_require_any_id_search_filters_met = 1
                AND IFNULL(CATEGORY_BEST.does_meet_id_filters, 0) = 0
                AND t_COMMAND_CATEGORY.does_meet_id_filters = 0
                AND IFNULL(t_USER.does_meet_id_filters, 0) = 0
            )
            OR  (
                a_require_any_non_id_search_filters_met = 1
                AND IFNULL(CATEGORY_BEST.does_meet_non_id_filters, 0) = 0
                AND t_COMMAND_CATEGORY.does_meet_non_id_filters = 0
                AND IFNULL(t_USER.does_meet_non_id_filters, 0) = 0
            )
        ;
    END IF;

    IF a_debug = 1 THEN
        SELECT 'After filter Commands and Categories';
        -- SELECT * FROM tmp_Command_Category_Calc_Command;
        -- SELECT * FROM tmp_Command_Calc_Command;
    END IF;

    IF a_debug = 1 THEN
        SELECT 'Before non-permitted data deletion';
        SELECT * FROM tmp_Command_Category_Calc_Command;
        SELECT * FROM tmp_Command_Calc_Command;
        SELECT * FROM tmp_Msg_Error_Calc_Command;
    END IF;
    
    IF EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Command 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_Command_Category_Calc_Command;
            SELECT * FROM tmp_Command_Calc_Command;
        END IF;

        DELETE FROM tmp_Command_Calc_Command;
        DELETE FROM tmp_Command_Category_Calc_Command;
    END IF;

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

    -- Outputs
	START TRANSACTION;
        -- Command Categories
        INSERT INTO fetchmetrics.DOG_Command_Category_Temp (
            guid
            , id_command_category
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT 
            a_guid
            , t_COMMAND_CATEGORY.id_command_category
            , COMMAND_CATEGORY.code
            , COMMAND_CATEGORY.name
            , COMMAND_CATEGORY.active

            , t_COMMAND_CATEGORY.does_meet_id_filters
            , t_COMMAND_CATEGORY.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Command_Category COMMAND_CATEGORY
        INNER JOIN tmp_Command_Category_Calc_Command t_COMMAND_CATEGORY ON COMMAND_CATEGORY.id_command_category = t_COMMAND_CATEGORY.id_command_category
        ;

        -- Commands
        INSERT INTO fetchmetrics.DOG_Command_Temp (
            guid
            , id_command
            , id_command_category
            , name
            , hand_signal_default_description
            , can_have_button
            , notes
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT 
            a_guid
            , t_COMMAND.id_command
            , t_COMMAND.id_command_category
            , COMMAND.name
            , COMMAND.hand_signal_default_description
            , COMMAND.can_have_button
            , COMMAND.notes
            , COMMAND.active

            , t_COMMAND.does_meet_id_filters
            , t_COMMAND.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Command COMMAND
        INNER JOIN tmp_Command_Calc_Command t_COMMAND ON COMMAND.id_command = t_COMMAND.id_command
        ;
    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_Command 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_Command_Category_Calc_Command;
        SELECT * FROM tmp_Command_Calc_Command;
    END IF;
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Notes_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Hand_Signal_Default_Description_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command_Category_Calc_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Calc_User_Access_Calc_Command;
    
    IF a_debug = 1 THEN
		CALL fetchmetrics.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*


CALL fetchmetrics.p_dog_calc_command (
      'slips                               ' -- a_guid
	, 1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 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
    , 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
);

CALL fetchmetrics.p_dog_calc_command (
      'slips                               ' -- a_guid
	, 1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_command_category
	, 0                                     	-- a_get_inactive_command_category
	, ''                                    	-- a_ids_command_category
    , 'pat,point'                                        -- a_names_command_category
    , 1                                     	-- a_get_all_command
	, 0                                     	-- a_get_inactive_command
	, ''                                    	-- a_ids_command
    , 'pat,point'                                        -- a_names_command
    , 'pat,point'                                        -- a_hand_signal_default_descriptions_command
    , 'pat,point'                                        -- a_notes_command
    , 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
    , 1                                         -- a_require_any_non_id_search_filters_met
    , 1                                         -- a_show_errors
    , 0                                     	-- a_debug
);
SELECT *
FROM fetchmetrics.DOG_Command_Category_Temp
;
SELECT *
FROM fetchmetrics.DOG_Command_Temp
;
CALL fetchmetrics.p_dog_clear_calc_command (
	'slips                               ' -- a_guid
    , 1 -- debug
);

DELETE
FROM fetchmetrics.DOG_Command_Category_Temp
;

DELETE
FROM fetchmetrics.DOG_Command_Temp
;

*/
