	
	--TeleHealthMeasurementUnitTypes

	declare @HeightId uniqueidentifier = '5247C90B-713C-4EBE-8299-7861CA44E400'
	declare @WeightId uniqueidentifier = 'CFB58C51-F70B-452D-99C7-617367FD1ACC'
	declare @TemperatureId uniqueidentifier = 'BC200A83-0D8A-4772-A1AE-A02063CCBCB2'
	declare @BloodPressureId uniqueidentifier = '95FEFA13-92C5-4955-8388-79B1B64726AD'
	declare @SugarLevelId uniqueidentifier = 'FDF5517C-2323-4322-B5D1-D942341F4248'
	declare @HeartRateId uniqueidentifier = 'FD2E13DE-7CD4-4832-9778-71B049431CF5'
	declare @SaturationId uniqueidentifier = 'C18A1956-EEDF-48DC-9ED7-DBC69B7904B0'

	DECLARE @TeleHealthMeasurementUnitTypes TABLE (Id uniqueidentifier NOT NULL,[Name] [nvarchar](500) NOT NULL,[Description] [nvarchar](500) Not NULL);

	Insert into @TeleHealthMeasurementUnitTypes(Id,[Name],[Description]) Values(@HeightId,'Height','');
	Insert into @TeleHealthMeasurementUnitTypes(Id,[Name],[Description]) Values(@WeightId,'Weight','');
	Insert into @TeleHealthMeasurementUnitTypes(Id,[Name],[Description]) Values(@TemperatureId,'Temperature','');
	Insert into @TeleHealthMeasurementUnitTypes(Id,[Name],[Description]) Values(@BloodPressureId,'BloodPressure','');
	Insert into @TeleHealthMeasurementUnitTypes(Id,[Name],[Description]) Values(@SugarLevelId,'SugarLevel','');
	Insert into @TeleHealthMeasurementUnitTypes(Id,[Name],[Description]) Values(@HeartRateId,'HeartRate','');
	Insert into @TeleHealthMeasurementUnitTypes(Id,[Name],[Description]) Values(@SaturationId,'Saturation','');

	MERGE INTO dbo.[TeleHealthMeasurementUnitTypes] AS TARGET  
		USING @TeleHealthMeasurementUnitTypes AS SOURCE  
		ON TARGET.Id = SOURCE.Id	
		WHEN MATCHED THEN    
			UPDATE SET	[Name] = SOURCE.[Name],
						[Description] = SOURCE.[Description]
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (Id, [Name], [Description]) VALUES (SOURCE.Id, SOURCE.[Name], SOURCE.[Description])  
	OUTPUT $action, SOURCE.*;

	--TeleHealthMeasurementUnits

	declare @HeightFeetId uniqueidentifier = '498AF0A1-637E-47B5-A74D-38FE8B3760AA'
	declare @HeightCmId uniqueidentifier = 'DA82D1FF-C3AE-4B03-9B60-32FB9D6A1DA8'
	declare @WeightLbsId uniqueidentifier = 'EB90C2D0-D670-45FC-8621-292868C630A2'
	declare @WeightKgsId uniqueidentifier = '371EB67D-7786-4156-86B8-A7C864635BAA'
	declare @TemperatureFahrenheitId uniqueidentifier = '1B000622-F796-4CE3-8DBF-CCD27CAF8969'
	declare @TemperatureCelsiusId uniqueidentifier = '3F377DA9-D613-482C-971D-8DADDF5487B9'
	declare @BloodPressureMmHgId uniqueidentifier = 'ED967EB3-F4A8-4FCE-95B2-71C17977668C'
	declare @BloodPressurekPaId uniqueidentifier = '832684C6-9475-4AD0-B4ED-B15F3BF0BDF1'
	declare @SugarLevelMgdLId uniqueidentifier = 'CC9AF003-00A0-4844-8050-0E5B24B3E54F'
	declare @SugarLevelMmolLId uniqueidentifier = '64476323-A472-4BE0-9115-93C14165D2BB'
	declare @HeartRateBeatsPerMinuteId uniqueidentifier = '4F3E4AF3-E254-4421-907B-268152480FFB'
	declare @SaturationPercentId uniqueidentifier = 'C66332B4-4110-4E86-A3DB-291C49A9AC19'

	DECLARE @TeleHealthMeasurementUnits TABLE (Id uniqueidentifier NOT NULL,MeasurementUnitTypeId uniqueidentifier NOT NULL,[Value] [nvarchar](500) Not NULL,
							[Description] [nvarchar](500) Not NULL,[DecimalPlaces] INT DEFAULT ((0)) NOT NULL);

	--Height
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@HeightFeetId,@HeightId,'Feet','Height in Feet',0);
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@HeightCmId,@HeightId,'CM','Height in centimeters',0);
	--Weight
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@WeightLbsId,@WeightId,'Lbs','Weight in Pounds',1);
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@WeightKgsId,@WeightId,'Kgs','Weight in Kilograms',1);
	--Temperature																		  
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@TemperatureFahrenheitId,@TemperatureId,'Fahrenheit','Temperature in Fahrenheit',1);
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@TemperatureCelsiusId,@TemperatureId,'Celsius','Temperature in Celsius',1);
	--BloodPressure																		  
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@BloodPressureMmHgId,@BloodPressureId,'MmHg','Blood Pressure in millimeters of mercury',0);
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@BloodPressurekPaId,@BloodPressureId,'kPa','Blood Pressure in kilopascals',1);
	--SugarLevel																		  
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@SugarLevelMgdLId,@SugarLevelId,'MgDL','Sugar Level in milligrams per decilitre',0);
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@SugarLevelMmolLId,@SugarLevelId,'MmolL','Sugar Level in millimoles per litre',0);
	--HeartRate																			 
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@HeartRateBeatsPerMinuteId,@HeartRateId,'BPM','Heart rate in beats per minute',0);
	--Saturation																		  
	Insert into @TeleHealthMeasurementUnits(Id,MeasurementUnitTypeId,[Value],[Description],DecimalPlaces) Values(@SaturationPercentId,@SaturationId,'Percent','Saturation in percent',0);

	MERGE INTO dbo.[TeleHealthMeasurementUnits] AS TARGET  
		USING @TeleHealthMeasurementUnits AS SOURCE  
		ON TARGET.Id = SOURCE.Id	
		WHEN MATCHED THEN    
			UPDATE SET	MeasurementUnitTypeId = SOURCE.MeasurementUnitTypeId,
						[Value] = SOURCE.[Value],
						[Description] = SOURCE.[Description]
		WHEN NOT MATCHED BY TARGET THEN
			INSERT (Id, MeasurementUnitTypeId,[Value], [Description]) VALUES (SOURCE.Id, SOURCE.MeasurementUnitTypeId, SOURCE.[Value], SOURCE.[Description])  
	OUTPUT $action, SOURCE.*;


	--TeleHealthMeasurementUnitGroups

	declare @MeasurementUnitGroupDefaultId uniqueidentifier = '2D88D5CA-EFBF-465D-A4C3-B39B7F35230C'

	DECLARE @TeleHealthMeasurementUnitGroups TABLE (Id uniqueidentifier NOT NULL,[Name] [nvarchar](500) NOT NULL,[Description] [nvarchar](500) Not NULL);

	Insert into @TeleHealthMeasurementUnitGroups(Id,[Name],[Description]) Values(@MeasurementUnitGroupDefaultId,'TeleHealth Measurement units group default','TeleHealth Default Group');

	MERGE INTO dbo.[TeleHealthMeasurementUnitGroups] AS TARGET  
		USING @TeleHealthMeasurementUnitGroups AS SOURCE  
		ON TARGET.Id = SOURCE.Id	
		WHEN MATCHED THEN    
			UPDATE SET	[Name] = SOURCE.[Name],
						[Description] = SOURCE.[Description]
		WHEN NOT MATCHED BY TARGET THEN
			INSERT (Id,[Name],[Description]) VALUES (SOURCE.Id, SOURCE.[Name], SOURCE.[Description])  
	OUTPUT $action, SOURCE.*;


	--TeleHealthMeasurementUnitGroupValues

	declare @TeleHealthMeasurementUnitGroupValuesDefaultIdHeight uniqueidentifier = 'E73D42F4-6967-488A-9526-03B353AAEF75'
	declare @TeleHealthMeasurementUnitGroupValuesDefaultIdWeight uniqueidentifier = '3B75676D-8EA4-464F-A5BD-E35E3ABB9BBB'
	declare @TeleHealthMeasurementUnitGroupValuesDefaultIdTemperature uniqueidentifier = 'ECE92365-6480-48D7-8CFD-B557BFAC5BC5'
	declare @TeleHealthMeasurementUnitGroupValuesDefaultIdBloodPressure uniqueidentifier = '27AC511C-6CF0-419F-A078-9D14E59798A9'
	declare @TeleHealthMeasurementUnitGroupValuesDefaultIdSugarLevel uniqueidentifier = '4234C687-7B10-4FE7-8A17-649F2ABC3BC5'
	declare @TeleHealthMeasurementUnitGroupValuesDefaultIdHeartRate uniqueidentifier = '6ED2E449-0601-4D9F-82C6-8C3C1BC43489'
	declare @TeleHealthMeasurementUnitGroupValuesDefaultIdSaturation uniqueidentifier = 'E7DFFC4F-58DD-4C55-9338-6BB2B8DDE16E'

	declare @TeleHealthMeasurementUnitGroupValues TABLE (Id uniqueidentifier NOT NULL, GroupId uniqueidentifier NOT NULL, MeasurementUnitId uniqueidentifier NOT NULL)

	Insert into @TeleHealthMeasurementUnitGroupValues(Id,GroupId,MeasurementUnitId) Values(@TeleHealthMeasurementUnitGroupValuesDefaultIdHeight,@MeasurementUnitGroupDefaultId,@HeightCmId);--Height Cm
	Insert into @TeleHealthMeasurementUnitGroupValues(Id,GroupId,MeasurementUnitId) Values(@TeleHealthMeasurementUnitGroupValuesDefaultIdWeight,@MeasurementUnitGroupDefaultId,@WeightKgsId);--Weight Kgs
	Insert into @TeleHealthMeasurementUnitGroupValues(Id,GroupId,MeasurementUnitId) Values(@TeleHealthMeasurementUnitGroupValuesDefaultIdTemperature,@MeasurementUnitGroupDefaultId,@TemperatureCelsiusId);--Temperature Celsius
	Insert into @TeleHealthMeasurementUnitGroupValues(Id,GroupId,MeasurementUnitId) Values(@TeleHealthMeasurementUnitGroupValuesDefaultIdBloodPressure,@MeasurementUnitGroupDefaultId,@BloodPressurekPaId);--BloodPressure kPaId
	Insert into @TeleHealthMeasurementUnitGroupValues(Id,GroupId,MeasurementUnitId) Values(@TeleHealthMeasurementUnitGroupValuesDefaultIdSugarLevel,@MeasurementUnitGroupDefaultId,@SugarLevelMmolLId);--SugarLevel MmolLId
	Insert into @TeleHealthMeasurementUnitGroupValues(Id,GroupId,MeasurementUnitId) Values(@TeleHealthMeasurementUnitGroupValuesDefaultIdHeartRate,@MeasurementUnitGroupDefaultId,@HeartRateBeatsPerMinuteId);--Heartrate BeatsPerMinute
	Insert into @TeleHealthMeasurementUnitGroupValues(Id,GroupId,MeasurementUnitId) Values(@TeleHealthMeasurementUnitGroupValuesDefaultIdSaturation,@MeasurementUnitGroupDefaultId,@SaturationPercentId);--Saturation Percent

	MERGE INTO dbo.[TeleHealthMeasurementUnitGroupValues] AS TARGET  
		USING @TeleHealthMeasurementUnitGroupValues AS SOURCE  
		ON TARGET.Id = SOURCE.Id	
		WHEN MATCHED THEN    
			UPDATE SET	GroupId = SOURCE.GroupId,
						MeasurementUnitId = SOURCE.MeasurementUnitId
		WHEN NOT MATCHED BY TARGET THEN
			INSERT (Id,GroupId,MeasurementUnitId) VALUES (SOURCE.Id, SOURCE.GroupId, SOURCE.MeasurementUnitId)  
	OUTPUT $action, SOURCE.*;


	--TeleHealthMeasurementUnitGroupsToLanguage

	declare @LanguageDefaultId int = 0--No such Language in DB- will be used as default

	declare @TeleHealthMeasurementUnitGroupsToLanguage TABLE (LanguageId int NOT NULL, GroupId uniqueidentifier NOT NULL)

	Insert into @TeleHealthMeasurementUnitGroupsToLanguage(LanguageId, GroupId) Values(@LanguageDefaultId,@MeasurementUnitGroupDefaultId)

	MERGE INTO dbo.[TeleHealthMeasurementUnitGroupsToLanguage] AS TARGET  
		USING @TeleHealthMeasurementUnitGroupsToLanguage AS SOURCE  
		ON TARGET.LanguageId = SOURCE.LanguageId	
		WHEN MATCHED THEN    
			UPDATE SET	GroupId = SOURCE.GroupId
		WHEN NOT MATCHED BY TARGET THEN
			INSERT (LanguageId,GroupId) VALUES (SOURCE.LanguageId, SOURCE.GroupId)  
	OUTPUT $action, SOURCE.*;
