SELECT * 
FROM fetchmetrics.DOG_Assessment A
WHERE A.id_assessment = 21
ORDER BY A.created_on DESC
;
SELECT * 
FROM fetchmetrics.DOG_Distraction D
INNER JOIN fetchmetrics.DOG_Assessment A ON D.id_assessment = A.id_assessment
WHERE 
	D.id_distraction = 5
    -- A.id_assessment = 21
ORDER BY A.created_on DESC, D.created_on DESC
;
SELECT * 
FROM fetchmetrics.DOG_Assessment_Command_Modality_Link ACML
INNER JOIN fetchmetrics.DOG_Assessment A ON ACML.id_assessment = A.id_assessment
WHERE 
	-- ACML.id_link = 5
    A.id_assessment = 21
ORDER BY A.created_on DESC, ACML.created_on DESC
;
SELECT * 
FROM fetchmetrics.DOG_Assessment_Response AR
INNER JOIN fetchmetrics.DOG_Assessment_Command_Modality_Link ACML ON AR.id_assessment_command_modality_link = ACML.id_link
INNER JOIN fetchmetrics.DOG_Assessment A ON ACML.id_assessment = A.id_assessment
WHERE 
	-- ACML.id_link = 5
    A.id_assessment = 21
ORDER BY A.created_on DESC, ACML.created_on DESC
;

