 
USE demo;

DROP PROCEDURE IF EXISTS demo.p_dog_get_many_obedience_level;

DELIMITER //
CREATE PROCEDURE demo.p_dog_get_many_obedience_level (
	  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_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_guid BINARY(36);
    DECLARE v_id_access_level_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 (
			  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 (
			  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_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 demo.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 demo.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_no_permission LIMIT 1);
    SET v_id_access_level_view := (SELECT ACCESS_LEVEL.id_access_level FROM demo.DOG_Access_Level ACCESS_LEVEL WHERE ACCESS_LEVEL.code = 'VIEW' LIMIT 1);
    
    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_debug := IFNULL(a_debug, 0);
    
    IF a_debug = 1 THEN
		SELECT 
			  a_id_user
            , a_get_all_obedience_level
            , a_get_inactive_obedience_level
            , a_ids_obedience_level
            , a_names_obedience_level
            , 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
		;

        SELECT
              v_id_type_error_bad_data
            , v_id_type_error_no_permission
            , v_guid
            , v_time_start
        ;
    END IF;

    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Obedience_Level;
    
    CREATE TEMPORARY TABLE tmp_Obedience_Level (
		  id_obedience_level INT NOT NULL
		, code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT

        , does_meet_id_filters BIT
        , does_meet_non_id_filters BIT
    );
    
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error (
          id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
		, id_type INT
		, code VARCHAR(250) NOT NULL
        , msg TEXT NOT NULL
    );
    
    
    -- Call Obedience_Level Calc
    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 -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_obedience_level -- a_get_all_obedience_level
                , a_get_inactive_obedience_level -- a_get_inactive_obedience_level
                , a_ids_obedience_level -- a_ids_obedience_level
                , a_names_obedience_level -- a_names_obedience_level
                , 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_all_id_search_filters_met
                , a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL demo.p_dog_calc_obedience_level (
              v_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_obedience_level -- a_get_all_obedience_level
            , a_get_inactive_obedience_level -- a_get_inactive_obedience_level
            , a_ids_obedience_level -- a_ids_obedience_level
            , a_names_obedience_level -- a_names_obedience_level
            , 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_all_id_search_filters_met
            , a_require_any_id_search_filters_met -- a_require_any_id_search_filters_met
            , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
            , a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        IF a_debug = 1 THEN
            SELECT COUNT(*) FROM demo.DOG_Obedience_Level_Temp;
            SELECT * FROM demo.DOG_Obedience_Level_Temp;
        END IF;
        
        INSERT INTO tmp_Obedience_Level (
              id_obedience_level
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              OBEDIENCE_LEVEL_T.id_obedience_level
            , OBEDIENCE_LEVEL_T.code
            , OBEDIENCE_LEVEL_T.name
            , OBEDIENCE_LEVEL_T.active

            , OBEDIENCE_LEVEL_T.does_meet_id_filters
            , OBEDIENCE_LEVEL_T.does_meet_non_id_filters
        FROM demo.DOG_Obedience_Level_Temp OBEDIENCE_LEVEL_T
        WHERE OBEDIENCE_LEVEL_T.GUID = v_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT COUNT(*) FROM tmp_Obedience_Level;
            SELECT * FROM tmp_Obedience_Level;
        END IF;
    END IF;
    
    -- Filter outputs
    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_Obedience_Level;
        END IF;

        DELETE FROM tmp_Obedience_Level;
    END IF;


    -- Outputs
    -- Obedience_Levels
    SELECT 
          t_OBEDIENCE_LEVELS.id_obedience_level
        , t_OBEDIENCE_LEVELS.code
        , t_OBEDIENCE_LEVELS.name
        , t_OBEDIENCE_LEVELS.active

        , t_OBEDIENCE_LEVELS.does_meet_id_filters
        , t_OBEDIENCE_LEVELS.does_meet_non_id_filters
    FROM tmp_Obedience_Level t_OBEDIENCE_LEVELS
    LEFT JOIN demo.DOG_Obedience_Level OBEDIENCE_LEVELS ON t_OBEDIENCE_LEVELS.id_obedience_level = OBEDIENCE_LEVELS.id_obedience_level
    ORDER BY t_OBEDIENCE_LEVELS.name
    ;

    -- 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 AND v_can_view = 1 THEN
        SELECT * FROM tmp_Obedience_Level;
    END IF;
	
	CALL demo.p_dog_clear_calc_obedience_level(
		v_guid -- a_guid
        , 0 -- a_debug
	);
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Obedience_Level;
    
    IF a_debug = 1 THEN
		CALL demo.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*

CALL demo.p_dog_get_many_obedience_level (
	  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
    , 1                                         -- a_require_all_id_search_filters_met
    , 1                                         -- a_require_any_id_search_filters_met
    , 0                                         -- a_require_all_non_id_search_filters_met
    , 1                                         -- a_require_any_non_id_search_filters_met
    , 1                                     	-- a_debug
);


CALL demo.p_dog_get_many_obedience_level (
	  1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_obedience_level
	, 0                                     	-- a_get_inactive_obedience_level
	, ''                                    	-- a_ids_obedience_level
    , 'pat,point'                               -- a_names_obedience_level
    , 1                                     	-- a_get_all_user
	, 0                                     	-- a_get_inactive_user
	, ''                                    	-- a_ids_user
    , 'pat,point'                                        -- a_names_user
    , 'pat,point'                                        -- 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
    , 1                                         -- a_require_any_non_id_search_filters_met
    , 1                                     	-- a_debug
);

*/