
USE parts;


-- Error Message Types
/* Existing table */
INSERT INTO parts.CORE_Msg_Error_Type (
	  code
    , name
    , description
    , is_breaking_error
)
VALUES 
	(
          'BAD_DATA'
        , 'Invalid data'
        , 'Rubbish data'
        , 1
    )
	, (
          'NO_PERMISSION'
        , 'No permission'
        , 'Not authorised'
        , 1
    )
    , (
          'MYSQL_ERROR'
        , 'MySQL error'
        , 'MySQL execution error.'
        , 1
    )
    , (
          'WARNING'
        , 'Warning'
        , 'Non-breaking error.'
        , 0
    )
;

INSERT INTO parts.CORE_File_Type (
      code
    , name
    , is_image
)
VALUES
      (
          'JPG'
        , 'JPG'
        , 1
    )
;

-- Access Levels
INSERT INTO parts.DOG_Access_Level (
	  display_order
    , code
    , name
    , priority
)
VALUES 
	(
          1
        , 'VIEW'
        , 'View'
        , 3
    )
	, (
          2
        , 'EDIT'
        , 'Edit'
        , 2
    )
	, (
          3
        , 'ADMIN'
        , 'Admin'
        , 1
    )
	, (
          4
        , 'NONE'
        , 'None'
        , 999
    )
;

-- Permission Groups
INSERT INTO parts.DOG_Permission_Group (
	display_order
    , code
    , name
)
VALUES 
	(
          0
        , 'DOG'
        , 'Dog'
    )
	, (
          1
        , 'USER'
        , 'User'
    )
    , (
          2
        , 'PROJECT_HUB'
        , 'Project Hub'
    )
;

-- Permissions
INSERT INTO parts.DOG_Permission (
	  display_order
    , code
    , name
    , id_permission_group
    , id_access_level_required
)
VALUES 
	(
          1
        , 'DOG_VIEW'
        , 'View Dog'
        , 1
        , 1
    )
	, (
          1
        , 'DOG_ADMIN'
        , 'Admin Dog'
        , 1
        , 3
    )
	, (
          1
        , 'DOG_CREATE'
        , 'Create Dog'
        , 1
        , 2
    )
	, (
          1
        , 'USER_VIEW'
        , 'View User'
        , 2
        , 1
    )
	, (
          1
        , 'USER_ADMIN'
        , 'Admin User'
        , 2
        , 3
    )
	, (
          1
        , 'USER_CREATE'
        , 'Create User'
        , 2
        , 2
    )
	, (
          1
        , 'CONTACT_FORM_ADMIN'
        , 'Admin Contact Form'
        , 3
        , 3
    )
	, (
          1
        , 'CONTACT_FORM_CREATE'
        , 'Create Contact Form'
        , 3
        , 4
    )
;

-- Users
INSERT INTO parts.DOG_User (
	  id_user_auth0
	, firstname
    , surname
	, email
	, is_super_user
    , active
)
VALUES 
	(
		  'auth0|6582b95c895d09a70ba10fef' -- id_user_auth0
		, 'Teddy' -- firstname
        , 'Middleton-Smith' -- surname
        , 'edward.middletonsmith@gmail.com' -- email
        , 1 -- is_super_user
		, 1 -- active
	)
    , (
		'auth0|672659014296b7f94a9bab45' -- id_user_auth0
		, 'Tierney' -- firstname
		, 'Gullen' -- surname
		, 'tierneybailey13@gmail.com' -- email
		, 1 -- is_super_user
		, 1 -- active
	)
    , (
          NULL -- id_user_auth0
        , 'Command Bot' -- firstname
        , 'Bot' -- surname
        , 'teddy@partsltd.co.uk' -- email
        , 0 -- is_super_user
        , 1 -- active
    )
    , (
          'auth0|679cc2fe0d652a47af90d16b' -- id_user_auth0
        , 'Demo' -- firstname
        , 'Demo' -- surname
        , 'bot@partsltd.co.uk' -- email
        , 0 -- is_super_user
        , 1 -- active
    )
;

-- Roles
INSERT INTO parts.DOG_Role (
	  display_order
	, code
	, name
    , id_user_created_by
)
VALUES 
	  (
          1
        , 'MANAGER'
        , 'Manager'
        , 1
    )
	, (
          2
        , 'USER'
        , 'User'
        , 1
    )
