 
USE parts;

DROP PROCEDURE IF EXISTS parts.p_DOG_get_many_dog;

DELIMITER //
CREATE PROCEDURE parts.p_DOG_get_many_dog (
	  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_debug BIT
)
BEGIN
    DECLARE v_code_type_error_bad_data VARCHAR(100);
    DECLARE v_code_type_error_no_permission VARCHAR(100);
    DECLARE v_id_type_error_bad_data INT;
    DECLARE v_id_type_error_no_permission INT;
    DECLARE v_has_filter_dog_id BIT;
    DECLARE v_has_filter_dog_name 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 parts.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 parts.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 parts.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 parts.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 parts.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_names_dog := TRIM(IFNULL(a_names_dog, ''));
    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_debug
		;

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

    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Dog;
    
    CREATE TEMPORARY TABLE tmp_Dog (
		  id_dog INT NOT NULL
        , name VARCHAR(250)
    );
    
	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
	);
    
    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_Name (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT NULL
	);
    DELETE FROM tmp_Split_Name;
    
    CALL parts.p_core_validate_guid ( v_guid );
    
    SET v_has_filter_dog_id = CASE WHEN a_ids_dog = '' THEN 0 ELSE 1 END;
    SET v_has_filter_dog_name = CASE WHEN a_names_dog = '' THEN 0 ELSE 1 END;

    -- Dogs
    IF v_has_filter_dog_id = 1 THEN
		CALL parts.p_split(v_guid, a_ids_dog, ',', a_debug);
		
		INSERT INTO tmp_Split_Id (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CONVERT(SPLIT_T.substring, DECIMAL(10,0)) AS as_int
		FROM parts.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = v_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL parts.p_clear_split_temp( v_guid );
	END IF;

    IF v_has_filter_dog_name = 1 THEN
		CALL parts.p_split(v_guid, a_names_dog, ',', a_debug);
		
		INSERT INTO tmp_Split_Name (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CONVERT(SPLIT_T.substring, DECIMAL(10,0)) AS as_int
		FROM parts.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = v_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL parts.p_clear_split_temp( v_guid );
	END IF;
    
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN parts.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 t_SPLIT_ID
            LEFT JOIN parts.DOG_Dog DOG ON t_SPLIT_ID.as_int = DOG.id_dog 
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(DOG.id_dog)
				OR  (
					DOG.active = 0
					AND a_get_inactive_dog = 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 Dog IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_ID.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Id t_SPLIT_ID
			LEFT JOIN parts.DOG_Dog DOG ON t_SPLIT_ID.as_int = DOG.id_dog
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(DOG.id_dog)
				OR  (
					DOG.active = 0
					AND a_get_inactive_dog = 0
				)
			;
        /* Don't error on names not found
		ELSEIF EXISTS (
			SELECT * 
            FROM tmp_Split t_SPLIT 
            LEFT JOIN parts.DOG_Dog DOG ON t_SPLIT.as_int = DOG.id_dog 
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
                OR  ISNULL(DOG.id_dog)
				OR  (
					DOG.active = 0
					AND a_get_inactive_dog = 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 Dog IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split t_SPLIT
			LEFT JOIN parts.DOG_Dog DOG ON t_SPLIT.as_int = DOG.id_dog
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
				OR  ISNULL(DOG.id_dog)
				OR  (
					DOG.active = 0
					AND a_get_inactive_dog = 0
				)
			;
        */
		ELSE
			INSERT INTO tmp_Dog (
				  id_dog
                , name
			)
			SELECT 
				  DOG.id_dog
                , DOG.name
			FROM parts.DOG_Dog DOG 
            LEFT JOIN tmp_Split_Id t_SPLIT_ID ON DOG.id_dog = t_SPLIT_ID.as_int
            LEFT JOIN tmp_Split_Name t_SPLIT_NAME ON DOG.name = t_SPLIT_NAME.substring
			WHERE 
                (
					    a_get_all_dog = 1
					OR  (
                            v_has_filter_dog_id = 1
                        OR  v_has_filter_dog_name = 1
					)
				)
				AND (
					    a_get_inactive_dog = 1
					OR  DOG.active = 1
				)
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split_Id;
    DELETE FROM tmp_Split_Name;
    

    -- 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 parts.p_DOG_calc_user(
          v_guid
        , a_id_user
        , FALSE -- a_get_inactive_user
        , v_id_permission_dog_view
        , 0 -- a_debug
    );
	
    SELECT
          IFNULL(CALC_USER_T.has_access, 0)
    INTO
          v_can_view
    FROM parts.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 Commands.'
		)
        ;
    END IF;
	
	CALL parts.p_DOG_clear_calc_user(
		v_guid
        , 0 -- a_debug
	);
    
    IF EXISTS(SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN parts.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;
        END IF;

        DELETE FROM tmp_Dog;
    END IF;

    -- Outputs
    -- Commands
    SELECT 
          t_DOG.id_dog
        , t_DOG.name
        , DOG.appearance
        , DOG.mass_kg
        , DOG.notes
        , DOG.active
    FROM parts.DOG_Dog DOG
    INNER JOIN tmp_Dog t_DOG ON DOG.id_dog = t_DOG.id_dog
	;
    
    -- 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 parts.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;
    END IF;
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Dog;
    
    IF a_debug = 1 THEN
		CALL parts.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*

CALL parts.p_DOG_get_many_dog (
	  1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_dog
	, 0                                     	-- a_get_inactive_dog
	, ''                                    	-- a_ids_dog
    , ''                                        -- a_names_dog
    , 1                                     	-- a_debug
);

*/