 
USE fetchmetrics;

DROP PROCEDURE IF EXISTS fetchmetrics.p_dog_get_many_image;

DELIMITER //
CREATE PROCEDURE fetchmetrics.p_dog_get_many_image (
	  IN a_id_user INT
    , IN a_get_all_file_type BIT
	, IN a_get_inactive_file_type BIT
	, IN a_ids_file_type TEXT
    , IN a_names_file_type TEXT
    , 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_image BIT
	, IN a_get_inactive_image BIT
	, IN a_ids_image TEXT
    , IN a_names_image 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_output_images 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_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 (
			  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 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 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;
    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 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);
    
    SET a_id_user := IFNULL(a_id_user, 0);
    /*
    SET a_get_all_file_type := IFNULL(a_get_all_file_type, 0);
    SET a_get_inactive_file_type := IFNULL(a_get_inactive_file_type, 0);
    SET a_ids_file_type := TRIM(IFNULL(a_ids_file_type, ''));
    SET a_names_file_type := TRIM(IFNULL(a_names_file_type, ''));
    -- 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_get_all_image := IFNULL(a_get_all_image, 0);
    SET a_get_inactive_image := IFNULL(a_get_inactive_image, 0);
    SET a_ids_image := TRIM(IFNULL(a_ids_image, ''));
    SET a_names_image := TRIM(IFNULL(a_names_image, ''));
    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_output_images := IFNULL(a_output_images, 0);
    SET a_debug := IFNULL(a_debug, 0);
    
    IF a_debug = 1 THEN
		SELECT 
			  a_id_user
            , a_get_all_file_type
            , a_get_inactive_file_type
            , a_ids_file_type
            , a_names_file_type
            , a_get_all_dog
            , a_get_inactive_dog
            , a_ids_dog
            , a_names_dog
            , a_get_all_image
            , a_get_inactive_image
            , a_ids_image
            , a_names_image
            , 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_output_images
            , 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_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Image;
    
    CREATE TEMPORARY TABLE tmp_Image (
		  id_image INT NOT NULL
        , id_file_type INT
        , id_dog INT
		, path VARCHAR(1024)
        , name VARCHAR(1024)
        , 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
    );
    
    -- Permissions
    IF a_debug = 1 THEN
		SELECT 
			  v_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_access(
          v_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_Access_Temp CALC_USER_T
    WHERE CALC_USER_T.GUID = v_guid
    LIMIT 1
    ;
    
    IF a_debug = 1 THEN
        SELECT v_can_view;
        SELECT COUNT(*) AS Count_Errors FROM tmp_Msg_Error t_ERROR;
        SELECT * FROM tmp_Msg_Error t_ERROR;
    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 Images.'
		)
        ;
    END IF;
	
	CALL fetchmetrics.p_dog_clear_calc_user_access(
		  v_guid
        , 0 -- a_debug
	);

    
    -- Call Image Calc
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error 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 
                  v_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_file_type -- a_get_all_file_type
                , a_get_inactive_file_type -- a_get_inactive_file_type
                , a_ids_file_type -- a_ids_file_type
                , a_names_file_type -- a_names_file_type
                , 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
                , a_get_all_image -- a_get_all_image
                , a_get_inactive_image -- a_get_inactive_image
                , a_ids_image -- a_ids_image
                , a_names_image -- a_names_image
                , 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 fetchmetrics.p_dog_calc_image (
              v_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_file_type -- a_get_all_file_type
            , a_get_inactive_file_type -- a_get_inactive_file_type
            , a_ids_file_type -- a_ids_file_type
            , a_names_file_type -- a_names_file_type
            , 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
            , a_get_all_image -- a_get_all_image
            , a_get_inactive_image -- a_get_inactive_image
            , a_ids_image -- a_ids_image
            , a_names_image -- a_names_image
            , 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 fetchmetrics.DOG_Image_Temp;
            SELECT * FROM fetchmetrics.DOG_Image_Temp;
        END IF;
        
        INSERT INTO tmp_Image (
              id_image
            , id_file_type
            , id_dog
            , path
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              IMAGE_T.id_image
            , IMAGE_T.id_file_type
            , IMAGE_T.id_dog
            , IMAGE_T.path
            , IMAGE_T.name
            , IMAGE_T.active

            , IMAGE_T.does_meet_id_filters
            , IMAGE_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Image_Temp IMAGE_T
        WHERE IMAGE_T.GUID = v_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT COUNT(*) FROM tmp_Image;
            SELECT * FROM tmp_Image;
        END IF;
    END IF;
    
    -- Filter outputs
    IF EXISTS(SELECT * FROM tmp_Msg_Error 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_Image;
        END IF;

        DELETE FROM tmp_Image;
    END IF;


    -- Outputs
    -- Images
    IF a_output_images = 1 THEN
        SELECT 
              t_IMAGES.id_image
            , t_IMAGES.id_file_type
            , FILE_TYPE.name AS name_file_type
            , t_IMAGES.id_dog
            , DOG.name AS name_dog
            , t_IMAGES.path
            , t_IMAGES.name
            , t_IMAGES.active

            , t_IMAGES.does_meet_id_filters
            , t_IMAGES.does_meet_non_id_filters
        FROM tmp_Image t_IMAGES
        LEFT JOIN fetchmetrics.DOG_Image IMAGES ON t_IMAGES.id_image = IMAGES.id_image
        LEFT JOIN fetchmetrics.CORE_File_Type FILE_TYPE ON IMAGES.id_file_type = FILE_TYPE.id_file_type
        LEFT JOIN fetchmetrics.DOG_Dog DOG ON IMAGES.id_dog = DOG.id_dog
        ORDER BY t_IMAGES.name
        ;
    END IF;

    -- 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 fetchmetrics.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_Image;
    END IF;
	
	CALL fetchmetrics.p_dog_clear_calc_image(
		v_guid -- a_guid
        , 0 -- a_debug
	);
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Image;
    
    IF a_debug = 1 THEN
		CALL fetchmetrics.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*

CALL fetchmetrics.p_dog_get_many_image (
	  1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_file_type
	, 0                                     	-- a_get_inactive_file_type
	, ''                                    	-- a_ids_file_type
    , ''                                        -- a_names_file_type
    , 1                                     	-- a_get_all_dog
	, 0                                     	-- a_get_inactive_dog
	, ''                                    	-- a_ids_dog
	, ''                                    	-- a_names_dog
    , 1                                     	-- a_get_all_image
	, 0                                     	-- a_get_inactive_image
	, ''                                    	-- a_ids_image
    , ''                                        -- a_names_image
    , 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_output_images
    , 1                                     	-- a_debug
);


CALL fetchmetrics.p_dog_get_many_image (
	  1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_file_type
	, 0                                     	-- a_get_inactive_file_type
	, ''                                    	-- a_ids_file_type
    , 'pat,point'                               -- a_names_file_type
    , 1                                     	-- a_get_all_dog
	, 0                                     	-- a_get_inactive_dog
	, ''                                    	-- a_ids_dog
	, 'pat,point'                              	-- a_names_dog
    , 1                                     	-- a_get_all_image
	, 0                                     	-- a_get_inactive_image
	, ''                                    	-- a_ids_image
    , 'pat,point'                               -- a_names_image
    , 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_output_images
    , 1                                     	-- a_debug
);

*/