;

-- Role Permission link
INSERT INTO parts.DOG_Role_Permission_Link (
	  id_role
    , id_permission
    , id_access_level
    , id_user_created_by
)
VALUES 
	  (
          1
        , 1
        , 3
        , 1
    )
	, (
          1
        , 2
        , 3
        , 1
    )
	, (
          1
        , 3
        , 3
        , 1
    )
	, (
          1
        , 4
        , 3
        , 1
    )
	, (
          1
        , 5
        , 3
        , 1
    )
	, (
          1
        , 6
        , 3
        , 1
    )
	, (
          1
        , 7
        , 3
        , 1
    )
	, (
          1
        , 8
        , 3
        , 1
    )
	, (
          2
        , 1
        , 1
        , 1
    )
	, (
          2
        , 2
        , 1
        , 1
    )
	, (
          2
        , 3
        , 1
        , 1
    )
	, (
          2
        , 4
        , 1
        , 1
    )
	, (
          2
        , 5
        , 1
        , 1
    )
	, (
          2
        , 6
        , 1
        , 1
    )
	, (
          2
        , 7
        , 4
        , 1
    )
	, (
          2
        , 8
        , 1
        , 1
    )
;

-- User Role link
INSERT INTO parts.DOG_User_Role_Link (
	  id_user
    , id_role
    , id_user_created_by
)
VALUES 
	(
          1
        , 1
        , 1
    )
	, (
          2
        , 2
        , 1
    )
	, (
          3
        , 2
        , 1
    )
	, (
          4
        , 2
        , 1
    )
;


# Unit of Measurement
INSERT INTO parts.DOG_Unit_Measurement (
	name_singular, name_plural, symbol, is_base_unit, is_unit_of_distance, is_unit_of_mass, is_unit_of_time, is_unit_of_volume
)
VALUES
	('metre', 'metres', 'm', 1, 1, 0, 0, 0)
	, ('millimetre', 'millimetres', 'mm', 0, 1, 0, 0, 0)
    , ('kilogram', 'kilograms', 'kg', 1, 0, 1, 0, 0)
	, ('gram', 'grams', 'g', 0, 0, 1, 0, 0)
	, ('litre', 'litres', 'L', 0, 0, 0, 0, 1)
	, ('millilitre', 'millilitres', 'mL', 0, 0, 0, 0, 1)
    , ('item', 'items', 'x', 0, 0, 0, 0, 0)
	, ('second', 'seconds', 's', 1, 0, 0, 1, 0)
	, ('hour', 'hours', 'h', 0, 0, 0, 1, 0)
	, ('day', 'days', 'd', 0, 0, 0, 1, 0)
    , ('ratio', 'ratios', '/', 0, 0, 0, 0, 0)
;

# Unit of Measurement Conversion
INSERT INTO parts.DOG_Unit_Measurement_Conversion (
	id_unit_derived
	, id_unit_base
	, display_order
	, multiplier_unit_base
	, increment_unit_base
	, apply_multiplier_before_increment
)
VALUES 
	(
		2 -- id_unit_derived
		, 1 -- id_unit_base
		, 1 -- display_order
		, 0.001 -- multiplier_unit_base
		, 0 -- increment_unit_base
		, apply_multiplier_before_increment
	)
	, (
		4 -- id_unit_derived
		, 3 -- id_unit_base
		, 1 -- display_order
		, 0.001 -- multiplier_unit_base
		, 0 -- increment_unit_base
		, apply_multiplier_before_increment
	)
	, (
		6 -- id_unit_derived
		, 5 -- id_unit_base
		, 1 -- display_order
		, 0.001 -- multiplier_unit_base
		, 0 -- increment_unit_base
		, apply_multiplier_before_increment
	)
	, (
		9 -- id_unit_derived
		, 8 -- id_unit_base
		, 1 -- display_order
		, 24 -- multiplier_unit_base
		, 0 -- increment_unit_base
		, apply_multiplier_before_increment
	)
;


-- Project-specific tables
INSERT INTO parts.DOG_Dog (
      name
    , appearance
    , mass_kg
)
VALUES 
      (
          'Molly'
        , 'Cute'
        , 20
    )
