 
USE fetchmetrics;

DROP PROCEDURE IF EXISTS fetchmetrics.p_dog_calc_assessment;

DELIMITER //
CREATE PROCEDURE fetchmetrics.p_dog_calc_assessment (
      IN a_guid BINARY(36)
    , 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_show_errors 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_has_filter_assessment_id BIT;
    DECLARE v_has_filter_assessment_notes BIT;
    DECLARE v_has_filter_assessment_temperature_max BIT;
    DECLARE v_has_filter_assessment_temperature_min BIT;
    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_Calc_Assessment (
			  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_Calc_Assessment (
			  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_Calc_Assessment 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_Calc_Assessment;
    END;

    SET v_time_start := CURRENT_TIMESTAMP(6);
    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);
    
    
    CALL fetchmetrics.p_core_validate_guid ( a_guid );

    SET a_id_user := IFNULL(a_id_user, 0);
    SET a_get_all_assessment := IFNULL(a_get_all_assessment, 0);
    SET a_get_inactive_assessment := IFNULL(a_get_inactive_assessment, 0);
    SET a_ids_assessment := TRIM(IFNULL(a_ids_assessment, ''));
    SET a_notes_assessment := TRIM(IFNULL(a_notes_assessment, ''));
    -- a_min_temperature_assessment
    -- SET a_max_temperature_assessment := 
    -- Foreign key filters corected in their Calc Stored Procedures
    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_show_errors := IFNULL(a_show_errors, 0);
    SET a_debug := IFNULL(a_debug, 0);
    
    IF a_debug = 1 THEN
		SELECT 
              a_guid
			, 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_show_errors
            , a_debug
		;

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

    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Notes_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Assessment_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_User_Handler_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Location_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Lighting_Level_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Weather_Calc_Assessment;

    CREATE TEMPORARY TABLE tmp_Weather_Calc_Assessment (
		  id_weather INT NOT NULL
        , code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );

    CREATE TEMPORARY TABLE tmp_Lighting_Level_Calc_Assessment (
		  id_lighting_level INT NOT NULL
        , code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );

    CREATE TEMPORARY TABLE tmp_Location_Calc_Assessment (
		  id_location INT NOT NULL
        , id_location_parent INT
        , code VARCHAR(250)
        , name VARCHAR(250)
        , active BIT
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );
    
    CREATE TEMPORARY TABLE tmp_User_Handler_Calc_Assessment (
		  id_user INT NOT NULL
		, id_permission_required INT NOT NULL
		, priority_access_level_required INT NOT NULL
        , is_super_user BIT
		, priority_access_level_user INT
		, has_access BIT
		, can_view BIT
		, can_edit BIT
		, can_admin BIT

        , does_meet_id_filters BIT
        , does_meet_non_id_filters BIT
    );
    
    CREATE TEMPORARY TABLE tmp_Assessment_Calc_Assessment (
		  id_assessment INT NOT NULL
		, id_weather INT
        , id_lighting_level INT
        , id_location INT
        , id_user_handler INT
        , notes TEXT
        , active BIT
        , temperature_celcius DECIMAL(5, 2)
        , does_meet_id_filters BIT NOT NULL
        , does_meet_non_id_filters BIT NOT NULL
    );
    
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error_Calc_Assessment (
          id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
		, id_type INT
		, code VARCHAR(250) NOT NULL
        , msg TEXT NOT NULL
	);
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Id_Calc_Assessment (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Id_Calc_Assessment;
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split_Notes_Calc_Assessment (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT
	);
    DELETE FROM tmp_Split_Notes_Calc_Assessment;
    
    SET v_has_filter_assessment_id := CASE WHEN a_ids_assessment <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_assessment_notes := CASE WHEN a_notes_assessment <> '' THEN 1 ELSE 0 END;
    SET v_has_filter_assessment_temperature_min := NOT ISNULL(a_min_temperature_assessment);
    SET v_has_filter_assessment_temperature_max := NOT ISNULL(a_max_temperature_assessment);

    -- Call Calc Weather
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Assessment 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 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_weather -- a_get_all_weather
                , a_get_inactive_weather -- a_get_inactive_weather
                , a_ids_weather -- a_ids_weather
                , a_names_weather -- a_names_weather
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_weather (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_weather -- a_get_all_weather
            , a_get_inactive_weather -- a_get_inactive_weather
            , a_ids_weather -- a_ids_weather
            , a_names_weather -- a_names_weather
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- 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
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Weather_Calc_Assessment (
              id_weather
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              WEATHER_T.id_weather
            , WEATHER_T.code
            , WEATHER_T.name
            , WEATHER_T.active

            , WEATHER_T.does_meet_id_filters
            , WEATHER_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Weather_Temp WEATHER_T
        WHERE WEATHER_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Weather_Calc_Assessment;
        END IF;
    END IF;

    -- Call Calc Lighting_Level
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Assessment 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 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_lighting_level -- a_get_all_lighting_level
                , a_get_inactive_lighting_level -- a_get_inactive_lighting_level
                , a_ids_lighting_level -- a_ids_lighting_level
                , a_names_lighting_level -- a_names_lighting_level
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_lighting_level (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_lighting_level -- a_get_all_lighting_level
            , a_get_inactive_lighting_level -- a_get_inactive_lighting_level
            , a_ids_lighting_level -- a_ids_lighting_level
            , a_names_lighting_level -- a_names_lighting_level
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- 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
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Lighting_Level_Calc_Assessment (
              id_lighting_level
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              LIGHTING_LEVEL_T.id_lighting_level
            , LIGHTING_LEVEL_T.code
            , LIGHTING_LEVEL_T.name
            , LIGHTING_LEVEL_T.active

            , LIGHTING_LEVEL_T.does_meet_id_filters
            , LIGHTING_LEVEL_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Lighting_Level_Temp LIGHTING_LEVEL_T
        WHERE LIGHTING_LEVEL_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Lighting_Level_Calc_Assessment;
        END IF;
    END IF;

    -- Call Calc Location
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Assessment 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 
                  a_guid -- a_guid
                , a_id_user -- a_id_user
                , a_get_all_location -- a_get_all_location
                , a_get_inactive_location -- a_get_inactive_location
                , a_ids_location -- a_ids_location
                , a_names_location -- a_names_location
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_location (
              a_guid -- a_guid
            , a_id_user -- a_id_user
            , a_get_all_location -- a_get_all_location
            , a_get_inactive_location -- a_get_inactive_location
            , a_ids_location -- a_ids_location
            , a_names_location -- a_names_location
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- 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
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_Location_Calc_Assessment (
              id_location
            , code
            , name
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              LOCATION_T.id_location
            , LOCATION_T.code
            , LOCATION_T.name
            , LOCATION_T.active

            , LOCATION_T.does_meet_id_filters
            , LOCATION_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Location_Temp LOCATION_T
        WHERE LOCATION_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Location_Calc_Assessment;
        END IF;
    END IF;

    -- Call Calc User
    IF NOT EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Assessment 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 
                  a_guid -- a_guid
                , a_get_all_user_handler -- a_get_all_user
                , a_get_inactive_user_handler -- a_get_inactive_user
                , a_ids_user_handler -- a_ids_user
                , NULL -- a_auth0_ids
                , a_names_user_handler -- a_names_user
                , a_emails_user_handler -- a_emails_user
                , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
                , 0 -- a_require_any_id_search_filters_met
                , a_require_all_non_id_search_filters_met -- a_require_all_non_id_search_filters_met
                , 0 -- a_require_any_non_id_search_filters_met
                , v_id_permission_dog_view -- a_ids_permission_required
                , v_id_access_level_view -- a_ids_access_level_required
                , 0 -- a_show_errors
                , 0 -- a_debug
            ;
        END IF;
        
        CALL fetchmetrics.p_dog_calc_user_access (
              a_guid -- a_guid
            , a_get_all_user_handler -- a_get_all_user
            , a_get_inactive_user_handler -- a_get_inactive_user
            , a_ids_user_handler -- a_ids_user
            , NULL -- a_auth0_ids
            , a_names_user_handler -- a_names_user
            , a_emails_user_handler -- a_emails_user
            , a_require_all_id_search_filters_met -- a_require_all_id_search_filters_met
            , 0 -- 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
            , 0 -- a_require_any_non_id_search_filters_met -- a_require_any_non_id_search_filters_met
            , v_id_permission_dog_view -- a_ids_permission_required
            , v_id_access_level_view -- a_ids_access_level_required
            , 0 -- a_show_errors
            , 0 -- a_debug
        );
        
        INSERT INTO tmp_User_Handler_Calc_Assessment (
              id_user
            , id_permission_required
            , priority_access_level_required
            , is_super_user
            , priority_access_level_user
            , has_access
            , can_view
            , can_edit
            , can_admin

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT
              CALC_USER_T.id_user
            , CALC_USER_T.id_permission_required
            , CALC_USER_T.priority_access_level_required
            , CALC_USER_T.is_super_user
            , CALC_USER_T.priority_access_level_user
            , CALC_USER_T.has_access
            , CALC_USER_T.can_view
            , CALC_USER_T.can_edit
            , CALC_USER_T.can_admin

            , CALC_USER_T.does_meet_id_filters
            , CALC_USER_T.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Calc_User_Access_Temp CALC_USER_T
        WHERE CALC_USER_T.GUID = a_guid
        ;
        
        IF a_debug = 1 THEN
            SELECT * FROM tmp_User_Handler_Calc_Assessment;
        END IF;
    END IF;

    -- Assessments
    IF v_has_filter_assessment_id = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_ids_assessment, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Id_Calc_Assessment (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CAST(SPLIT_T.substring AS DECIMAL(10,0)) AS as_int
		FROM fetchmetrics.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = a_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL fetchmetrics.p_core_clear_split( a_guid );
	END IF;

    IF v_has_filter_assessment_notes = 1 THEN
		CALL fetchmetrics.p_core_split(a_guid, a_notes_assessment, ',', a_debug);

        SET sql_mode = '';
		
		INSERT INTO tmp_Split_Notes_Calc_Assessment (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CAST(SPLIT_T.substring AS DECIMAL(10,0)) AS as_int
		FROM fetchmetrics.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = a_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL fetchmetrics.p_core_clear_split( a_guid );
	END IF;
    
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Assessment 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 EXISTS (
			SELECT * 
            FROM tmp_Split_Id_Calc_Assessment t_SPLIT_ID
            LEFT JOIN fetchmetrics.DOG_Assessment ASSESSMENT ON t_SPLIT_ID.as_int = ASSESSMENT.id_assessment 
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
                OR  ISNULL(ASSESSMENT.id_assessment)
				OR  (
					ASSESSMENT.active = 0
					AND a_get_inactive_assessment = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error_Calc_Assessment (
				  id_type
		        , code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Assessment IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT_ID.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split_Id_Calc_Assessment t_SPLIT_ID
			LEFT JOIN fetchmetrics.DOG_Assessment ASSESSMENT ON t_SPLIT_ID.as_int = ASSESSMENT.id_assessment
			WHERE 
				    ISNULL(t_SPLIT_ID.as_int) 
				OR  ISNULL(ASSESSMENT.id_assessment)
				OR  (
					ASSESSMENT.active = 0
					AND a_get_inactive_assessment = 0
				)
			;
        /* Don't error on names, or notes not found
		ELSEIF EXISTS ()
        */
		ELSE
            IF a_debug = 1 THEN
                SELECT 'Assessment Filters';
                WITH 
                    Assessment_Id_Filter AS (
                        SELECT ASSESSMENT.id_assessment
                        FROM tmp_Split_Id_Calc_Assessment t_SPLIT_ID
                        INNER JOIN fetchmetrics.DOG_Assessment ASSESSMENT ON t_SPLIT_ID.as_int = ASSESSMENT.id_assessment
                    )
                    , Assessment_Notes_Filter AS (
                        SELECT ASSESSMENT.id_assessment
                        FROM tmp_Split_Notes_Calc_Assessment t_SPLIT_NOTES
                        INNER JOIN fetchmetrics.DOG_Assessment ASSESSMENT ON ASSESSMENT.notes LIKE CONCAT('%', t_SPLIT_NOTES.substring, '%')
                        WHERE 
                                t_SPLIT_NOTES.substring IS NOT NULL
                            AND t_SPLIT_NOTES.substring <> ''
                    )
                    , Assessment_Filters AS (
                        SELECT 
                            ASSESSMENT_COMBINED.id_assessment
                            , MAX(ASSESSMENT_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                            , MAX(ASSESSMENT_COMBINED.does_meet_notes_filter) AS does_meet_notes_filter
                        FROM (
                            SELECT 
                                ASSESSMENT_ID_FILTER.id_assessment 
                                , 1 AS does_meet_id_filter
                                , 0 AS does_meet_notes_filter
                            FROM Assessment_Id_Filter ASSESSMENT_ID_FILTER
                            UNION
                            SELECT 
                                ASSESSMENT_NOTES_FILTER.id_assessment 
                                , 0 AS does_meet_id_filter
                                , 1 AS does_meet_notes_filter
                            FROM Assessment_Notes_Filter ASSESSMENT_NOTES_FILTER
                        ) ASSESSMENT_COMBINED
                        GROUP BY ASSESSMENT_COMBINED.id_assessment
                    )
                SELECT 
                      ASSESSMENT.id_assessment
                    , ASSESSMENT.id_weather
                    , ASSESSMENT.id_lighting_level
                    , ASSESSMENT.id_location
                    , ASSESSMENT.id_user_handler
                    , ASSESSMENT.notes
                    , ASSESSMENT.temperature_celcius
                    , ASSESSMENT.active
                    , CASE WHEN 
                            v_has_filter_assessment_id = 0 
                        OR  ASSESSMENT_FILTERS.does_meet_id_filter = 1
                    THEN 1 ELSE 0 END AS does_meet_id_filters
                    , CASE WHEN 
                            (
                                v_has_filter_assessment_notes = 0
                            AND v_has_filter_assessment_temperature_min = 0
                            AND v_has_filter_assessment_temperature_max = 0
                        )
                        OR  ASSESSMENT_FILTERS.does_meet_notes_filter = 1
                        OR  (
                                v_has_filter_assessment_temperature_min = 0
                            OR  (
                                    v_has_filter_assessment_temperature_min = 1
                                AND ASSESSMENT.temperature_celcius >= v_has_filter_assessment_temperature_min
                            )
                        )
                        OR  (
                                v_has_filter_assessment_temperature_max = 0
                            OR  (
                                    v_has_filter_assessment_temperature_max = 1
                                AND ASSESSMENT.temperature_celcius <= v_has_filter_assessment_temperature_max
                            )
                        )
                    THEN 1 ELSE 0 END AS does_meet_non_id_filters
                FROM fetchmetrics.DOG_Assessment ASSESSMENT
                LEFT JOIN Assessment_Filters ASSESSMENT_FILTERS ON ASSESSMENT.id_assessment = ASSESSMENT_FILTERS.id_assessment
                WHERE 
                    (
                            a_get_all_assessment = 1
                        OR  (
                                v_has_filter_assessment_id = 1
                            AND ASSESSMENT_FILTERS.does_meet_id_filter = 1
                        )
                        OR  (
                                v_has_filter_assessment_notes = 1
                            AND ASSESSMENT_FILTERS.does_meet_notes_filter = 1
                        )
                        OR  (
                                v_has_filter_assessment_temperature_min = 1
                            AND ASSESSMENT.temperature_celcius >= v_has_filter_assessment_temperature_min
                        )
                        OR  (
                                v_has_filter_assessment_temperature_max = 1
                            AND ASSESSMENT.temperature_celcius <= v_has_filter_assessment_temperature_max
                        )
                    )
                    AND (
                            a_get_inactive_assessment = 1
                        OR  ASSESSMENT.active = 1
                    )
                ;
            END IF;
			INSERT INTO tmp_Assessment_Calc_Assessment (
				  id_assessment
                , id_weather
                , id_lighting_level
                , id_location
                , id_user_handler
                , notes
                , temperature_celcius
                , active
                , does_meet_id_filters
                , does_meet_non_id_filters
			)
            WITH 
                Assessment_Id_Filter AS (
                    SELECT ASSESSMENT.id_assessment
                    FROM tmp_Split_Id_Calc_Assessment t_SPLIT_ID
                    INNER JOIN fetchmetrics.DOG_Assessment ASSESSMENT ON t_SPLIT_ID.as_int = ASSESSMENT.id_assessment
                )
                , Assessment_Notes_Filter AS (
                    SELECT ASSESSMENT.id_assessment
                    FROM tmp_Split_Notes_Calc_Assessment t_SPLIT_NOTES
                    INNER JOIN fetchmetrics.DOG_Assessment ASSESSMENT ON ASSESSMENT.notes LIKE CONCAT('%', t_SPLIT_NOTES.substring, '%')
                    WHERE 
                            t_SPLIT_NOTES.substring IS NOT NULL
                        AND t_SPLIT_NOTES.substring <> ''
                )
                , Assessment_Filters AS (
                    SELECT 
                        ASSESSMENT_COMBINED.id_assessment
                        , MAX(ASSESSMENT_COMBINED.does_meet_id_filter) AS does_meet_id_filter
                        , MAX(ASSESSMENT_COMBINED.does_meet_notes_filter) AS does_meet_notes_filter
                    FROM (
                        SELECT 
                            ASSESSMENT_ID_FILTER.id_assessment 
                            , 1 AS does_meet_id_filter
                            , 0 AS does_meet_notes_filter
                        FROM Assessment_Id_Filter ASSESSMENT_ID_FILTER
                        UNION
                        SELECT 
                            ASSESSMENT_NOTES_FILTER.id_assessment 
                            , 0 AS does_meet_id_filter
                            , 1 AS does_meet_notes_filter
                        FROM Assessment_Notes_Filter ASSESSMENT_NOTES_FILTER
                    ) ASSESSMENT_COMBINED
                    GROUP BY ASSESSMENT_COMBINED.id_assessment
                )
            SELECT 
                  ASSESSMENT.id_assessment
                , ASSESSMENT.id_weather
                , ASSESSMENT.id_lighting_level
                , ASSESSMENT.id_location
                , ASSESSMENT.id_user_handler
                , ASSESSMENT.notes
                , ASSESSMENT.temperature_celcius
                , ASSESSMENT.active
                , CASE WHEN 
                        v_has_filter_assessment_id = 0 
                    OR  ASSESSMENT_FILTERS.does_meet_id_filter = 1
                THEN 1 ELSE 0 END AS does_meet_id_filters
                , CASE WHEN 
                        (
                            v_has_filter_assessment_notes = 0
                        AND v_has_filter_assessment_temperature_min = 0
                        AND v_has_filter_assessment_temperature_max = 0
                    )
                    OR  ASSESSMENT_FILTERS.does_meet_notes_filter = 1
                    OR  (
                            v_has_filter_assessment_temperature_min = 0
                        OR  (
                                v_has_filter_assessment_temperature_min = 1
                            AND ASSESSMENT.temperature_celcius >= v_has_filter_assessment_temperature_min
                        )
                    )
                    OR  (
                            v_has_filter_assessment_temperature_max = 0
                        OR  (
                                v_has_filter_assessment_temperature_max = 1
                            AND ASSESSMENT.temperature_celcius <= v_has_filter_assessment_temperature_max
                        )
                    )
                THEN 1 ELSE 0 END AS does_meet_non_id_filters
            FROM fetchmetrics.DOG_Assessment ASSESSMENT
            LEFT JOIN Assessment_Filters ASSESSMENT_FILTERS ON ASSESSMENT.id_assessment = ASSESSMENT_FILTERS.id_assessment
            WHERE 
                (
                        a_get_all_assessment = 1
                    OR  (
                            v_has_filter_assessment_id = 1
                        AND ASSESSMENT_FILTERS.does_meet_id_filter = 1
                    )
                    OR  (
                            v_has_filter_assessment_notes = 1
                        AND ASSESSMENT_FILTERS.does_meet_notes_filter = 1
                    )
                    OR  (
                            v_has_filter_assessment_temperature_min = 1
                        AND ASSESSMENT.temperature_celcius >= v_has_filter_assessment_temperature_min
                    )
                    OR  (
                            v_has_filter_assessment_temperature_max = 1
                        AND ASSESSMENT.temperature_celcius <= v_has_filter_assessment_temperature_max
                    )
                )
                AND (
                        a_get_inactive_assessment = 1
                    OR  ASSESSMENT.active = 1
                )
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split_Id_Calc_Assessment;
    DELETE FROM tmp_Split_Notes_Calc_Assessment;

    IF a_debug = 1 THEN
        SELECT 'After get Assessments';
        SELECT * FROM tmp_Assessment_Calc_Assessment;
    END IF;

    -- Filter records
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error_Calc_Assessment 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
        DELETE t_ASSESSMENT
        FROM tmp_Assessment_Calc_Assessment t_ASSESSMENT
        LEFT JOIN tmp_Weather_Calc_Assessment t_WEATHER ON t_ASSESSMENT.id_weather = t_WEATHER.id_weather
        LEFT JOIN tmp_Lighting_Level_Calc_Assessment t_LIGHTING_LEVEL ON t_ASSESSMENT.id_lighting_level = t_LIGHTING_LEVEL.id_lighting_level
        LEFT JOIN tmp_Location_Calc_Assessment t_LOCATION ON t_ASSESSMENT.id_location = t_LOCATION.id_location
        LEFT JOIN tmp_User_Handler_Calc_Assessment t_USER_HANDLER ON t_ASSESSMENT.id_user_handler = t_USER_HANDLER.id_user
        WHERE
                (
                    a_require_all_id_search_filters_met = 1
                AND (
                        t_ASSESSMENT.does_meet_id_filters = 0
                    OR  IFNULL(t_WEATHER.does_meet_id_filters, 1) = 0
                    OR  IFNULL(t_LIGHTING_LEVEL.does_meet_id_filters, 1) = 0
                    OR  IFNULL(t_LOCATION.does_meet_id_filters, 1) = 0
                    OR  IFNULL(t_USER_HANDLER.does_meet_id_filters, 1) = 0
                )
            )
            OR  (
                    a_require_all_non_id_search_filters_met = 1
                AND (
                        t_ASSESSMENT.does_meet_non_id_filters = 0
                    OR  IFNULL(t_WEATHER.does_meet_non_id_filters, 1) = 0
                    OR  IFNULL(t_LIGHTING_LEVEL.does_meet_non_id_filters, 1) = 0
                    OR  IFNULL(t_LOCATION.does_meet_non_id_filters, 1) = 0
                    OR  IFNULL(t_USER_HANDLER.does_meet_non_id_filters, 1) = 0
                )
            )
            OR  (
                a_require_any_id_search_filters_met = 1
                AND t_ASSESSMENT.does_meet_id_filters = 0
                AND IFNULL(t_WEATHER.does_meet_id_filters, 1) = 0
                AND IFNULL(t_LIGHTING_LEVEL.does_meet_id_filters, 1) = 0
                AND IFNULL(t_LOCATION.does_meet_id_filters, 1) = 0
                AND IFNULL(t_USER_HANDLER.does_meet_id_filters, 1) = 0
            )
            OR  (
                a_require_any_non_id_search_filters_met = 1
                AND t_ASSESSMENT.does_meet_non_id_filters = 0
                AND IFNULL(t_WEATHER.does_meet_non_id_filters, 1) = 0
                AND IFNULL(t_LIGHTING_LEVEL.does_meet_non_id_filters, 1) = 0
                AND IFNULL(t_LOCATION.does_meet_non_id_filters, 1) = 0
                AND IFNULL(t_USER_HANDLER.does_meet_non_id_filters, 1) = 0
            )
        ;
    END IF;

    IF a_debug = 1 THEN
        SELECT 'After filter Assessments';
        SELECT * FROM tmp_Weather_Calc_Assessment;
        SELECT * FROM tmp_Lighting_Level_Calc_Assessment;
        SELECT * FROM tmp_Location_Calc_Assessment;
        SELECT * FROM tmp_User_Handler_Calc_Assessment;
        SELECT * FROM tmp_Assessment_Calc_Assessment;
    END IF;


    -- Permissions
    IF a_debug = 1 THEN
		SELECT 
			  a_guid -- a_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(
          a_guid -- a_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 = a_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_Calc_Assessment t_ME
        WHERE t_ME.id_type <> v_id_type_error_no_permission
        ;
		INSERT INTO tmp_Msg_Error_Calc_Assessment (
			  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 Button Icons.'
		)
        ;
    END IF;
	
	CALL fetchmetrics.p_dog_clear_calc_user_access(
		a_guid
        , 0 -- a_debug
	);

    IF a_debug = 1 THEN
        SELECT 'Before non-permitted data deletion';
        SELECT * FROM tmp_Weather_Calc_Assessment;
        SELECT * FROM tmp_Lighting_Level_Calc_Assessment;
        SELECT * FROM tmp_Location_Calc_Assessment;
        SELECT * FROM tmp_User_Handler_Calc_Assessment;
        SELECT * FROM tmp_Assessment_Calc_Assessment;
        SELECT * FROM tmp_Msg_Error_Calc_Assessment;
    END IF;
    
    IF EXISTS(SELECT * FROM tmp_Msg_Error_Calc_Assessment 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_Weather_Calc_Assessment;
            SELECT * FROM tmp_Lighting_Level_Calc_Assessment;
            SELECT * FROM tmp_Location_Calc_Assessment;
            SELECT * FROM tmp_User_Handler_Calc_Assessment;
            SELECT * FROM tmp_Assessment_Calc_Assessment;
        END IF;

        DELETE FROM tmp_Assessment_Calc_Assessment;
        DELETE FROM tmp_User_Handler_Calc_Assessment;
        DELETE FROM tmp_Location_Calc_Assessment;
        DELETE FROM tmp_Lighting_Level_Calc_Assessment;
        DELETE FROM tmp_Weather_Calc_Assessment;
    END IF;

    IF a_debug = 1 THEN
        SELECT 'After non-permitted data deletion';
    END IF;

    -- Outputs
	START TRANSACTION;
        -- Assessments
        INSERT INTO fetchmetrics.DOG_Assessment_Temp (
            guid
            , id_assessment
            , id_weather
            , id_lighting_level
            , id_location
            , id_user_handler
            , notes
            , temperature_celcius
            , active

            , does_meet_id_filters
            , does_meet_non_id_filters
        )
        SELECT 
            a_guid
            , t_ASSESSMENT.id_assessment
            , t_ASSESSMENT.id_weather
            , t_ASSESSMENT.id_lighting_level
            , t_ASSESSMENT.id_location
            , t_ASSESSMENT.id_user_handler
            , ASSESSMENT.notes
            , ASSESSMENT.temperature_celcius
            , ASSESSMENT.active

            , t_ASSESSMENT.does_meet_id_filters
            , t_ASSESSMENT.does_meet_non_id_filters
        FROM fetchmetrics.DOG_Assessment ASSESSMENT
        INNER JOIN tmp_Assessment_Calc_Assessment t_ASSESSMENT ON ASSESSMENT.id_assessment = t_ASSESSMENT.id_assessment
        ;
    COMMIT;

    -- Errors
    IF a_show_errors = 1 THEN
        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_Calc_Assessment t_ERROR
        INNER JOIN fetchmetrics.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
        ;
    END IF;

    IF a_debug = 1 AND v_can_view = 1 THEN
        SELECT * FROM tmp_Weather_Calc_Assessment;
        SELECT * FROM tmp_Lighting_Level_Calc_Assessment;
        SELECT * FROM tmp_Location_Calc_Assessment;
        SELECT * FROM tmp_User_Handler_Calc_Assessment;
        SELECT * FROM tmp_Assessment_Calc_Assessment;
    END IF;
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Notes_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Name_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Split_Id_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Assessment_Calc_Assessment;
    DROP TEMPORARY TABLE IF EXISTS tmp_Image_Calc_Assessment;
    
    IF a_debug = 1 THEN
		CALL fetchmetrics.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*

CALL fetchmetrics.p_dog_calc_assessment (
      'slippery                            ' -- a_guid
	, 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
    , 0                                         -- a_require_all_id_search_filters_met
    , 0                                         -- 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
    , 0                                         -- a_show_errors
    , 0                                     	-- a_debug
);

CALL fetchmetrics.p_dog_calc_assessment (
      'slippery                            ' -- a_guid
	, 1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_assessment
	, 0                                     	-- a_get_inactive_assessment
	, ''                                    	-- a_ids_assessment
    , 'pat,point'                                        -- a_notes_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_show_errors
    , 0                                     	-- a_debug
);
SELECT *
FROM fetchmetrics.DOG_Assessment_Temp
;

CALL fetchmetrics.p_dog_clear_calc_assessment (
	'slippery                            ' -- a_guid
    , 1 -- debug
);

-- DELETE FROM fetchmetrics.DOG_Assessment_Temp;

*/