/*
INSERT INTO fetchmetrics.DOG_Assessment_Command_Modality_Link (
    
id_assessment
, 	id_command 
    ,	id_command_modality 	
    ,id_bribe 	
    ,distance_from_handler_metres 
    ,is_in_sight_of_handler 	
    ,is_in_scent_range_of_handler 
    ,is_in_hearing_range_of_handler 
    ,is_on_lead 
    ,active
)
SELECT
	21 -- id_assessment
	, 	C.id_command 
    ,	1 + (C.id_command % 3) -- id_command_modality 	
    , 1 + ((1 + C.id_command) % 3) -- id_bribe 	
    , 1 + ((2 + C.id_command) % 3) -- distance_from_handler_metres 
    , case when (C.id_command % 3) = 0 then 0 else 1 end -- is_in_sight_of_handler 	
    ,case when ((1 + C.id_command) % 3) = 0 then 0 else 1 end -- is_in_scent_range_of_handler 
    , case when ((2 + C.id_command) % 3) = 0 then 0 else 1 end -- is_in_hearing_range_of_handler 
    , case when (C.id_command % 17) = 0 then 1 else 0 end -- is_on_lead 
    , 1 -- active
FROM fetchmetrics.DOG_Command C
-- INNER JOIN Filtered_Commands FC ON C.id_command = FC.id_command
-- WHERE FC.index_command_in_category % 3 = 
WHERE (C.id_command % 7 = 0) or (C.id_command % 11 = 0) or (C.id_command % 13 = 0)
;

INSERT INTO fetchmetrics.DOG_Assessment_Response (
	id_assessment_command_modality_link
    , id_response_quality_metric
    , id_obedience_level
    , value_measured
)
SELECT
	  ACML.id_link AS id_assessment_command_modality_link
    ,  METRIC.id_response_quality_metric
    , 1 + (ACML.id_command % 8) id_obedience_level
    , 1 + ((METRIC.id_response_quality_metric * 3 + ACML.id_command) % 8) value_measured
FROM fetchmetrics.DOG_Assessment_Command_Modality_Link ACML
INNER JOIN fetchmetrics.DOG_Assessment A ON ACML.id_assessment = A.id_assessment
CROSS JOIN (
	SELECT 3 AS id_response_quality_metric
   	UNION
    SELECT 1 AS id_response_quality_metric
) METRIC
WHERE
		A.id_assessment = 21
	AND ACML.id_link <> 5
;

WITH 
	  ACML_Response_Count AS (
        SELECT 
          	  ACML.id_link
        	, COUNT(*) AS response_count
        FROM fetchmetrics.DOG_Assessment_Command_Modality_Link ACML
        INNER JOIN fetchmetrics.DOG_Assessment_Response AR ON ACML.id_link = AR.id_assessment_command_modality_link
        GROUP BY ACML.id_link
    )
    , ACML_With_Responses AS (
        SELECT 
          	  ACML.id_link
        	, CASE WHEN IFNULL(ACML_RESPONSE_COUNT.response_count, 0) > 0 THEN 1 ELSE 0 END AS has_responses
        FROM fetchmetrics.DOG_Assessment_Command_Modality_Link ACML
        LEFT JOIN ACML_Response_Count ACML_RESPONSE_COUNT ON ACML.id_link = ACML_RESPONSE_COUNT.id_link
    )
    , Category_Size_Known AS (
        SELECT 
        	CC.id_command_category
        	, COUNT(*) AS count_known_commands
        FROM fetchmetrics.DOG_Command_Category CC
        INNER JOIN fetchmetrics.DOG_Command C ON CC.id_command_category = C.id_command_category
        LEFT JOIN fetchmetrics.DOG_Assessment_Command_Modality_Link ACML ON C.id_command = ACML.id_command
        LEFT JOIN ACML_With_Responses ACML_WITH_RESPONSES ON ACML.id_link = ACML_WITH_RESPONSES.id_link
        LEFT JOIN fetchmetrics.DOG_Assessment A ON ACML.id_assessment = A.id_assessment
        -- LEFT JOIN fetchmetrics.DOG_Assessment_Response AR ON ACML.id_link = AR.id_assessment_command_modality_link
        WHERE 
        		ACML_WITH_RESPONSES.has_responses = 1
       		AND CC.active = 1
        	AND C.active = 1
        	AND ACML.active = 1
        	AND A.active = 1
        	AND A.id_assessment = 21
        GROUP BY CC.id_command_category
    )
    , Category_Size AS (
        SELECT 
        	CC.id_command_category
        	, COUNT(*) AS count_commands
        FROM fetchmetrics.DOG_Command_Category CC
        INNER JOIN fetchmetrics.DOG_Command C ON CC.id_command_category = C.id_command_category
        WHERE 
        		CC.active = 1
        	AND C.active = 1
       GROUP BY CC.id_command_category
    )
SELECT *
FROM fetchmetrics.DOG_Command_Category CC
LEFT JOIN Category_Size_Known C_SIZE_KNOWN ON CC.id_command_category = C_SIZE_KNOWN.id_command_category
LEFT JOIN Category_Size C_SIZE ON CC.id_command_category = C_SIZE.id_command_category
WHERE CC.active = 1
;


INSERT INTO fetchmetrics.DOG_Assessment (
      id_temp
    , id_weather
    , id_lighting_level
    , id_location 
    , id_user_handler
    , notes
    , temperature_celcius
    , created_on
)
SELECT
      A.id_assessment
    , A.id_weather
    , A.id_lighting_level
    , A.id_location
    , A.id_user_handler
    , A.notes
    , A.temperature_celcius
    , DATE_SUB(A.created_on, INTERVAL 5 WEEK)
FROM fetchmetrics.DOG_Assessment A
WHERE A.id_assessment <= 21
;

INSERT INTO fetchmetrics.DOG_Distraction (
      id_temp
    , id_assessment
    , id_distraction_type
    , id_intensity_level_emotional
    , id_intensity_level_scent
    , id_intensity_level_sight
    , id_intensity_level_sound
    , id_intensity_level_touch
    , quantity
    , proximity_metres
    , notes
)
SELECT
      D.id_distraction
    , D.id_assessment
    , D.id_distraction_type
    , D.id_intensity_level_emotional
    , D.id_intensity_level_scent
    , D.id_intensity_level_sight
    , D.id_intensity_level_sound
    , D.id_intensity_level_touch
    , D.quantity
    , D.proximity_metres
    , D.notes
FROM fetchmetrics.DOG_Distraction D
INNER JOIN fetchmetrics.DOG_Assessment A 
    -- ON D.id_assessment = A.id_assessment
    ON  D.id_assessment = A.id_temp
    AND A.id_assessment > 910
-- WHERE A.id_assessment <= 21
;

INSERT INTO fetchmetrics.DOG_Assessment_Command_Modality_Link (
      id_temp
    , id_assessment
    , id_command
    , id_command_modality
    , id_bribe
    , distance_from_handler_metres
    , is_in_sight_of_handler
    , is_in_scent_range_of_handler
    , is_in_hearing_range_of_handler
    , is_on_lead
)
SELECT
      ACML.id_link
    , ACML.id_assessment
    , ACML.id_command
    , ACML.id_command_modality
    , ACML.id_bribe
    , ACML.distance_from_handler_metres
    , ACML.is_in_sight_of_handler
    , ACML.is_in_scent_range_of_handler
    , ACML.is_in_hearing_range_of_handler
    , ACML.is_on_lead
FROM fetchmetrics.DOG_Assessment_Command_Modality_Link ACML
INNER JOIN fetchmetrics.DOG_Assessment A 
    -- ON ACML.id_assessment = A.id_assessment
    ON  ACML.id_assessment = A.id_temp
    AND A.id_assessment > 910
-- WHERE A.id_assessment <= 21
;

INSERT INTO fetchmetrics.DOG_Assessment_Response (
      id_temp
    , id_assessment_command_modality_link
    , id_response_quality_metric
    , id_obedience_level
    , value_measured
    , notes
)
SELECT
      AR.id_response
    , ACML.id_link -- (SELECT ACML_NEW.id_assessment_command_modality_link FROM fetchmetrics.DOG_Assessment_Command_Modality_Link ACML_NEW WHERE ACML_NEW.id_temp
    , AR.id_response_quality_metric
    , AR.id_obedience_level
    , POWER(CASE WHEN AR.id_obedience_level = 3 THEN 0.9 ELSE 1.35 END, 6) * AR.value_measured
    , AR.notes
FROM fetchmetrics.DOG_Assessment_Response AR
INNER JOIN fetchmetrics.DOG_Assessment_Command_Modality_Link ACML 
    -- ON AR.id_assessment_command_modality_link = ACML.id_link
    ON  AR.id_assessment_command_modality_link = ACML.id_temp
    AND ACML.id_link > 3168
-- INNER JOIN fetchmetrics.DOG_Assessment A ON AR.id_assessment = A.id_assessment
-- WHERE A.id_assessment <= 21
;

-- DELETE A FROM fetchmetrics.DOG_Assessment A WHERE A.id_assessment > 21;
-- DELETE A FROM fetchmetrics.DOG_Assessment A WHERE A.id_assessment > 21;
-- DELETE A FROM fetchmetrics.DOG_Assessment A WHERE A.id_assessment > 21;
-- DELETE A FROM fetchmetrics.DOG_Assessment A WHERE A.id_assessment > 21;
*/

SELECT *
FROM fetchmetrics.DOG_Assessment_Response AR
INNER JOIN fetchmetrics.DOG_Assessment_Command_Modality_Link ACML ON AR.id_assessment_command_modality_link = ACML.id_link
INNER JOIN fetchmetrics.DOG_Assessment A ON ACML.id_assessment = A.id_assessment
INNER JOIN fetchmetrics.DOG_Command C ON ACML.id_command = C.id_command
WHERE C.name LIKE '%SIT%'
ORDER BY A.created_on, AR.id_response
;