;

INSERT INTO parts.DOG_Breed (
      code
    , name
)
VALUES 
      (
          'BORDER_COLLIE'
        , 'Border Collie'
    )
;

INSERT INTO parts.DOG_Dog_Breed_Link (
      id_dog
    , id_breed
    , lineage_ratio
)
VALUES 
      (
          1
        , 1
        , 1
    )
;

INSERT INTO parts.DOG_Drive (
      code
    , name
)
VALUES 
      (
          'PREY'
        , 'Prey'
    )
    , (
          'PACK'
        , 'Pack'
    )
;

INSERT INTO parts.DOG_Dog_Drive_Link (
      id_dog
    , id_drive
    , dominance_ratio
)
VALUES 
      (
          1
        , 1
        , 0.5
    )
    , (
          1
        , 2
        , 0.5
    )
;

/*
INSERT INTO parts.DOG_Understanding_Level (
      code
    , name
)
VALUES 
      (
          'UNKNOWN'
        , 'Not known'
    )
    , (
          'INTRODUCED'
        , 'Has been introduced'
    )
    , (
          'RECOGNISES_COMBINED_VISUAL_AND_VERBAL_SIGNAL'
        , 'Combined visual and verbal signal recognised'
    )
    , (
          'RECOGNISES_VISUAL_SIGNAL'
        , 'Visual signal alone recognised'
    )
    , (
          'RECOGNISES_VERBAL_SIGNAL'
        , 'Verbal signal alone recognised'
    )
    , (
          'UNDERSTOOD'
        , 'Understood'
    )
;

INSERT INTO parts.DOG_Obedience_Level (
      code
    , name
)
VALUES 
      (
          'REFUSAL'
        , 'Refuses to obey'
    )
    , (
          'ON_HER_TERMS'
        , 'Might obey if she feels like it'
    )
    , (
          'OBEYS_UNDISTRACTED'
        , 'Obeys without distractions'
    )
    , (
          'OBEYS_MILDLY_DISTRACTED'
        , 'Obeys when mildly distracted'
    )
    , (
          'OBEYS_PERFECTLY'
        , 'Obeys in all situations'
    )
;
*/

INSERT INTO parts.DOG_Command_Category (
      code
    , name
)
VALUES 
      (
          'ACTIVITIES'
        , 'Activities'
    )
    , (
          'BASIC_OBEDIENCE'
        , 'Basic obedience'
    )
    , (
          'BEHAVIOUR_MODIFICATION'
        , 'Behaviour modification'
    )
    , (
          'BODY_PARTS'
        , 'Body parts'
    )
    , (
          'CLOTHING_AND_EQUIPMENT'
        , 'Clothing and equipment'
    )
    , (
          'COMPARISON'
        , 'Comparison'
    )
    , (
          'ADJECTIVES'
        , 'Compliments and insults (adjectives)'
    )
    , (
          'EMOTIONAL_STATE'
        , 'Emotional state'
    )
    , (
          'ENVIRONMENT'
        , 'Environment'
    )
    , (
          'ESSENTIAL_NEEDS'
        , 'Essential needs'
    )
    , (
          'IMPULSE_CONTROL'
        , 'Impulse control'
    )
    , (
          'LOCATION'
        , 'Location'
    )
    , (
          'MANNERS'
        , 'Manners'
    )
    , (
          'MEALTIME'
        , 'Mealtime'
    )
    , (
          'PARTY_TRICK'
        , 'Party trick'
    )
    , (
          'PEOPLE'
        , 'People'
    )
    , (
          'PLAY'
        , 'Play'
    )
    , (
          'POSITION'
        , 'Position'
    )
    , (
          'QUESTIONS'
        , 'Questions'
    )
    , (
          'SERVICE'
        , 'Service'
    )
    , (
          'TIME_AND_SCHEDULING'
        , 'Time and scheduling'
    )
    , (
          'UTILITY'
        , 'Utility'
    )
;

/*
INSERT INTO parts.DOG_Command (
      code
    , name
)
VALUES 
      (
          'lick'
        , 'nips'
    )
;
*/

