
USE demo;

-- Clear previous proc
DROP PROCEDURE IF EXISTS p_dog_get_many_user;


DELIMITER //
CREATE PROCEDURE p_dog_get_many_user (
	IN a_id_user INT
    , IN a_id_user_auth0 VARCHAR(200)
	, IN a_get_all_user BIT
	, IN a_get_inactive_user BIT
	, IN a_ids_user TEXT
    , IN a_ids_user_auth0 TEXT
    , IN a_names_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_debug BIT
)
BEGIN
    DECLARE v_code_type_error_bad_data VARCHAR(50);
    DECLARE v_guid BINARY(36);
    DECLARE v_has_filter_user_auth0_id BIT;
    DECLARE v_has_filter_user_id BIT;
    DECLARE v_has_filter_user_name BIT;
    DECLARE v_id_access_level_admin INT;
    DECLARE v_id_access_level_view INT;
    DECLARE v_id_minimum INT;
    DECLARE v_id_permission_dog_admin INT;
    DECLARE v_id_permission_user INT;
    DECLARE v_id_permission_user_admin INT;
    DECLARE v_id_type_error_bad_data INT;
    DECLARE v_ids_permission_required VARCHAR(4000);
    DECLARE v_is_new BIT;
    DECLARE v_rank_max 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 (
			  id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
			, id_type INT NULL
			, code VARCHAR(100) NOT NULL
			, msg TEXT NOT NULL
		);

        INSERT INTO tmp_Msg_Error (
			  id_type
            , code
            , msg
		)
        SELECT 
			  MET.id_type
            , @errno
            , @text
		FROM demo.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 t_ERROR
        INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
        ;
        
		DROP TABLE IF EXISTS tmp_Msg_Error;
    END;
    
    SET v_time_start := CURRENT_TIMESTAMP(6);
    SET v_guid := UUID();
    SET v_id_access_level_admin := (SELECT id_access_level FROM demo.DOG_Access_Level WHERE code = 'ADMIN' LIMIT 1);
    SET v_id_access_level_view := (SELECT id_access_level FROM demo.DOG_Access_Level WHERE code = 'VIEW' LIMIT 1);
    SET v_id_permission_dog_admin := (SELECT id_permission FROM demo.DOG_Permission WHERE code = 'DOG_ADMIN' LIMIT 1);
    SET v_id_permission_user := (SELECT id_permission FROM demo.DOG_Permission WHERE code = 'USER_VIEW' LIMIT 1);
    SET v_id_permission_user_admin := (SELECT id_permission FROM demo.DOG_Permission WHERE code = 'USER_ADMIN' LIMIT 1);
    SET v_code_type_error_bad_data := 'BAD_DATA';
    SET v_id_type_error_bad_data := (SELECT id_type FROM demo.CORE_Msg_Error_Type WHERE code = v_code_type_error_bad_data LIMIT 1);
    SET v_ids_permission_required := CONCAT(v_id_permission_user, ',', v_id_permission_user_admin, ',', v_id_permission_dog_admin);
    SET v_is_new := FALSE;
    
    SET a_get_all_user := IFNULL(a_get_all_user, 1);
    SET a_get_inactive_user := IFNULL(a_get_inactive_user, 0);
    SET a_ids_user := TRIM(IFNULL(a_ids_user, ''));
    SET a_ids_user_auth0 := TRIM(IFNULL(a_ids_user_auth0, ''));
    SET a_names_user := TRIM(IFNULL(a_names_user, ''));
    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_debug := IFNULL(a_debug, 0);
    
    IF a_debug = 1 THEN
		SELECT
			a_id_user
			, a_id_user_auth0
			, a_get_all_user
			, a_get_inactive_user
			, a_ids_user
			, a_ids_user_auth0
            , a_names_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_debug
		;
    END IF;
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Auth0_Id;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_User;
    
    CREATE TEMPORARY TABLE tmp_User (
		  id_user INT NULL
        , can_admin_dog BIT NULL
        , can_admin_user BIT NULL
        , does_meet_id_filters BIT
        , does_meet_non_id_filters BIT
    );
    
	CREATE TEMPORARY TABLE tmp_Msg_Error (
		  id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
		, id_type INT NOT NULL
        , code VARCHAR(100) NOT NULL
        , msg TEXT NOT NULL
	);
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Id (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT NULL
	);
    DELETE FROM tmp_Split_Id;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Auth0_Id (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT NULL
	);
    DELETE FROM tmp_Split_Auth0_Id;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Name (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT NULL
	);
    DELETE FROM tmp_Split_Name;
    
    IF ISNULL(a_id_user) AND NOT ISNULL(a_id_user_auth0) THEN
		SET a_id_user := (SELECT U.id_user FROM demo.DOG_User U WHERE U.id_user_auth0 = a_id_user_auth0 LIMIT 1);
    END IF;
    
    IF 
            ISNULL(a_id_user) 
        AND ISNULL(a_id_user_auth0)
    THEN
        INSERT INTO tmp_Msg_Error (
              id_type
            , code
            , msg
        )
        VALUES (
              v_id_type_error_bad_data
            , v_code_type_error_bad_data
            , CONCAT('User ID required for authorisation.')
        )
        ;
    END IF;
    
    SET v_has_filter_user_id := CASE WHEN a_ids_user = '' THEN 0 ELSE 1 END;
    SET v_has_filter_user_auth0_id := CASE WHEN a_ids_user_auth0 = '' THEN 0 ELSE 1 END;
    SET v_has_filter_user_name := CASE WHEN a_names_user = '' THEN 0 ELSE 1 END;
    
	IF a_debug = 1 THEN
		SELECT
              v_has_filter_user_id
            , v_has_filter_user_auth0_id
            , v_has_filter_user_name
		;
    END IF;
    
    -- User IDs
    IF v_has_filter_user_id = 1 THEN
		CALL demo.p_core_split(v_guid, a_ids_user, ',', FALSE);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Id (
			  substring
			, as_int
		)
		SELECT 
			  substring
			, CAST(substring AS DECIMAL(10,0)) AS as_int
		FROM demo.CORE_Split_Temp
		WHERE 
                GUID = v_guid
			AND IFNULL(substring, '') != ''
		;
		
		CALL demo.p_core_clear_split( v_guid );
        
		IF EXISTS (
			SELECT * 
            FROM tmp_Split_Id t_SPLIT_ID
            LEFT JOIN demo.DOG_User USER ON t_SPLIT_ID.as_int = USER.id_user
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(USER.id_user)
                OR  USER.active = 0
		) THEN
			INSERT INTO tmp_Msg_Error (
				  id_type
				, code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
				, v_code_type_error_bad_data
				, CONCAT('Invalid or inactive User IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_ID.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Id t_SPLIT_ID
			LEFT JOIN demo.DOG_User U ON t_SPLIT_ID.as_int = USER.id_user
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(USER.id_user)
                OR  USER.active = 0
			;
		END IF;
	END IF;
    
    -- Auth0 User IDs
    IF v_has_filter_user_auth0_id = 1 THEN
		CALL demo.p_core_split(v_guid, a_ids_user_auth0, ',', FALSE);
		
		INSERT INTO tmp_Split_Auth0_Id (
			substring
		)
		SELECT 
			substring
		FROM demo.CORE_Split_Temp
		WHERE 
                GUID = v_guid
			AND IFNULL(substring, '') != ''
		;
		
		CALL demo.p_core_clear_split( v_guid );
        
		IF EXISTS (
			SELECT * 
            FROM tmp_Split_Auth0_Id t_SPLIT_AUTH0
            LEFT JOIN demo.DOG_User USER ON t_SPLIT_AUTH0.substring = USER.id_user_auth0
			WHERE 
				    ISNULL(t_SPLIT_AUTH0.substring) 
                OR  ISNULL(USER.id_user_auth0)
                OR  USER.active = 0
		) THEN
			INSERT INTO tmp_Msg_Error (
				  id_type
				, code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
				, v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Auth0 User IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_AUTH0.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Auth0_Id t_SPLIT_AUTH0
			LEFT JOIN demo.DOG_User USER ON t_SPLIT_AUTH0.substring = USER.id_user_auth0
			WHERE 
				    ISNULL(t_SPLIT_AUTH0.substring) 
				OR  ISNULL(USER.id_user_auth0)
                OR  USER.active = 0
			;
		END IF;
	END IF;
    
    -- User Names
    IF v_has_filter_user_name = 1 THEN
		CALL demo.p_core_split(v_guid, a_names_user, ',', FALSE);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Name (
			  substring
		)
		SELECT 
			  substring
		FROM demo.CORE_Split_Temp
		WHERE 
                GUID = v_guid
			AND IFNULL(substring, '') != ''
		;
		
		CALL demo.p_core_clear_split( v_guid );
	END IF;

    IF
            v_has_filter_user_id = 1
        OR  v_has_filter_user_auth0_id = 1
    THEN
        INSERT INTO tmp_User (
              id_user
            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        WITH 
              User_Id_Filter AS (
                SELECT USER.id_user
                FROM tmp_Split_Id t_SPLIT_ID
                INNER JOIN demo.DOG_User USER ON t_SPLIT_ID.as_int = USER.id_user
            )
            , User_Auth0_Id_Filter AS (
                SELECT USER.id_user
                FROM tmp_Split_Auth0_Id t_SPLIT_AUTH0
                INNER JOIN demo.DOG_User USER ON t_SPLIT_AUTH0.substring = USER.id_user_auth0
            )
            , User_Name_Filter AS (
                SELECT USER.id_user
                FROM tmp_Split_Name t_SPLIT_NAME
                INNER JOIN demo.DOG_User USER ON CONCAT(USER.firstname, ' ', USER.surname) LIKE CONCAT('%', t_SPLIT_NAME.substring, '%')
                WHERE IFNULL(t_SPLIT_NAME.substring, '') <> ''
            )
            , User_Filters AS (
                SELECT 
                        USER_COMBINED.id_user
                    , MAX(USER_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                    , MAX(USER_COMBINED.does_meet_auth0_id_filter) AS does_meet_auth0_id_filter
                    , MAX(USER_COMBINED.does_meet_name_filter) AS does_meet_name_filter
                FROM (
                    SELECT 
                            ID_FILTER.id_user
                        , 1 AS does_meet_id_filter
                        , 0 AS does_meet_auth0_id_filter
                        , 0 AS does_meet_name_filter
                    FROM User_Id_Filter ID_FILTER
                    UNION
                    SELECT 
                            AUTH0_ID_FILTER.id_user
                        , 0 AS does_meet_id_filter
                        , 1 AS does_meet_auth0_id_filter
                        , 0 AS does_meet_name_filter
                    FROM User_Auth0_Id_Filter AUTH0_ID_FILTER
                    UNION
                    SELECT 
                            NAME_FILTER.id_user 
                        , 0 AS does_meet_id_filter
                        , 0 AS does_meet_auth0_id_filter
                        , 1 AS does_meet_name_filter
                    FROM User_Name_Filter NAME_FILTER
                ) USER_COMBINED
                GROUP BY USER_COMBINED.id_user
            )
        SELECT 
            USER.id_user
            , CASE WHEN 
                    (
                        v_has_filter_user_id = 0 
                    AND v_has_filter_user_auth0_id = 0
                )
                OR  IFNULL(USER_FILTERS.does_meet_id_filter, 0) = 1
                OR  IFNULL(USER_FILTERS.does_meet_auth0_id_filter, 0) = 1
            THEN 1 ELSE 0 END AS does_meet_id_filters
            , CASE WHEN
                    (
                        v_has_filter_user_name = 0
                )
                OR  IFNULL(USER_FILTERS.does_meet_name_filter, 0) = 1
            THEN 1 ELSE 0 END AS does_meet_non_id_filters
        FROM demo.DOG_User USER
        LEFT JOIN User_Filters USER_FILTERS ON USER.id_user = USER_FILTERS.id_user
        WHERE 
            (
                    a_get_all_user = 1
                OR  (
                        v_has_filter_user_id = 1
                    AND USER_FILTERS.does_meet_id_filter = 1
                )
                OR  (
                        v_has_filter_user_auth0_id = 1
                    AND USER_FILTERS.does_meet_auth0_id_filter = 1
                )
                OR  (
                        v_has_filter_user_id = 1
                    AND USER_FILTERS.does_meet_id_filter = 1
                )
            )
            AND (
                    a_get_inactive_user = 1
                OR  USER.active = 1
            )
        ;
    END IF;
    
    DELETE FROM tmp_Split_Id;
    DELETE FROM tmp_Split_Auth0_Id;
    DELETE FROM tmp_Split_Name;
    
    IF a_debug = 1 THEN
        SELECT 'After get all Users';
		SELECT * FROM tmp_User;
    END IF;
    
    -- Filter records
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.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_USER
        FROM tmp_User t_USER
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND t_USER.does_meet_id_filters = 0
            )
            OR  (
                    a_require_all_non_id_search_filters_met = 1
                AND t_USER.does_meet_non_id_filters = 0
            )
            OR  (
                a_require_any_id_search_filters_met = 1
                AND t_USER.does_meet_id_filters = 0
            )
            OR  (
                a_require_any_non_id_search_filters_met = 1
                AND t_USER.does_meet_non_id_filters = 0
            )
        ;
    END IF;
    
    IF a_debug = 1 THEN
        SELECT 'After filter Users';
		SELECT * FROM tmp_User;
    END IF;
    
    -- Calculated fields
    -- Can admin dog
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.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 
				v_guid -- guid
				, a_id_user -- ids_user
				, FALSE -- get_inactive_user
				, v_id_permission_dog_admin -- ids_permission
				, v_id_access_level_admin -- ids_access_level
				, 0 -- a_show_errors
				, 0 -- a_debug
			;
			SELECT * FROM demo.DOG_Calc_User_Temp;
        END IF;
        
        CALL demo.p_dog_calc_user(
			v_guid -- guid
            , a_id_user -- ids_user
            , FALSE -- get_inactive_user
            , v_id_permission_dog_admin -- ids_permission
            , v_id_access_level_admin -- ids_access_level
			, 0 -- a_show_errors
            , 0 -- a_debug
		);
        
        IF a_debug = 1 THEN
			SELECT * FROM demo.DOG_Calc_User_Temp WHERE GUID = v_guid;
        END IF;
        
        UPDATE tmp_User t_U
        INNER JOIN demo.DOG_Calc_User_Temp CUT 
			ON CUT.GUID = v_guid
            AND t_U.id_user = CUT.id_user
        SET t_U.can_admin_dog = CUT.can_admin
        ;
        
		CALL demo.p_dog_clear_calc_user( v_guid, FALSE );
	END IF;
    
    -- Can admin user
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.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 
				v_guid -- guid
				, a_id_user -- ids_user
				, FALSE -- get_inactive_user
				, v_id_permission_user_admin -- ids_permission
				, v_id_access_level_admin -- ids_access_level
				, 0 -- a_show_errors
				, 0 -- a_debug
			;
			SELECT * FROM demo.DOG_Calc_User_Temp;
        END IF;
        
        CALL demo.p_dog_calc_user(
			v_guid -- guid
            , a_id_user -- ids_user
            , FALSE -- get_inactive_user
            , v_id_permission_user_admin -- ids_permission
            , v_id_access_level_admin -- ids_access_level
			, 0 -- a_show_errors
            , 0 -- a_debug
		);
        
        IF a_debug = 1 THEN
			SELECT * FROM demo.DOG_Calc_User_Temp WHERE GUID = v_guid;
        END IF;
        
        UPDATE tmp_User t_U
        INNER JOIN demo.DOG_Calc_User_Temp CUT 
			ON CUT.GUID = v_guid
            AND t_U.id_user = CUT.id_user
        SET t_U.can_admin_user = CUT.can_admin
        ;
        
		CALL demo.p_dog_clear_calc_user( v_guid, FALSE );
	END IF;

    -- Permissions
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.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 
				v_guid -- guid
				, a_id_user -- ids_user
				, FALSE -- get_inactive_user
				, v_ids_permission_required -- ids_permission
				, v_id_access_level_view -- ids_access_level
				, 0 -- a_show_errors
				, 0 -- a_debug
			;
			SELECT * FROM demo.DOG_Calc_User_Temp;
        END IF;
        
        CALL demo.p_dog_calc_user(
			v_guid -- guid
            , a_id_user -- ids_user
            , FALSE -- get_inactive_user
            , v_ids_permission_required -- ids_permission
            , v_id_access_level_view -- ids_access_level
			, 0 -- a_show_errors
            , 0 -- a_debug
		);
        
        IF a_debug = 1 THEN
			SELECT * FROM demo.DOG_Calc_User_Temp WHERE GUID = v_guid;
        END IF;
        
        IF NOT EXISTS (
			SELECT can_view 
            FROM demo.DOG_Calc_User_Temp CUT 
            WHERE
				    CUT.GUID = v_guid
                AND can_view = 1
		) THEN
			INSERT INTO tmp_Msg_Error (
                  id_type
				, code
				, msg
			)
			VALUES (
                  v_id_type_error_bad_data
				, v_code_type_error_bad_data
				, CONCAT('You do not have view permissions for ', (SELECT name FROM demo.DOG_Permission P INNER JOIN demo.DOG_Calc_User_Temp CUT ON P.id_permission = CUT.id_permission_required WHERE GUID = v_guid AND IFNULL(can_view, 0) = 0 LIMIT 1)) 
			)
			;
		ELSE
            SET a_debug := a_debug;
        END IF;
        
		CALL demo.p_dog_clear_calc_user( v_guid, FALSE );
	END IF;
    
    
    IF EXISTS(SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.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_Dog_Calc_Dog;
        END IF;

        DELETE FROM tmp_Dog_Calc_Dog;
    END IF;
    
    -- Returns
    /* NULL record required for flask sql_alchemy to detect result set */
    IF EXISTS (SELECT * FROM tmp_Msg_Error) THEN
		DELETE FROM tmp_User;
        INSERT INTO tmp_User ( id_user )
        VALUES ( NULL );
    END IF;
    
    
	SELECT 
		  U.id_user
		, U.id_user_auth0
		, U.firstname
		, U.surname
		, U.email
		, U.is_email_verified
		, U.is_super_user
		, t_U.can_admin_dog
		, t_U.can_admin_user
        , v_is_new AS is_new
	FROM tmp_User t_U
	INNER JOIN demo.DOG_User U ON t_U.id_user = U.id_user
	;
    
    # Errors
    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 t_ERROR
    INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
    ;
    
    IF a_debug = 1 THEN
		SELECT * FROM tmp_User;
    END IF;
    
    -- Clean up
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Auth0_Id;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_User;
    
    IF a_debug = 1 THEN
		CALL demo.p_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;



/*
CALL p_dog_get_many_user (
	NULL
	, 'google-oauth2|109567376920138999933'
	, False
	, False
	-- , False
	, NULL
	, 'google-oauth2|109567376920138999933'
	, 0
);
	NULL # a_id_user
    , 'auth0|6582b95c895d09a70ba10fef' # a_id_user_auth0
    , 0 # a_get_all_user
	, 0 # a_get_inactive_user
    -- , 0 # a_get_first_user_only
	, NULL # a_ids_user
	, 'auth0|6582b95c895d09a70ba10fef' # a_ids_user_auth0
    , 0 -- a_debug
);*/
/*
select * FROM demo.DOG_Calc_User_Temp;
delete FROM demo.DOG_Calc_User_Temp;

SELECT * 
FROM demo.DOG_USER;

CALL p_dog_get_many_user(
	NULL -- :a_id_user, 
    , 'auth0|6582b95c895d09a70ba10fef' -- :a_id_user_auth0, 
    , 1 -- :a_get_all_user,
    , 0 --  :a_get_inactive_user,
    -- , 0 --  :a_get_first_user_only,
    , NULL --  :a_ids_user,
    , 'auth0|6582b95c895d09a70ba10fef' --  :a_ids_user_auth0
    , '' -- a_names_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,
    , 1 --  :a_require_any_non_id_search_filters_met,
    , 0 -- a_debug
);

*/
