 
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_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_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);
    
    
    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_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;
    
    CREATE TEMPORARY TABLE tmp_Button_Shape_Calc_Button_Shape (
		  id_button_shape INT NOT NULL
        , 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;

    -- 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_SHAPES ON t_SPLIT_ID.as_int = BUTTON_SHAPES.id_button_shape 
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(BUTTON_SHAPES.id_button_shape)
				OR  (
					BUTTON_SHAPES.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_SHAPES ON t_SPLIT_ID.as_int = BUTTON_SHAPES.id_button_shape
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(BUTTON_SHAPES.id_button_shape)
				OR  (
					BUTTON_SHAPES.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
                , does_meet_id_filters
                , does_meet_non_id_filters
			)
            WITH 
                  Button_Shape_Id_Filter AS (
                    SELECT BUTTON_SHAPES.id_button_shape
                    FROM tmp_Split_Id_Calc_Button_Shape t_SPLIT_ID
                    INNER JOIN fetchmetrics.DOG_Button_Shape BUTTON_SHAPES ON t_SPLIT_ID.as_int = BUTTON_SHAPES.id_button_shape
                )
                , Button_Shape_Name_Filter AS (
                    SELECT BUTTON_SHAPES.id_button_shape
                    FROM tmp_Split_Name_Calc_Button_Shape t_SPLIT_NAME
                    INNER JOIN fetchmetrics.DOG_Button_Shape BUTTON_SHAPES ON BUTTON_SHAPES.name LIKE CONCAT('%', t_SPLIT_NAME.substring, '%')
                    WHERE NULLIF(t_SPLIT_NAME.substring, '') IS NOT NULL
                )
                , Button_Shape_Notes_Filter AS (
                    SELECT BUTTON_SHAPES.id_button_shape
                    FROM tmp_Split_Notes_Calc_Button_Shape t_SPLIT_NOTES
                    INNER JOIN fetchmetrics.DOG_Button_Shape BUTTON_SHAPES ON BUTTON_SHAPES.name LIKE CONCAT('%', t_SPLIT_NOTES.substring, '%')
                    WHERE NULLIF(t_SPLIT_NOTES.substring, '') IS NOT NULL
                )
                , Button_Shape_Filters AS (
                    SELECT 
                          BUTTON_SHAPES_COMBINED.id_button_shape
                        , MAX(BUTTON_SHAPES_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                        , MAX(BUTTON_SHAPES_COMBINED.does_meet_name_filter) AS does_meet_name_filter
                        , MAX(BUTTON_SHAPES_COMBINED.does_meet_notes_filter) AS does_meet_notes_filter
                    FROM (
                        SELECT 
                              BUTTON_SHAPES_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_SHAPES_ID_FILTER
                        UNION
                        SELECT 
                              BUTTON_SHAPES_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_SHAPES_NAME_FILTER
                        UNION
                        SELECT 
                              BUTTON_SHAPES_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_SHAPES_NOTES_FILTER
                    ) BUTTON_SHAPES_COMBINED
                    GROUP BY BUTTON_SHAPES_COMBINED.id_button_shape
                )
            SELECT 
                  BUTTON_SHAPES.id_button_shape
                , CASE WHEN 
                        v_has_filter_button_shape_id = 0 
                    OR  IFNULL(BUTTON_SHAPES_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_SHAPES_FILTERS.does_meet_name_filter, 0) = 1
                    OR  IFNULL(BUTTON_SHAPES_FILTERS.does_meet_notes_filter, 0) = 1
                THEN 1 ELSE 0 END AS does_meet_non_id_filters
            FROM fetchmetrics.DOG_Button_Shape BUTTON_SHAPES
            LEFT JOIN Button_Shape_Filters BUTTON_SHAPES_FILTERS ON BUTTON_SHAPES.id_button_shape = BUTTON_SHAPES_FILTERS.id_button_shape
            WHERE 
                (
                        a_get_all_button_shape = 1
                    OR  (
                            v_has_filter_button_shape_id = 1
                        AND BUTTON_SHAPES_FILTERS.does_meet_id_filter = 1
                    )
                    OR  (
                            v_has_filter_button_shape_name = 1
                        AND BUTTON_SHAPES_FILTERS.does_meet_name_filter = 1
                    )
                )
                AND (
                        a_get_inactive_button_shape = 1
                    OR  BUTTON_SHAPES.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_SHAPES
        FROM tmp_Button_Shape_Calc_Button_Shape t_BUTTON_SHAPES
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND (
                        t_BUTTON_SHAPES.does_meet_id_filters = 0
                )
            )
            OR  (
                    a_require_all_non_id_search_filters_met = 1
                AND (
                        t_BUTTON_SHAPES.does_meet_non_id_filters = 0
                )
            )
            OR  (
                a_require_any_id_search_filters_met = 1
                AND t_BUTTON_SHAPES.does_meet_id_filters = 0
            )
            OR  (
                a_require_any_non_id_search_filters_met = 1
                AND t_BUTTON_SHAPES.does_meet_non_id_filters = 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

    -- 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(
          a_guid -- a_guid
        , 0 -- get_all_user
        , 0 -- get_inactive_user
        , a_id_user -- ids_user
        , '' -- a_auth0_ids_user
        , '' -- a_names_user
        , '' -- a_emails_user
        , 1 -- a_require_all_id_search_filters_met
        , 1 -- a_require_any_id_search_filters_met
        , 0 -- a_require_all_non_id_search_filters_met
        , 0 -- a_require_any_non_id_search_filters_met
        , v_id_permission_dog_view -- ids_permission
        , v_id_access_level_view -- ids_access_level
        , 0 -- a_show_errors
        , 0 -- a_debug
    );
	
    SELECT
          IFNULL(CALC_USER_T.has_access, 0)
    INTO
          v_can_view
    FROM fetchmetrics.DOG_Calc_User_Temp CALC_USER_T
    WHERE CALC_USER_T.GUID = a_guid
    LIMIT 1
    ;
    
    IF a_debug = 1 THEN
        SELECT v_can_view;
    END IF;
    
    IF (v_can_view = 0) THEN
		DELETE t_ME
        FROM tmp_Msg_Error_Calc_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(
		a_guid
        , 0 -- a_debug
	);

    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_SHAPES.id_button_shape
            , BUTTON_SHAPES.code
            , BUTTON_SHAPES.name
            , BUTTON_SHAPES.notes
            , BUTTON_SHAPES.active

            , t_BUTTON_SHAPES.does_meet_id_filters
            , t_BUTTON_SHAPES.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Button_Shape BUTTON_SHAPES
        INNER JOIN tmp_Button_Shape_Calc_Button_Shape t_BUTTON_SHAPES ON BUTTON_SHAPES.id_button_shape = t_BUTTON_SHAPES.id_button_shape
        ORDER BY BUTTON_SHAPES.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;
    
    CALL fetchmetrics.p_dog_clear_calc_dog ( a_guid, 0 );

    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;
    
    IF a_debug = 1 THEN
		CALL fetchmetrics.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*


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
    , 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
;
/*
SELECT *
FROM fetchmetrics.DOG_Button_Shape_Temp C
WHERE 
        C.does_meet_id_filters
    AND C.does_meet_non_id_filters
;
*/
CALL fetchmetrics.p_dog_clear_calc_button_shape (
	'gripe                               ' -- a_guid
    , 1 -- debug
);


DELETE
FROM fetchmetrics.DOG_Button_Shape_Temp
;

*/