/*
INSERT INTO parts.DOG_Dog_Command_Link (
      id_dog
    , id_command
    , id_understanding_level
    , id_obedience_level
    , notes
)
VALUES 
      (
          'lick'
        , 'nips'
    )
;
*/

INSERT INTO parts.DOG_Command_Modality (
      code
    , name
)
VALUES 
      (
          'VISUAL'
        , 'Visual signal'
    )
    , (
          'VERBAL'
        , 'Verbal signal'
    )
    , (
          'BUTTON'
        , 'Button press'
    )
;

INSERT INTO parts.DOG_Location (
      code
    , name
)
VALUES 
      (
          'HOME'
        , 'Home'
    )
;

/*
INSERT INTO parts.DOG_Location_Link (
      id_location_parent
    , id_location_child
)
VALUES 
      (
          1
        , 2
    )
;
*/

INSERT INTO parts.DOG_Button_Shape (
      code
    , name
    , description
)
VALUES 
      (
          'ROUND'
        , 'Round'
        , 'Cylindrical'
    )
    , (
          'HEXAGONAL'
        , 'Hexagonal'
        , 'Hexagonal prism'
    )
;

INSERT INTO parts.DOG_Colour (
      code
    , name
)
VALUES 
      (
          'RED'
        , 'Red'
    )
    , (
          'GREEN'
        , 'Green'
    )
;

/*
INSERT INTO parts.DOG_Image (
      id_file_type
    , id_dog
    , path
    , display_order
)
VALUES 
      (
          1
        , 1
        , NULL
        , NULL
    )
;
*/

/*
INSERT INTO parts.DOG_Button_Icon (
      id_image
    , code
    , name
    , description
)
VALUES 
      (
          1
        , NULL
    )
;
*/

/*
INSERT INTO parts.DOG_Command_Button_Link (
      id_command
    , id_button_shape
    , id_button_colour
    , id_button_icon
    , id_location
)
VALUES 
      (
          1     -- AS id_command
        , 1     -- AS id_button_shape
        , 1     -- AS id_button_colour
        , NULL  -- AS id_button_icon
        , 1     -- AS id_location
    )
;
*/

INSERT INTO parts.DOG_Weather (
      code
    , name
)
VALUES 
      (
          'CLEAR_SUNNY'
        , 'Sunny and clear'
    )
    , (
          'LIGHTLY_CLOUDY_SUNNY'
        , 'Sunny with some clouds'
    )
    , (
          'CLOUDY'
        , 'Cloudy'
    )
    , (
          'RAINY'
        , 'Raining'
    )
    , (
          'SNOWING'
        , 'Snowing'
    )
    , (
          'THAWING'
        , 'Thawing'
    )
;

INSERT INTO parts.DOG_Lighting_Level (
      code
    , name
)
VALUES 
      (
          'BRIGHT'
        , 'Bright'
    )
    , (
          'MODERATELY_BRIGHT'
        , 'Moderately Bright'
    )
    , (
          'DAWN_DUSK'
        , 'Dawn or Dusk'
    )
    , (
          'DARK_WITH_SOME_LIGHTING'
        , 'Dark With Some Lighting'
    )
    , (
          'DARK'
        , 'Dark'
    )
;

/*
INSERT INTO parts.DOG_Assessment (
      id_weather
	, id_lighting_level
	, id_location
	, id_user_handler
	, notes
    , difficulty_level
    , active
)
VALUES 
    (

    )
;
*/

INSERT INTO parts.DOG_Distraction_Type (
      code
    , name
)
VALUES 
      (
          'RESTLESSNESS'
        , 'Restlessness'
    )
    , (
          'LIVE_PREY'
        , 'Live Prey'
    )
    , (
          'DEAD_PREY'
        , 'Dead Prey'
    )
    , (
          'VEHICLES_MACHINES'
        , 'Vehicles and Machines'
    )
    , (
          'EXPLOSION'
        , 'Explosion'
    )
    , (
          'Treat'
        , 'Treat'
    )
    , (
          'TOY'
        , 'Toy'
    )
    , (
          'SQUEAKY_TOY'
        , 'Squeaky Toy'
    )
;

