 
USE parts;

DROP PROCEDURE IF EXISTS parts.p_dog_get_many_assessment;

DELIMITER //
CREATE PROCEDURE parts.p_dog_get_many_assessment (
	  IN a_id_user INT
    , IN a_get_all_assessment BIT
	, IN a_get_inactive_assessment BIT
	, IN a_ids_assessment TEXT
    , IN a_notes_assessment TEXT
    , IN a_min_temperature_assessment DECIMAL(5, 2)
    , IN a_max_temperature_assessment DECIMAL(5, 2)
    , IN a_get_all_weather BIT
	, IN a_get_inactive_weather BIT
	, IN a_ids_weather TEXT
    , IN a_names_weather TEXT
    , IN a_get_all_lighting_level BIT
	, IN a_get_inactive_lighting_level BIT
	, IN a_ids_lighting_level TEXT
    , IN a_names_lighting_level TEXT
    , IN a_get_all_location BIT
	, IN a_get_inactive_location BIT
	, IN a_ids_location TEXT
    , IN a_names_location TEXT
    , IN a_get_all_user_handler BIT
    , IN a_get_inactive_user_handler BIT
	, IN a_ids_user_handler TEXT
    -- , IN a_auth0_ids_user_handler TEXT
    , IN a_names_user_handler TEXT
    , IN a_emails_user_handler 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_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 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 v_id_access_level_view := (SELECT ACCESS_LEVEL.id_access_level FROM parts.DOG_Access_Level ACCESS_LEVEL WHERE ACCESS_LEVEL.code = 'VIEW' LIMIT 1);
    
    SET a_id_user := IFNULL(a_id_user, 0);
    /* filters handled by Calc Stored Procedure
    */
    SET a_debug := IFNULL(a_debug, 0);
    
    IF a_debug = 1 THEN
		SELECT 
			  a_id_user
            , a_get_all_assessment
            , a_get_inactive_assessment
            , a_ids_assessment
            , a_notes_assessment
            , a_min_temperature_assessment
            , a_max_temperature_assessment
            , a_get_all_weather
            , a_get_inactive_weather
            , a_ids_weather
            , a_names_weather
            , a_get_all_lighting_level
            , a_get_inactive_lighting_level
            , a_ids_lighting_level
            , a_names_lighting_level
            , a_get_all_location
            , a_get_inactive_location
            , a_ids_location
            , a_names_location
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            -- , a_auth0_ids
            , a_names_user_handler
            , a_emails_user_handler
            , 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_id_permission_dog_view
            , v_time_start
        ;
    END IF;

    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Assessment;
    
    CREATE TEMPORARY TABLE tmp_Assessment (
		  id_assessment INT NOT NULL
		, id_weather INT
        , id_lighting_level INT
        , id_location INT
        , id_user_handler INT
        , notes TEXT
        , temperature_celcius DECIMAL(5, 2)
        -- , difficulty_level FLOAT
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );
    
	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 parts.p_dog_calc_user(
          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 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;
        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 Assessments.'
		)
        ;
    END IF;
	
	CALL parts.p_dog_clear_calc_user(
		  v_guid
        , 0 -- a_debug
	);

    
    -- Call Assessment Calc
    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 a_debug = 1 THEN
            SELECT 
                  v_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_assessment -- a_get_all_assessment
                , a_get_inactive_assessment -- a_get_inactive_assessment
                , a_ids_assessment -- a_ids_assessment
                , a_notes_assessment -- a_notes_assessment
                , a_min_temperature_assessment -- a_min_temperature_assessment
                , a_max_temperature_assessment -- a_max_temperature_assessment
                , a_get_all_weather -- a_get_all_weather
                , a_get_inactive_weather -- a_get_inactive_weather
                , a_ids_weather
                , a_names_weather
                , a_get_all_lighting_level
                , a_get_inactive_lighting_level
                , a_ids_lighting_level
                , a_names_lighting_level
                , a_get_all_location
                , a_get_inactive_location
                , a_ids_location
                , a_names_location
                , a_get_all_user_handler
                , a_get_inactive_user_handler
                , a_ids_user_handler
                -- , a_auth0_ids
                , a_names_user_handler
                , a_emails_user_handler
                , 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 parts.p_dog_calc_assessment (
              v_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_assessment -- a_get_all_assessment
            , a_get_inactive_assessment -- a_get_inactive_assessment
            , a_ids_assessment -- a_ids_assessment
            , a_notes_assessment -- a_notes_assessment
            , a_min_temperature_assessment -- a_min_temperature_assessment
            , a_max_temperature_assessment -- a_max_temperature_assessment
            , a_get_all_weather -- a_get_all_weather
            , a_get_inactive_weather -- a_get_inactive_weather
            , a_ids_weather
            , a_names_weather
            , a_get_all_lighting_level
            , a_get_inactive_lighting_level
            , a_ids_lighting_level
            , a_names_lighting_level
            , a_get_all_location
            , a_get_inactive_location
            , a_ids_location
            , a_names_location
            , a_get_all_user_handler
            , a_get_inactive_user_handler
            , a_ids_user_handler
            -- , a_auth0_ids
            , a_names_user_handler
            , a_emails_user_handler
            , 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 parts.DOG_Assessment_Temp;
            SELECT * FROM parts.DOG_Assessment_Temp;
        END IF;
        
        INSERT INTO tmp_Assessment (
              id_assessment
            , id_weather 
            , id_lighting_level
            , id_location 
            , id_user_handler
            , notes
            , temperature_celcius
            -- , difficulty_level
            , active
            , does_meet_id_filters 
            , does_meet_non_id_filters
        )
        SELECT
              ASSESSMENT_T.id_assessment
            , ASSESSMENT_T.id_weather
            , ASSESSMENT_T.id_lighting_level
            , ASSESSMENT_T.id_location
            , ASSESSMENT_T.id_user_handler
            , ASSESSMENT_T.notes
            , ASSESSMENT_T.temperature_celcius
            -- , ASSESSMENT_T.difficulty_level
            , ASSESSMENT_T.active

            , ASSESSMENT_T.does_meet_id_filters
            , ASSESSMENT_T.does_meet_non_id_filters
        FROM parts.DOG_Assessment_Temp ASSESSMENT_T
        WHERE ASSESSMENT_T.GUID = v_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT COUNT(*) FROM tmp_Assessment;
            SELECT * FROM tmp_Assessment;
        END IF;
    END IF;
    
    -- Filter outputs
    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_Assessment;
        END IF;

        DELETE FROM tmp_Assessment;
    END IF;


    -- Outputs
    -- Assessments
    SELECT 
          t_ASSESSMENT.id_assessment
        , t_ASSESSMENT.id_weather
        , WEATHER.name AS name_weather
        , t_ASSESSMENT.id_lighting_level
        , LIGHTING_LEVEL.name AS name_lighting_level
        , t_ASSESSMENT.id_location
        , LOCATIONS.name AS name_location
        , t_ASSESSMENT.id_user_handler
        , USER.firstname AS firstname_user_handler
        , USER.surname AS surname_user_handler
        , t_ASSESSMENT.notes
        , t_ASSESSMENT.temperature_celcius
        -- , t_ASSESSMENT.difficulty_level
        , t_ASSESSMENT.active

        , t_ASSESSMENT.does_meet_id_filters
        , t_ASSESSMENT.does_meet_non_id_filters
        /*
        , t_WEATHER.does_meet_id_filters_weather
        , t_WEATHER.does_meet_non_id_filters_weather
        , t_LIGHTING_LEVEL.does_meet_id_filters_lighting_level
        , t_LIGHTING_LEVEL.does_meet_non_id_filters_lighting_level
        , t_LOCATIONS.does_meet_id_filters_location
        , t_LOCATIONS.does_meet_non_id_filters_location
        , t_USER_HANDLER.does_meet_id_filters_user_handler
        , t_USER_HANDLER.does_meet_non_id_filters_user_handler
        */
    FROM tmp_Assessment t_ASSESSMENT
    INNER JOIN parts.DOG_Assessment ASSESSMENT ON t_ASSESSMENT.id_assessment = ASSESSMENT.id_assessment
    LEFT JOIN parts.DOG_Weather WEATHER ON t_ASSESSMENT.id_weather = WEATHER.id_weather
    -- LEFT JOIN tmp_Weather t_WEATHER ON t_ASSESSMENT.id_weather = t_WEATHER.id_weather
    LEFT JOIN parts.DOG_Lighting_Level LIGHTING_LEVEL ON t_ASSESSMENT.id_lighting_level = LIGHTING_LEVEL.id_lighting_level
    -- LEFT JOIN tmp_Lighting_Level t_LIGHTING_LEVEL ON t_ASSESSMENT.id_lighting_level = LIGHTING_LEVEL.id_lighting_level
    LEFT JOIN parts.DOG_Location LOCATIONS ON t_ASSESSMENT.id_location = LOCATIONS.id_location
    -- LEFT JOIN tmp_Location t_LOCATIONS ON t_ASSESSMENT.id_location = t_LOCATIONS.id_location
    LEFT JOIN parts.DOG_User USER ON t_ASSESSMENT.id_user_handler = USER.id_user
    -- LEFT JOIN tmp_User t_USER_HANDLER ON t_ASSESSMENT.id_user_handler = t_USER_HANDLER.id_user
    ORDER BY ASSESSMENT.created_on DESC
    ;

    -- 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_Assessment;
    END IF;
	
	CALL parts.p_dog_clear_calc_assessment(
		v_guid -- a_guid
        , 0 -- a_debug
	);
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Assessment;
    
    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_assessment (
	  1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_assessment
	, 0                                     	-- a_get_inactive_assessment
	, ''                                    	-- a_ids_assessment
    , ''                                        -- a_notes_assessment
	, NULL                                     	-- a_min_temperature_assessment
	, NULL                                     	-- a_max_temperature_assessment
    , 1                                     	-- a_get_all_weather
	, 0                                     	-- a_get_inactive_weather
	, ''                                    	-- a_ids_weather
    , ''                                        -- a_names_weather
    , 1                                     	-- a_get_all_lighting_level
	, 0                                     	-- a_get_inactive_lighting_level
	, ''                                    	-- a_ids_lighting_level
    , ''                                        -- a_names_lighting_level
    , 1                                     	-- a_get_all_location
	, 0                                     	-- a_get_inactive_location
	, ''                                    	-- a_ids_location
    , ''                                        -- a_names_location
    , 1                                     	-- a_get_all_user_handler
	, 0                                     	-- a_get_inactive_user_handler
	, ''                                    	-- a_ids_user_handler
    -- , IN a_auth0_ids_user_handler TEXT
    , ''                                        -- a_names_user_handler
    , ''                                        -- a_emails_user_handler
    , 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_assessment (
	  1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_assessment
	, 0                                     	-- a_get_inactive_assessment
	, ''                                    	-- a_ids_assessment
    , 'pat,point'                                        -- a_notes_assessment
	, NULL                                     	-- a_min_temperature_assessment
	, NULL                                     	-- a_max_temperature_assessment
    , 1                                     	-- a_get_all_weather
	, 0                                     	-- a_get_inactive_weather
	, ''                                    	-- a_ids_weather
    , 'pat,point'                                        -- a_names_weather
    , 1                                     	-- a_get_all_lighting_level
	, 0                                     	-- a_get_inactive_lighting_level
	, ''                                    	-- a_ids_lighting_level
    , 'pat,point'                                        -- a_names_lighting_level
    , 1                                     	-- a_get_all_location
	, 0                                     	-- a_get_inactive_location
	, ''                                    	-- a_ids_location
    , 'pat,point'                                        -- a_names_location
    , 1                                     	-- a_get_all_user_handler
	, 0                                     	-- a_get_inactive_user_handler
	, ''                                    	-- a_ids_user_handler
    -- , IN a_auth0_ids_user_handler TEXT
    , 'pat,point'                                        -- a_names_user_handler
    , 'pat,point'                                        -- a_emails_user_handler
    , 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
);

*/