 
USE fetchmetrics;

DROP PROCEDURE IF EXISTS fetchmetrics.p_dog_calc_button_shape;

DELIMITER //
CREATE PROCEDURE fetchmetrics.p_dog_calc_button_shape (
      IN a_guid BINARY(36)
    , IN a_id_user INT
    , IN a_get_all_button_shape BIT
	, IN a_get_inactive_button_shape BIT
	, IN a_ids_button_shape TEXT
    , IN a_names_button_shape TEXT
    , IN a_notes_button_shape 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_button_shape_id BIT;
    DECLARE v_has_filter_button_shape_name BIT;
    DECLARE v_has_filter_button_shape_notes 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_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_Button_Shape (
			  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_Button_Shape (
			  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_Button_Shape 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_Button_Shape;
    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);
    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_button_shape := IFNULL(a_get_all_button_shape, 0);
    SET a_get_inactive_button_shape := IFNULL(a_get_inactive_button_shape, 0);
    SET a_ids_button_shape := TRIM(IFNULL(a_ids_button_shape, ''));
    SET a_names_button_shape := TRIM(IFNULL(a_names_button_shape, ''));
    SET a_notes_button_shape := TRIM(IFNULL(a_notes_button_shape, ''));
    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_button_shape
            , a_get_inactive_button_shape
            , a_ids_button_shape
            , a_names_button_shape
            , a_notes_button_shape
            , 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_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name_Calc_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Button_Shape_Calc_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Calc_User_Access_Calc_Button_Shape;
    
	CREATE TEMPORARY TABLE tmp_Calc_User_Access_Calc_Button_Shape (
		  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_Button_Shape_Calc_Button_Shape (
		  id_button_shape 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_Button_Shape (
          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_Button_Shape (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Id_Calc_Button_Shape;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Name_Calc_Button_Shape (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Name_Calc_Button_Shape;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Notes_Calc_Button_Shape (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Notes_Calc_Button_Shape;
    
    SET v_has_filter_button_shape_id = CASE WHEN a_ids_button_shape <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_button_shape_name = CASE WHEN a_names_button_shape <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_button_shape_notes = CASE WHEN a_notes_button_shape <> '' 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_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)
        , 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_Button_Shape t_ME
        WHERE t_ME.id_type <> v_id_type_error_no_permission
        ;
		INSERT INTO tmp_Msg_Error_Calc_Button_Shape (
			  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_Shapes.'
		)
        ;
    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_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-- 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_id_permission_dog_view -- ids_permission
        , v_id_access_level_view -- ids_access_level
        , 0 -- a_show_errors
        , 0 -- a_debug
    );
    
    INSERT INTO tmp_Calc_User_Access_Calc_Button_Shape (
        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 fetchmetrics.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_Button_Shape;
        SELECT COUNT(*) AS Count_Errors FROM tmp_Msg_Error_Calc_Button_Shape t_ERROR;
        SELECT * FROM tmp_Msg_Error_Calc_Button_Shape t_ERROR;
    END IF;
	
	CALL fetchmetrics.p_dog_clear_calc_user_access(
		a_guid
        , 0 -- a_debug
	);


    -- Button_Shapes
    IF v_has_filter_button_shape_id = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_ids_button_shape, ',', a_debug);

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

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

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Notes_Calc_Button_Shape (
			  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_Button_Shape 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_Button_Shape t_SPLIT_ID
            LEFT JOIN fetchmetrics.DOG_Button_Shape BUTTON_SHAPE ON t_SPLIT_ID.as_int = BUTTON_SHAPE.id_button_shape 
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(BUTTON_SHAPE.id_button_shape)
				OR  (
					BUTTON_SHAPE.active = 0
					AND a_get_inactive_button_shape = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error_Calc_Button_Shape (
				  id_type
		        , code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Button_Shape IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_ID.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Id_Calc_Button_Shape t_SPLIT_ID
			LEFT JOIN fetchmetrics.DOG_Button_Shape BUTTON_SHAPE ON t_SPLIT_ID.as_int = BUTTON_SHAPE.id_button_shape
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(BUTTON_SHAPE.id_button_shape)
				OR  (
					BUTTON_SHAPE.active = 0
					AND a_get_inactive_button_shape = 0
				)
			;
        /* Don't error on names, hand signals, or notes not found
		ELSEIF EXISTS ()
        */
		ELSE
			INSERT INTO tmp_Button_Shape_Calc_Button_Shape (
				  id_button_shape
                , exists_valid_link
                , id_user
                , does_meet_id_filters
                , does_meet_non_id_filters
			)
            WITH 
                  Button_Shape_Id_Filter AS (
                    SELECT BUTTON_SHAPE.id_button_shape
                    FROM tmp_Split_Id_Calc_Button_Shape t_SPLIT_ID
                    INNER JOIN fetchmetrics.DOG_Button_Shape BUTTON_SHAPE ON t_SPLIT_ID.as_int = BUTTON_SHAPE.id_button_shape
                )
                , Button_Shape_Name_Filter AS (
                    SELECT BUTTON_SHAPE.id_button_shape
                    FROM tmp_Split_Name_Calc_Button_Shape t_SPLIT_NAME
                    INNER JOIN fetchmetrics.DOG_Button_Shape BUTTON_SHAPE ON BUTTON_SHAPE.name LIKE CONCAT('%', t_SPLIT_NAME.substring, '%')
                    WHERE NULLIF(t_SPLIT_NAME.substring, '') IS NOT NULL
                )
                , Button_Shape_Notes_Filter AS (
                    SELECT BUTTON_SHAPE.id_button_shape
                    FROM tmp_Split_Notes_Calc_Button_Shape t_SPLIT_NOTES
                    INNER JOIN fetchmetrics.DOG_Button_Shape BUTTON_SHAPE ON BUTTON_SHAPE.name LIKE CONCAT('%', t_SPLIT_NOTES.substring, '%')
                    WHERE NULLIF(t_SPLIT_NOTES.substring, '') IS NOT NULL
                )
                , Button_Shape_Filters AS (
                    SELECT 
                          BUTTON_SHAPE_COMBINED.id_button_shape
                        , MAX(BUTTON_SHAPE_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                        , MAX(BUTTON_SHAPE_COMBINED.does_meet_name_filter) AS does_meet_name_filter
                        , MAX(BUTTON_SHAPE_COMBINED.does_meet_notes_filter) AS does_meet_notes_filter
                    FROM (
                        SELECT 
                              BUTTON_SHAPE_ID_FILTER.id_button_shape 
                            , 1 AS does_meet_id_filter
                            , 0 AS does_meet_name_filter
                            , 0 AS does_meet_notes_filter
                        FROM Button_Shape_Id_Filter BUTTON_SHAPE_ID_FILTER
                        UNION
                        SELECT 
                              BUTTON_SHAPE_NAME_FILTER.id_button_shape 
                            , 0 AS does_meet_id_filter
                            , 1 AS does_meet_name_filter
                            , 0 AS does_meet_notes_filter
                        FROM Button_Shape_Name_Filter BUTTON_SHAPE_NAME_FILTER
                        UNION
                        SELECT 
                              BUTTON_SHAPE_NOTES_FILTER.id_button_shape 
                            , 0 AS does_meet_id_filter
                            , 0 AS does_meet_name_filter
                            , 1 AS does_meet_notes_filter
                        FROM Button_Shape_Notes_Filter BUTTON_SHAPE_NOTES_FILTER
                    ) BUTTON_SHAPE_COMBINED
                    GROUP BY BUTTON_SHAPE_COMBINED.id_button_shape
                )
                , Button_Shape_Access AS (
                    SELECT 
                        BUTTON_SHAPE.id_button_shape
                        , 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 BUTTON_SHAPE.id_button_shape
                            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_button_shape
                        , t_USER.id_user
                    FROM fetchmetrics.DOG_Button_Shape BUTTON_SHAPE
                    LEFT JOIN fetchmetrics.DOG_Button_Shape_User_Link BUTTON_SHAPE_USER_LINK
                        ON  BUTTON_SHAPE.id_button_shape = BUTTON_SHAPE_USER_LINK.id_button_shape
                        AND (
                                (
                                    a_get_inactive_button_shape = 1
                                AND a_get_inactive_user = 1
                            )
                            OR  BUTTON_SHAPE_USER_LINK.active = 1
                        )
                    LEFT JOIN tmp_Calc_User_Access_Calc_Button_Shape t_USER 
                        ON  BUTTON_SHAPE_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  BUTTON_SHAPE_USER_LINK.id_access_level = ACCESS_LEVEL.id_access_level
                        AND ACCESS_LEVEL.active = 1
                )
            SELECT 
                  BUTTON_SHAPE.id_button_shape
                , BUTTON_SHAPE_ACCESS.exists_valid_link
                , BUTTON_SHAPE_ACCESS.id_user
                , CASE WHEN 
                        v_has_filter_button_shape_id = 0 
                    OR  IFNULL(BUTTON_SHAPE_FILTERS.does_meet_id_filter, 0) = 1
                THEN 1 ELSE 0 END AS does_meet_id_filters
                , CASE WHEN 
                        (
                            v_has_filter_button_shape_name = 0
                        AND v_has_filter_button_shape_notes = 0
                    )
                    OR  IFNULL(BUTTON_SHAPE_FILTERS.does_meet_name_filter, 0) = 1
                    OR  IFNULL(BUTTON_SHAPE_FILTERS.does_meet_notes_filter, 0) = 1
                THEN 1 ELSE 0 END AS does_meet_non_id_filters
            FROM fetchmetrics.DOG_Button_Shape BUTTON_SHAPE
            LEFT JOIN Button_Shape_Filters BUTTON_SHAPE_FILTERS ON BUTTON_SHAPE.id_button_shape = BUTTON_SHAPE_FILTERS.id_button_shape
            LEFT JOIN Button_Shape_Access BUTTON_SHAPE_ACCESS 
                ON  BUTTON_SHAPE.id_button_shape = BUTTON_SHAPE_ACCESS.id_button_shape
                AND BUTTON_SHAPE_ACCESS.index_link_in_button_shape = 1
            WHERE 
                (
                        a_get_all_button_shape = 1
                    OR  (
                            v_has_filter_button_shape_id = 1
                        AND BUTTON_SHAPE_FILTERS.does_meet_id_filter = 1
                    )
                    OR  (
                            v_has_filter_button_shape_name = 1
                        AND BUTTON_SHAPE_FILTERS.does_meet_name_filter = 1
                    )
                )
                AND IFNULL(BUTTON_SHAPE_ACCESS.exists_valid_link, 0) = 1
                AND (
                        a_get_inactive_button_shape = 1
                    OR  BUTTON_SHAPE.active = 1
                )
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split_Id_Calc_Button_Shape;
    DELETE FROM tmp_Split_Name_Calc_Button_Shape;
    DELETE FROM tmp_Split_Notes_Calc_Button_Shape;

    IF a_debug = 1 THEN
        SELECT 'After get Button_Shapes ';
        SELECT * FROM tmp_Button_Shape_Calc_Button_Shape;
    END IF;

    -- Filter records
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Button_Shape 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_BUTTON_SHAPE
        FROM tmp_Button_Shape_Calc_Button_Shape t_BUTTON_SHAPE
        LEFT JOIN tmp_Calc_User_Access_Calc_Button_Shape t_USER ON t_BUTTON_SHAPE.id_user = t_USER.id_user
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND (
                        t_BUTTON_SHAPE.does_meet_id_filters = 0
                    OR  (
                            t_BUTTON_SHAPE.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_BUTTON_SHAPE.does_meet_non_id_filters = 0
                    OR  (
                            t_BUTTON_SHAPE.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_BUTTON_SHAPE.does_meet_id_filters = 0
                AND (
                        t_BUTTON_SHAPE.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_BUTTON_SHAPE.does_meet_non_id_filters = 0
                AND (
                        t_BUTTON_SHAPE.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 Button_Shapes';
        SELECT * FROM tmp_Button_Shape_Calc_Button_Shape;
    END IF;

    -- Calculated fields

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

        DELETE FROM tmp_Button_Shape_Calc_Button_Shape;
    END IF;

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

    -- Outputs
	START TRANSACTION;
        -- Button_Shapes
        INSERT INTO fetchmetrics.DOG_Button_Shape_Temp (
              guid
            , id_button_shape
            , code
            , name
            , notes
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT 
              a_guid
            , t_BUTTON_SHAPE.id_button_shape
            , BUTTON_SHAPE.code
            , BUTTON_SHAPE.name
            , BUTTON_SHAPE.notes
            , BUTTON_SHAPE.active

            , t_BUTTON_SHAPE.does_meet_id_filters
            , t_BUTTON_SHAPE.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Button_Shape BUTTON_SHAPE
        INNER JOIN tmp_Button_Shape_Calc_Button_Shape t_BUTTON_SHAPE ON BUTTON_SHAPE.id_button_shape = t_BUTTON_SHAPE.id_button_shape
        ORDER BY BUTTON_SHAPE.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_Button_Shape 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_Button_Shape_Calc_Button_Shape;
    END IF;

    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Notes_Calc_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name_Calc_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Button_Shape_Calc_Button_Shape;
    DROP TEMPORARY TABLE IF EXISTS tmp_Calc_User_Access_Calc_Button_Shape;
    
    IF a_debug = 1 THEN
		CALL fetchmetrics.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*

SELECT *
FROM fetchmetrics.DOG_Button_Shape
;

CALL fetchmetrics.p_dog_calc_button_shape (
      'gripe                               ' -- a_guid
	, 1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_button_shape
	, 0                                     	-- a_get_inactive_button_shape
	, ''                                    	-- a_ids_button_shape
    , ''                                        -- a_names_button_shape
    , ''                                        -- a_notes_button_shape
    , 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_Button_Shape_Temp
;
CALL fetchmetrics.p_dog_clear_calc_button_shape (
	'gripe                               ' -- a_guid
    , 1 -- debug
);


DELETE
FROM fetchmetrics.DOG_Button_Shape_Temp
;

*/
