 
USE demo;

DROP PROCEDURE IF EXISTS demo.p_dog_get_many_dog_command;
DROP PROCEDURE IF EXISTS demo.p_dog_get_many_dog_command_link;

DELIMITER //
CREATE PROCEDURE demo.p_dog_get_many_dog_command_link (
	  IN a_id_user INT
    , IN a_get_all_dog BIT
	, IN a_get_inactive_dog BIT
	, IN a_ids_dog TEXT
    , IN a_names_dog TEXT
    , IN a_get_all_command_category BIT
	, IN a_get_inactive_command_category BIT
	, IN a_ids_command_category TEXT
    , IN a_names_command_category TEXT
    , IN a_get_all_command BIT
	, IN a_get_inactive_command BIT
	, IN a_ids_command TEXT
    , IN a_names_command TEXT
    , IN a_debug BIT
)
BEGIN
    DECLARE v_code_type_error_bad_data VARCHAR(100);
    DECLARE v_code_type_error_no_permission VARCHAR(100);
    DECLARE v_code_type_error_warning VARCHAR(100);
    DECLARE v_id_type_error_bad_data INT;
    DECLARE v_id_type_error_no_permission INT;
    DECLARE v_id_type_error_warning INT;
    DECLARE v_has_filter_dog BIT;
    DECLARE v_guid BINARY(36);
    DECLARE v_id_permission_dog_view INT;
    DECLARE v_id_minimum INT;
    DECLARE v_time_start TIMESTAMP(6);
    DECLARE v_can_view BIT;
    
    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 VARCHAR(4000) 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_code_type_error_warning := 'WARNING';
    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_type_error_warning := (SELECT ERROR_TYPE.id_type FROM demo.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_warning LIMIT 1);
    SET v_id_permission_dog_view := (SELECT PERMISSION.id_permission FROM demo.DOG_Permission PERMISSION WHERE PERMISSION.code = 'DOG_VIEW' LIMIT 1);
    
    SET a_id_user := IFNULL(a_id_user, 0);
    -- SET a_get_all_dog := IFNULL(a_get_all_dog, 0);
    -- SET a_get_inactive_dog := IFNULL(a_get_inactive_dog, 0);
    -- SET a_ids_dog := TRIM(IFNULL(a_ids_dog, ''));
    -- SET a_get_all_command_category := IFNULL(a_get_all_command_category, 0);
    -- SET a_get_inactive_command_category := IFNULL(a_get_inactive_command_category, 0);
    -- SET a_ids_command_category := TRIM(IFNULL(a_ids_command_category, ''));
    -- SET a_get_all_command := IFNULL(a_get_all_command, 0);
    -- SET a_get_inactive_command := IFNULL(a_get_inactive_command, 0);
    -- SET a_ids_command := TRIM(IFNULL(a_ids_command, ''));
    SET a_debug := IFNULL(a_debug, 0);
    
    IF a_debug = 1 THEN
		SELECT 
			  a_id_user
            , a_get_all_dog
            , a_get_inactive_dog
            , a_ids_dog
            , a_names_dog
            , a_get_all_command_category
            , a_get_inactive_command_category
            , a_ids_command_category
            , a_names_command_category
            , a_get_all_command
            , a_get_inactive_command
            , a_ids_command
            , a_names_command
            , a_debug
		;

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

    DROP TEMPORARY TABLE IF EXISTS tmp_Split;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command_Category;
    DROP TEMPORARY TABLE IF EXISTS tmp_Dog;
    
    CREATE TEMPORARY TABLE tmp_Dog (
		  id_dog INT NOT NULL
        , name VARCHAR(250)
        , appearance VARCHAR(1000)
        , mass_kg DECIMAL(7, 3)
        , notes TEXT
        , active BIT
    );
    
    CREATE TEMPORARY TABLE tmp_Command_Category (
		  id_command_category INT NOT NULL
        , code VARCHAR(100)
        , name VARCHAR(250)
        , active BIT
    );
    
    CREATE TEMPORARY TABLE tmp_Command (
		  id_command INT NOT NULL
		, id_command_category INT NOT NULL
        , name VARCHAR(250)
        , hand_signal_default_description TEXT
        , can_have_button BIT
        , notes TEXT
        , active BIT
    );
    
	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 VARCHAR(4000) NOT NULL
	);
    

    -- Permissions
    IF a_debug = 1 THEN
		SELECT 
			  v_guid
			, a_id_user
			, FALSE -- a_get_inactive_user
			, v_id_permission_dog_view
			, 0 -- a_debug
        ;
    END IF;
    
	CALL demo.p_dog_calc_user(
          v_guid -- a_guid
        , a_id_user -- a_ids_user
        , FALSE -- a_get_inactive_user
        , v_id_permission_dog_view -- a_ids_permission
        , 0 -- a_debug
    );
	
    SELECT
          IFNULL(CALC_USER_T.has_access, 0)
    INTO
          v_can_view
    FROM demo.DOG_Calc_User_Temp CALC_USER_T
    WHERE CALC_USER_T.GUID = v_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 t_ME
        WHERE t_ME.id_type <> v_id_type_error_no_permission
        ;
		INSERT INTO tmp_Msg_Error (
			  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 Commands.'
		)
        ;
    END IF;
	
	CALL demo.p_dog_clear_calc_user(
		v_guid
        , 0 -- a_debug
	);
    
    -- Call Dog 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_dog -- a_get_all_dog
                , a_get_inactive_dog -- a_get_inactive_dog
                , a_ids_dog -- a_ids_dog
                , a_names_dog -- a_names_dog
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL demo.p_dog_calc_dog(
            v_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_dog -- a_get_all_dog
            , a_get_inactive_dog -- a_get_inactive_dog
            , a_ids_dog -- a_ids_dog
            , a_names_dog -- a_names_dog
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Dog (
              id_dog
            , name
            , appearance
            , mass_kg
            , notes
            , active
        )
        SELECT
              DOG_T.id_dog
            , DOG_T.name
            , DOG_T.appearance
            , DOG_T.mass_kg
            , DOG_T.notes
            , DOG_T.active
        FROM demo.DOG_Dog_Temp DOG_T
        WHERE DOG_T.GUID = v_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Dog;
        END IF;
    END IF;

    -- Call Command 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_command_category
                , a_get_inactive_command_category
                , a_ids_command_category
                , a_names_command_category
                , a_get_all_command -- a_get_all_command
                , a_get_inactive_command -- a_get_inactive_command
                , a_ids_command -- a_ids_command
                , a_names_command -- a_names_command
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL demo.p_dog_calc_command (
            v_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_command_category
            , a_get_inactive_command_category
            , a_ids_command_category
            , a_names_command_category
            , a_get_all_command -- a_get_all_command
            , a_get_inactive_command -- a_get_inactive_command
            , a_ids_command -- a_ids_command
            , a_names_command -- a_names_command
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Command_Category (
              id_command_category
            , code
            , name
            , active
        )
        SELECT
              COMMAND_CATEGORY_T.id_command_category
            , COMMAND_CATEGORY_T.code
            , COMMAND_CATEGORY_T.name
            , COMMAND_CATEGORY_T.active
        FROM demo.DOG_Command_Category_Temp COMMAND_CATEGORY_T
        WHERE COMMAND_CATEGORY_T.GUID = v_guid
        ;
        
        INSERT INTO tmp_Command (
              id_command
            , id_command_category
            , name
            , hand_signal_default_description
            , can_have_button
            , notes
            , active
        )
        SELECT
              COMMAND_T.id_command
            , COMMAND_T.id_command_category
            , COMMAND_T.name
            , COMMAND_T.hand_signal_default_description
            , COMMAND_T.can_have_button
            , COMMAND_T.notes
            , COMMAND_T.active
        FROM demo.DOG_Command_Temp COMMAND_T
        WHERE COMMAND_T.GUID = v_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Command_Category;
            SELECT * FROM tmp_Command;
        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_Dog;
            SELECT * FROM tmp_Command;
            SELECT * FROM tmp_Command_Category;
        END IF;

        DELETE FROM tmp_Dog;
        DELETE FROM tmp_Command;
        DELETE FROM tmp_Command_Category;
    END IF;

    -- Outputs
    -- Commands
    SELECT 
          DOG_COMMAND_LINK.id_link AS id_dog_command_link
        , t_DOG.id_dog
        , t_DOG.name AS name_dog
        , t_COMMAND.id_command_category
        , t_COMMAND_CATEGORY.name AS name_command_category
        , t_COMMAND.id_command
        , t_COMMAND.name AS name_command
    --    , t_COMMAND.has_button AS command_has_button
        , COALESCE(DOG_COMMAND_LINK.hand_signal_description, t_COMMAND.hand_signal_default_description) AS hand_signal_description_command
        , t_COMMAND.can_have_button AS can_have_button_command
        , DOG_COMMAND_LINK.notes
        , DOG_COMMAND_LINK.active
    FROM demo.DOG_Dog_Command_Link DOG_COMMAND_LINK
    INNER JOIN tmp_Dog t_DOG ON DOG_COMMAND_LINK.id_dog = t_DOG.id_dog
    INNER JOIN tmp_Command t_COMMAND ON DOG_COMMAND_LINK.id_command = t_COMMAND.id_command
    INNER JOIN tmp_Command_Category t_COMMAND_CATEGORY ON t_COMMAND.id_command_category = t_COMMAND_CATEGORY.id_command_category
	;
    
    -- 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_Dog;
        SELECT * FROM tmp_Command;
        SELECT * FROM tmp_Command_Category;
    END IF;
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Split;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command_Category;
    DROP TEMPORARY TABLE IF EXISTS tmp_Dog;
    
    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_dog_command_link (
	  1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_dog
	, 0                                     	-- a_get_inactive_dog
	, ''                                    	-- a_ids_dog
    , 1                                     	-- a_get_all_command_category
	, 0                                     	-- a_get_inactive_command_category
	, ''                                    	-- a_ids_command_category
    , 1                                     	-- a_get_all_command
	, 0                                     	-- a_get_inactive_command
	, ''                                    	-- a_ids_command
    , 1                                     	-- a_debug
);

CALL demo.p_dog_get_many_dog_command_link (
	  1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_dog
	, 0                                     	-- a_get_inactive_dog
	, ''                                    	-- a_ids_dog
	, ''                                    	-- a_names_dog
    , 1                                     	-- a_get_all_command_category
	, 0                                     	-- a_get_inactive_command_category
	, ''                                    	-- a_ids_command_category
	, ''                                    	-- a_names_command_category
    , 0                                     	-- a_get_all_command
	, 0                                     	-- a_get_inactive_command
	, '2'                                    	-- a_ids_command
	, '2'                                    	-- a_names_command
    , 1                                     	-- a_debug
);

*/