INSERT INTO parts.DOG_Distraction_Intensity_Level (
      code
    , name
)
VALUES 
      (
          'NEGLIGIBLE'
        , 'Negligible'
    )
    , (
          'MILD'
        , 'Mild'
    )
    , (
          'MODERATE'
        , 'Moderate'
    )
    , (
          'STRONG'
        , 'Strong'
    )
    , (
          'EXTREME'
        , 'Extreme'
    )
;

/*
INSERT INTO parts.DOG_Distraction (
      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
    , active
)
VALUES 
    (

    )
;
*/

INSERT INTO parts.DOG_Bribe (
      code
    , name
)
VALUES 
      (
          'NONE'
        , 'None'
    )
    , (
          'TREAT'
        , 'Treat'
    )
    , (
          'FAVOURITE_TREAT'
        , 'Favourite treat'
    )
    , (
          'TOY'
        , 'Toy'
    )
    , (
          'FAVOURITE_TOY'
        , 'Favourite Toy'
    )
    , (
          'SQUEAKY_TOY'
        , 'Squeaky Toy'
    )
;

/*
INSERT INTO parts.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
	, trial_count
    , active
)
VALUES 
    (

    )
;
*/

INSERT INTO parts.DOG_Response_Quality_Metric (
      id_unit_measurement
    , code
    , name
    , value_min
    , value_max
)
VALUES 
      (
          8
        , 'RESPONSE_TIME'
        , 'Response Time'
        , 0
        , NULL
    )
    , (
          11
        , 'ACCURACY_RATIO'
        , 'Accuracy Ratio'
        , 0
        , 1
    )
    , (
          8
        , 'COMPLIANCE_DURATION'
        , 'Compliance Duration'
        , 0
        , NULL
    )
    , (
          11
        , 'PRECISION_RATIO'
        , 'Precision Ratio'
        , 0
        , 1
    )
;

INSERT INTO parts.DOG_Obedience_Level (
      code
    , name
)
VALUES 
      (
          'EAGER'
        , 'Eager'
    )
    , (
          'FIRM_LOOK'
        , 'After Firm Look'
    )
    , (
          'POWER_POSE'
        , 'After Power Pose'
    )
    , (
          'THREATEN_TO_APPROACH'
        , 'After Threatening to Approach'
    )
    , (
          'LITTLE_MOVE'
        , 'After Moving a Few Steps'
    )
    , (
          'HALF_APPROACH'
        , 'After Moving Half Way'
    )
    , (
          'MOST_APPROACH'
        , 'After Approaching Most of The Way'
    )
    , (
          'TOUCH_COLLAR'
        , 'After Touching Collar'
    )
    , (
          'RUN_AWAY_AND_RETURN'
        , 'After Running Away And Returning'
    )
    , (
          'LEAD'
        , 'After Being Lead by Collar or Lead'
    )
    , (
          'CHECK_BRIBE'
        , 'After Check or Bribe'
    )
    , (
          'CHECKS_BRIBES'
        , 'After Multiple Checks and/or Bribes'
    )
    , (
          'REFUSAL'
        , 'None - Refusal'
    )
    , (
          'REFUSAL_RUN_AWAY'
        , 'Refused and Ran Away Refusing to Return'
    )
;

/*
 Post Excel-insert:

SELECT COUNT(*) FROM parts.DOG_Command_Temp;

CALL parts.p_dog_save_command (
	'nipples'
    , (SELECT GUID FROM parts.DOG_Command_Temp COMMAND_TEMP ORDER BY id_temp DESC LIMIT 1)
    , 1
    , 1
);


SELECT *
FROM parts.DOG_Dog;

/ *
SELECT *
FROM parts.DOG_Command;
* /

SELECT *
-- DELETE
FROM parts.DOG_Obedience_Level;

/ *
DELETE FROM parts.DOG_Dog_Command_Link;
INSERT INTO parts.DOG_Dog_Command_Link (
      id_dog
    , id_command
    , id_understanding_level
    , id_obedience_level
    , notes
)
SELECT
	  DOG.id_dog
    , COMMAND.id_command
    , 
FROM parts.DOG_Dog DOG
CROSS JOIN parts.DOG_Command COMMAND
WHERE DOG.name = 'Molly'

* /

SELECT * FROM parts.DOG_Dog_Command_Link;

*/
