 
USE fetchmetrics;

DROP PROCEDURE IF EXISTS fetchmetrics.p_dog_calc_obedience_level;

DELIMITER //
CREATE PROCEDURE fetchmetrics.p_dog_calc_obedience_level (
      IN a_guid BINARY(36)
    , IN a_id_user INT
    , IN a_get_all_obedience_level BIT
	, IN a_get_inactive_obedience_level BIT
	, IN a_ids_obedience_level TEXT
    , IN a_names_obedience_level 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_obedience_level_id BIT;
    DECLARE v_has_filter_obedience_level_name BIT;
    DECLARE v_id_access_level_view INT;
    DECLARE v_id_minimum INT;
    DECLARE v_id_permission_command_view 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_ids_permission_required VARCHAR(200);
    DECLARE v_is_super_user BIT;
    DECLARE v_priority_access_level_none INT;
    DECLARE v_priority_access_level_view 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_Obedience_Level (
			  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_Obedience_Level (
			  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_Obedience_Level 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_Obedience_Level;
    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_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);
    SET v_ids_permission_required := (SELECT CONCAT(
        CONVERT(v_id_permission_dog_view, CHAR)
        , ','
        , CONVERT(v_id_permission_command_view, CHAR)
    ));
    SET v_priority_access_level_none := (SELECT ACCESS_LEVEL.priority FROM fetchmetrics.DOG_Access_Level ACCESS_LEVEL WHERE ACCESS_LEVEL.code = 'NONE' LIMIT 1);
    SET v_priority_access_level_view := (SELECT ACCESS_LEVEL.priority FROM fetchmetrics.DOG_Access_Level ACCESS_LEVEL WHERE ACCESS_LEVEL.id_access_level = v_id_access_level_view);

    
    CALL fetchmetrics.p_core_validate_guid ( a_guid );

    SET a_id_user := IFNULL(a_id_user, 0);
    SET a_get_all_obedience_level := IFNULL(a_get_all_obedience_level, 0);
    SET a_get_inactive_obedience_level := IFNULL(a_get_inactive_obedience_level, 0);
    SET a_ids_obedience_level := TRIM(IFNULL(a_ids_obedience_level, ''));
    SET a_names_obedience_level := TRIM(IFNULL(a_names_obedience_level, ''));
    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_obedience_level
            , a_get_inactive_obedience_level
            , a_ids_obedience_level
            , a_names_obedience_level
            , 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_command_view
            , v_id_permission_dog_view
            , v_ids_permission_required
            , v_time_start
        ;
    END IF;

    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name_Calc_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Obedience_Level_Calc_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Calc_User_Access_Calc_Obedience_Level;
    
	CREATE TEMPORARY TABLE tmp_Calc_User_Access_Calc_Obedience_Level (
		  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
        , active BIT

        , does_meet_id_filters BIT
        , does_meet_non_id_filters BIT
	);
    
    CREATE TEMPORARY TABLE tmp_Obedience_Level_Calc_Obedience_Level (
		  id_obedience_level INT NOT NULL
        , exists_valid_link BIT NOT NULL
        , id_user INT
        , 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_Obedience_Level (
          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_Obedience_Level (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Id_Calc_Obedience_Level;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Name_Calc_Obedience_Level (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Name_Calc_Obedience_Level;
    
    SET v_has_filter_obedience_level_id = CASE WHEN a_ids_obedience_level <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_obedience_level_name = CASE WHEN a_names_obedience_level <> '' 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_ids_permission_required -- 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_ids_permission_required -- 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 demo.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_Obedience_Level t_ME
        WHERE t_ME.id_type <> v_id_type_error_no_permission
        ;
		INSERT INTO tmp_Msg_Error_Calc_Obedience_Level (
			  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 Obedience_Levels.'
		)
        ;
    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
            , a_get_all_user -- get_all_user
            , a_get_inactive_user -- 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_ids_permission_required -- ids_permission
            , v_id_access_level_view -- ids_access_level
            , 0 -- a_show_errors
			, 0 -- a_debug
        ;
    END IF;
    
	CALL demo.p_dog_calc_user_access(
          a_guid-- guid
        , a_get_all_user -- get_all_user
        , a_get_inactive_user -- 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_ids_permission_required -- ids_permission
        , v_id_access_level_view -- ids_access_level
        , 0 -- a_show_errors
        , 0 -- a_debug
    );
    
    INSERT INTO tmp_Calc_User_Access_Calc_Obedience_Level (
        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
        , active
        , 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.active
        , CALC_USER_T.does_meet_id_filters
        , CALC_USER_T.does_meet_non_id_filters
    FROM demo.DOG_Calc_User_Access_Temp CALC_USER_T
    WHERE CALC_USER_T.GUID = a_guid
    ;
    
    IF a_debug = 1 THEN
        SELECT 'After get many user';
        SELECT * FROM tmp_Calc_User_Access_Calc_Obedience_Level;
    END IF;
	
	CALL demo.p_dog_clear_calc_user_access(
		a_guid
        , 0 -- a_debug
	);


    -- Obedience_Levels
    IF v_has_filter_obedience_level_id = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_ids_obedience_level, ',', a_debug);

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

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Name_Calc_Obedience_Level (
			  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_Obedience_Level 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_Obedience_Level t_SPLIT_ID
            LEFT JOIN fetchmetrics.DOG_Obedience_Level OBEDIENCE_LEVEL ON t_SPLIT_ID.as_int = OBEDIENCE_LEVEL.id_obedience_level 
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(OBEDIENCE_LEVEL.id_obedience_level)
				OR  (
					OBEDIENCE_LEVEL.active = 0
					AND a_get_inactive_obedience_level = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error_Calc_Obedience_Level (
				  id_type
		        , code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Obedience_Level IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_ID.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Id_Calc_Obedience_Level t_SPLIT_ID
			LEFT JOIN fetchmetrics.DOG_Obedience_Level OBEDIENCE_LEVEL ON t_SPLIT_ID.as_int = OBEDIENCE_LEVEL.id_obedience_level
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(OBEDIENCE_LEVEL.id_obedience_level)
				OR  (
					OBEDIENCE_LEVEL.active = 0
					AND a_get_inactive_obedience_level = 0
				)
			;
        /* Don't error on names, hand signals, or notes not found
		ELSEIF EXISTS ()
        */
		ELSE
			INSERT INTO tmp_Obedience_Level_Calc_Obedience_Level (
				  id_obedience_level
                , exists_valid_link
                , id_user
                , does_meet_id_filters
                , does_meet_non_id_filters
			)
            WITH 
                  Obedience_Level_Id_Filter AS (
                    SELECT OBEDIENCE_LEVEL.id_obedience_level
                    FROM tmp_Split_Id_Calc_Obedience_Level t_SPLIT_ID
                    INNER JOIN fetchmetrics.DOG_Obedience_Level OBEDIENCE_LEVEL ON t_SPLIT_ID.as_int = OBEDIENCE_LEVEL.id_obedience_level
                )
                , Obedience_Level_Name_Filter AS (
                    SELECT OBEDIENCE_LEVEL.id_obedience_level
                    FROM tmp_Split_Name_Calc_Obedience_Level t_SPLIT_NAME
                    INNER JOIN fetchmetrics.DOG_Obedience_Level OBEDIENCE_LEVEL ON OBEDIENCE_LEVEL.name LIKE CONCAT('%', t_SPLIT_NAME.substring, '%')
                    WHERE NULLIF(t_SPLIT_NAME.substring, '') IS NOT NULL
                )
                , Obedience_Level_Filters AS (
                    SELECT 
                          OBEDIENCE_LEVEL_COMBINED.id_obedience_level
                        , MAX(OBEDIENCE_LEVEL_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                        , MAX(OBEDIENCE_LEVEL_COMBINED.does_meet_name_filter) AS does_meet_name_filter
                    FROM (
                        SELECT 
                              OBEDIENCE_LEVEL_ID_FILTER.id_obedience_level 
                            , 1 AS does_meet_id_filter
                            , 0 AS does_meet_name_filter
                        FROM Obedience_Level_Id_Filter OBEDIENCE_LEVEL_ID_FILTER
                        UNION
                        SELECT 
                              OBEDIENCE_LEVEL_NAME_FILTER.id_obedience_level 
                            , 0 AS does_meet_id_filter
                            , 1 AS does_meet_name_filter
                        FROM Obedience_Level_Name_Filter OBEDIENCE_LEVEL_NAME_FILTER
                    ) OBEDIENCE_LEVEL_COMBINED
                    GROUP BY OBEDIENCE_LEVEL_COMBINED.id_obedience_level
                )
                , Obedience_Level_Access AS (
                    SELECT 
                        OBEDIENCE_LEVEL.id_obedience_level
                        , CASE WHEN 
                                v_is_super_user = 1
                            OR  (
                                    t_USER.id_user IS NOT NULL
                                AND IFNULL(ACCESS_LEVEL.priority, v_priority_access_level_none) <= v_priority_access_level_view
                            )
                        THEN 1 ELSE 0 END AS exists_valid_link
                        , ROW_NUMBER() OVER (
                            PARTITION BY OBEDIENCE_LEVEL.id_obedience_level
                            ORDER BY 
                                  CASE WHEN 
                                        v_is_super_user = 1
                                    OR  (
                                            t_USER.id_user IS NOT NULL
                                        AND IFNULL(ACCESS_LEVEL.priority, v_priority_access_level_none) <= v_priority_access_level_view
                                    )
                                THEN 1 ELSE 0 END DESC
                                , t_USER.does_meet_id_filters DESC
                                , t_USER.does_meet_non_id_filters DESC
                        ) AS index_link_in_obedience_level
                        , t_USER.id_user
                    FROM fetchmetrics.DOG_Obedience_Level OBEDIENCE_LEVEL
                    LEFT JOIN fetchmetrics.DOG_Obedience_Level_User_Link OBEDIENCE_LEVEL_USER_LINK
                        ON  OBEDIENCE_LEVEL.id_obedience_level = OBEDIENCE_LEVEL_USER_LINK.id_obedience_level
                        AND (
                                (
                                    a_get_inactive_obedience_level = 1
                                AND a_get_inactive_user = 1
                            )
                            OR  OBEDIENCE_LEVEL_USER_LINK.active = 1
                        )
                    LEFT JOIN tmp_Calc_User_Access_Calc_Obedience_Level t_USER 
                        ON  OBEDIENCE_LEVEL_USER_LINK.id_user = t_USER.id_user
                        AND (
                                a_get_inactive_user = 1
                            OR  t_USER.active = 1
                        )
                    LEFT JOIN fetchmetrics.DOG_Access_Level ACCESS_LEVEL 
                        ON  OBEDIENCE_LEVEL_USER_LINK.id_access_level = ACCESS_LEVEL.id_access_level
                        AND ACCESS_LEVEL.active = 1
                )
            SELECT 
                  OBEDIENCE_LEVEL.id_obedience_level
                , IFNULL(OBEDIENCE_LEVEL_ACCESS.exists_valid_link, 0) AS exists_valid_link
                , OBEDIENCE_LEVEL_ACCESS.id_user
                , CASE WHEN 
                        v_has_filter_obedience_level_id = 0 
                    OR  IFNULL(OBEDIENCE_LEVEL_FILTERS.does_meet_id_filter, 0) = 1
                THEN 1 ELSE 0 END AS does_meet_id_filters
                , CASE WHEN 
                        (
                            v_has_filter_obedience_level_name = 0
                    )
                    OR  IFNULL(OBEDIENCE_LEVEL_FILTERS.does_meet_name_filter, 0) = 1
                THEN 1 ELSE 0 END AS does_meet_non_id_filters
            FROM fetchmetrics.DOG_Obedience_Level OBEDIENCE_LEVEL
            LEFT JOIN Obedience_Level_Filters OBEDIENCE_LEVEL_FILTERS ON OBEDIENCE_LEVEL.id_obedience_level = OBEDIENCE_LEVEL_FILTERS.id_obedience_level
            LEFT JOIN Obedience_Level_Access OBEDIENCE_LEVEL_ACCESS 
                ON  OBEDIENCE_LEVEL.id_obedience_level = OBEDIENCE_LEVEL_ACCESS.id_obedience_level
                AND OBEDIENCE_LEVEL_ACCESS.index_link_in_obedience_level = 1
            WHERE 
                (
                        a_get_all_obedience_level = 1
                    OR  (
                            v_has_filter_obedience_level_id = 1
                        AND OBEDIENCE_LEVEL_FILTERS.does_meet_id_filter = 1
                    )
                    OR  (
                            v_has_filter_obedience_level_name = 1
                        AND OBEDIENCE_LEVEL_FILTERS.does_meet_name_filter = 1
                    )
                )
                AND IFNULL(OBEDIENCE_LEVEL_ACCESS.exists_valid_link, 0) = 1
                AND (
                        a_get_inactive_obedience_level = 1
                    OR  OBEDIENCE_LEVEL.active = 1
                )
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split_Id_Calc_Obedience_Level;
    DELETE FROM tmp_Split_Name_Calc_Obedience_Level;

    IF a_debug = 1 THEN
        SELECT 'After get Obedience_Levels ';
        SELECT * FROM tmp_Obedience_Level_Calc_Obedience_Level;
    END IF;

    -- Filter records
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Obedience_Level 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_OBEDIENCE_LEVEL
        FROM tmp_Obedience_Level_Calc_Obedience_Level t_OBEDIENCE_LEVEL
        LEFT JOIN tmp_Calc_User_Access_Calc_Obedience_Level t_USER ON t_OBEDIENCE_LEVEL.id_user = t_USER.id_user
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND (
                        t_OBEDIENCE_LEVEL.does_meet_id_filters = 0
                    OR  (
                            t_OBEDIENCE_LEVEL.exists_valid_link = 0
                        AND IFNULL(t_USER.does_meet_id_filters, 0) = 0
                    )
                )
            )
            OR  (
                    a_require_all_non_id_search_filters_met = 1
                AND (
                        t_OBEDIENCE_LEVEL.does_meet_non_id_filters = 0
                    OR  (
                            t_OBEDIENCE_LEVEL.exists_valid_link = 0
                        AND IFNULL(t_USER.does_meet_non_id_filters, 0) = 0
                    )
                )
            )
            OR  (
                a_require_any_id_search_filters_met = 1
                AND t_OBEDIENCE_LEVEL.does_meet_id_filters = 0
                AND (
                        t_OBEDIENCE_LEVEL.exists_valid_link = 0
                    AND IFNULL(t_USER.does_meet_id_filters, 0) = 0
                )
            )
            OR  (
                a_require_any_non_id_search_filters_met = 1
                AND t_OBEDIENCE_LEVEL.does_meet_non_id_filters = 0
                AND (
                        t_OBEDIENCE_LEVEL.exists_valid_link = 0
                    AND IFNULL(t_USER.does_meet_non_id_filters, 0) = 0
                )
            )
        ;
    END IF;

    IF a_debug = 1 THEN
        SELECT 'After filter Obedience_Levels';
        SELECT * FROM tmp_Obedience_Level_Calc_Obedience_Level;
    END IF;

    -- Calculated fields

    IF a_debug = 1 THEN
        SELECT 'Before non-permitted data deletion';
        SELECT * FROM tmp_Obedience_Level_Calc_Obedience_Level;
        SELECT * FROM tmp_Msg_Error_Calc_Obedience_Level;
    END IF;
    
    IF EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Obedience_Level 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_Obedience_Level_Calc_Obedience_Level;
        END IF;

        DELETE FROM tmp_Obedience_Level_Calc_Obedience_Level;
    END IF;

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

    -- Outputs
	START TRANSACTION;
        -- Obedience_Levels
        INSERT INTO fetchmetrics.DOG_Obedience_Level_Temp (
              guid
            , id_obedience_level
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT 
              a_guid
            , t_OBEDIENCE_LEVEL.id_obedience_level
            , OBEDIENCE_LEVEL.code
            , OBEDIENCE_LEVEL.name
            , OBEDIENCE_LEVEL.active

            , t_OBEDIENCE_LEVEL.does_meet_id_filters
            , t_OBEDIENCE_LEVEL.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Obedience_Level OBEDIENCE_LEVEL
        INNER JOIN tmp_Obedience_Level_Calc_Obedience_Level t_OBEDIENCE_LEVEL ON OBEDIENCE_LEVEL.id_obedience_level = t_OBEDIENCE_LEVEL.id_obedience_level
        ORDER BY OBEDIENCE_LEVEL.name
        ;
    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_Obedience_Level 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_Obedience_Level_Calc_Obedience_Level;
    END IF;
    
    CALL fetchmetrics.p_dog_clear_calc_dog ( a_guid, 0 );

    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name_Calc_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Obedience_Level_Calc_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Calc_User_Access_Calc_Obedience_Level;
    
    IF a_debug = 1 THEN
		CALL fetchmetrics.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*


CALL fetchmetrics.p_dog_calc_obedience_level (
      'gripe                               ' -- a_guid
	, 1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_obedience_level
	, 0                                     	-- a_get_inactive_obedience_level
	, ''                                    	-- a_ids_obedience_level
    , ''                                        -- a_names_obedience_level
    , 1                                     	-- a_get_all_user
	, 0                                     	-- a_get_inactive_user
	, ''                                    	-- a_ids_user
    , ''                                        -- a_names_user
    , ''                                        -- a_emails_user
    , 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_Obedience_Level_Temp
;
/*
SELECT *
FROM fetchmetrics.DOG_Obedience_Level_Temp C
WHERE 
        C.does_meet_id_filters
    AND C.does_meet_non_id_filters
;
*/
CALL fetchmetrics.p_dog_clear_calc_obedience_level (
	'gripe                               ' -- a_guid
    , 1 -- debug
);


DELETE
FROM fetchmetrics.DOG_Obedience_Level_Temp
;

*/
