/*
Add timer Job + Parameters
	Telehealth jobs 
		- GeneratePatientMeasurementMonitoringPeriodss
*/

	--04: ConfigurationGroups
	--	Create Instance of the group 
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );

	--Job
	INSERT into @ConfigurationGroups (EntityId, GuidKey) VALUES ( 4,'CA072D3E-91A2-4031-9FA8-59DE926926D1')
	--Job parameters
	INSERT into @ConfigurationGroups (EntityId, GuidKey) VALUES ( 5,'499D7842-9FA2-4F55-82FA-B7E4AD2021E4')

	MERGE INTO dbo.ConfigurationGroups AS TARGET  
		USING @ConfigurationGroups AS SOURCE  
		ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
		WHEN MATCHED THEN    
				UPDATE SET entityid = SOURCE.entityid
		WHEN NOT MATCHED BY TARGET THEN   
				INSERT (EntityId,GuidKey) VALUES (Source.EntityId,upper(source.GuidKey)) 
	OUTPUT $action, SOURCE.*;

	declare @configurationGroupIdJob int
	select @configurationGroupIdJob = id from ConfigurationGroups where guidkey='CA072D3E-91A2-4031-9FA8-59DE926926D1'

		declare @configurationGroupIdJobParameters int
	select @configurationGroupIdJobParameters = id from ConfigurationGroups where guidkey='499D7842-9FA2-4F55-82FA-B7E4AD2021E4'

	--05: ConfigurationGroupParameters
	--	Fill the group instance
	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES ( @configurationGroupIdJob, 1, N'TeleHealthGeneratePatientMeasurementMonitoringPeriodsTimerJob')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES ( @configurationGroupIdJob, 16, N'TeleHealthGeneratePatientMeasurementMonitoringPeriodsTimerJob')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES ( @configurationGroupIdJob, 17, N'1')

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 1, N'TeleHealthGeneratePatientMeasurementMonitoringPeriodsParameters')	--Name
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 18, N'3600') --Interval 1 hour
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 19, N'1')	--IntervalUnit
--	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 20, N'250')	--BulkSize
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 22, N'')	--DependantProcesses
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 23, N'-206')	--ProcessId
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 80, N'1')	--IsEnabled

	MERGE INTO dbo.ConfigurationGroupParameters AS TARGET  
			USING @ConfigurationGroupParameters AS SOURCE  
			ON TARGET.GroupId  = SOURCE.GroupId
			AND TARGET.ParameterId =  SOURCE.ParameterId
			WHEN MATCHED THEN    
					UPDATE SET Value=source.Value
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( GroupId,ParameterId,Value) VALUES (Source.GroupId, SOURCE.ParameterId,source.Value)  
	OUTPUT $action, SOURCE.*;

	--06: ConfigurationEntityTree
	--	Set the identifier in the tree for the instance
	DECLARE @ConfigurationEntityTree TABLE (EntityTypeId int not null,GroupId int NOT NULL, MachineName nvarchar(max), ServiceName nvarchar(max), ServiceId nvarchar(max), 
											ModuleIdentifier nvarchar(max),ParentEntityId int null,GuidKey uniqueidentifier not null );

	--Job
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) select 4, @configurationGroupIdJob, N'MachineId', N'TeleHealthJobs', N'',N'', NULL, 'E9B9D98A-CCDC-4ADC-8363-3D731E298034'

	MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
			USING @ConfigurationEntityTree AS SOURCE  
			ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
             
			WHEN MATCHED THEN
					UPDATE SET  MachineName=source.MachineName, ServiceName=source.ServiceName, ServiceId=source.ServiceId, 
					ModuleIdentifier=source.ModuleIdentifier, ParentEntityId=source.ParentEntityId
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId,GuidKey) 
					VALUES ( source.EntityTypeId, source.GroupId, source.MachineName, source.ServiceName, source.ServiceId, source.ModuleIdentifier, source.ParentEntityId, upper(source.GuidKey))
	OUTPUT $action, SOURCE.*;


	-- delete values from temp table
	delete from @ConfigurationEntityTree
	
	declare @treeJobId int
	select @treeJobId = id from ConfigurationEntityTree where guidkey='E9B9D98A-CCDC-4ADC-8363-3D731E298034'

	--Job parameters
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) select 5, @configurationGroupIdJobParameters, N'MachineId', N'TeleHealthJobs', N'TeleHealthGeneratePatientMeasurementMonitoringPeriodsTimerJob',N'', @treeJobId, 'D2285F13-B497-4CE1-A993-6AAC51069B83'


		MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
			USING @ConfigurationEntityTree AS SOURCE  
			ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
             
			WHEN MATCHED THEN    
					UPDATE SET  MachineName=source.MachineName, ServiceName=source.ServiceName, ServiceId=source.ServiceId, 
					ModuleIdentifier=source.ModuleIdentifier, ParentEntityId=source.ParentEntityId
			WHEN NOT MATCHED BY TARGET THEN   
					INSERT ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId,GuidKey) 
					VALUES ( source.EntityTypeId, source.GroupId, source.MachineName, source.ServiceName, source.ServiceId, source.ModuleIdentifier, source.ParentEntityId, upper(source.GuidKey))
	OUTPUT $action, SOURCE.*;
