--Configuration Tables - add PriorityConfiguration parameters:
	--01
	--PriorityConfiguration ENTITY:
	DECLARE @ConfigurationEntityTypes TABLE ([Id] [int] NOT NULL,[Title] [nvarchar](max) NOT NULL);

	INSERT into @ConfigurationEntityTypes ([Id], [Title]) VALUES (17, N'PriorityConfiguration');

	--select * from  dbo.[ConfigurationEntityTypes]
	SET IDENTITY_INSERT [dbo].[ConfigurationEntityTypes] ON;

		MERGE INTO dbo.[ConfigurationEntityTypes] AS TARGET  
			USING @ConfigurationEntityTypes AS SOURCE  
			ON TARGET.Id = SOURCE.Id	
			WHEN MATCHED THEN    
				UPDATE SET Title = SOURCE.Title
			WHEN NOT MATCHED BY TARGET THEN   
				INSERT ([Id], [Title]) VALUES (SOURCE.Id, SOURCE.Title)  
		OUTPUT $action, SOURCE.*;
		
	SET IDENTITY_INSERT [dbo].[ConfigurationEntityTypes] OFF;
    
	---------------------------------------------------------------
	---------------------------------------------------------------
	--03 define parameters for entity PriorityConfiguration (16)
	
	DECLARE @ConfigurationEntityParameters TABLE (Id int NOT NULL,EntityId int NOT NULL,ParameterId int not null);

	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 116, 17, 17) --IsActive
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 117, 17, 23) --ProcessId
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 118, 17, 78) --Method
							 		
	MERGE INTO dbo.ConfigurationEntityParameters AS TARGET  							 
		USING @ConfigurationEntityParameters AS SOURCE  								 
		ON TARGET.Id = SOURCE.Id
		WHEN MATCHED THEN    
			UPDATE SET entityid = SOURCE.entityid,ParameterId=source.ParameterId 
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (Id, EntityId,ParameterId) VALUES (SOURCE.Id,Source.EntityId, SOURCE.ParameterId)  
	OUTPUT $action, SOURCE.*;


	---------------------------------------------------------------
	---------------------------------------------------------------
	--04 - create 3 groups for TimeoutConfiguration Entity
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );
	
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (16, '193DC925-5C0D-4DDB-BA50-406287695303') --UdpEthernet 
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (16, '71EAD0EE-8FBE-47ED-A054-723A8A49E474') --UdpCellular
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (16, 'C824704C-8A66-4327-916E-A0B4BA0A5A36') --Sms

	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.*;
	
	---------------------------------------------------------------
	---------------------------------------------------------------
	--05 - create 3 groups for Affiliated Entity		
					
	--UdpEthernet
	declare @configurationGroupIdUdpEthernet int
	select @configurationGroupIdUdpEthernet = id from ConfigurationGroups where guidkey='193DC925-5C0D-4DDB-BA50-406287695303'
		
	--UdpCellular
	declare @configurationGroupIdUdpCellular int
	select @configurationGroupIdUdpCellular = id from ConfigurationGroups where guidkey='71EAD0EE-8FBE-47ED-A054-723A8A49E474'

	--SMS
	declare @configurationGroupIdSms int
	select @configurationGroupIdSms = id from ConfigurationGroups where guidkey='C824704C-8A66-4327-916E-A0B4BA0A5A36'

	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));
	
	--UdpEthernet
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpEthernet,17,  N'1'	-- IsActive
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpEthernet,23,  N'1'	-- ProcessId- used for priority
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpEthernet,78,  N'UdpEthernet'	-- Method

	--UdpCellular
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpCellular,17,  N'1'	-- IsActive
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpCellular,23,  N'2'	-- ProcessId- used for priority
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpCellular,78,  N'UdpCellular'	-- Method

	--SMS
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdSms,17,  N'1'	-- IsActive
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdSms,23,  N'3'	-- ProcessId- used for priority
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdSms,78,  N'Sms'	-- Method

	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 );

	--UdpEthernet
	INSERT into @ConfigurationEntityTree (EntityTypeId, GroupId,						  MachineName,  ServiceName,			ServiceId,ModuleIdentifier,	ParentEntityId,	GuidKey) 
	select									17,			@configurationGroupIdUdpEthernet, N'MachineId', N'PriorityConfiguration', N'',	  N'PanelCommandSending', NULL,			'D0D81431-A0A9-472C-875B-ED360E4DCEF7'

	--UdpCellular
	INSERT into @ConfigurationEntityTree (EntityTypeId, GroupId,						  MachineName,  ServiceName,	 ServiceId,  ModuleIdentifier, ParentEntityId, GuidKey) 
	select									17,			@configurationGroupIdUdpCellular, N'MachineId', N'PriorityConfiguration', N'',N'PanelCommandSending', NULL,			'A0909916-BE10-4DE1-8450-E4CBA7916C94'
	
	--SMS
	INSERT into @ConfigurationEntityTree (EntityTypeId, GroupId,				 MachineName,  ServiceName,				ServiceId,	ModuleIdentifier, ParentEntityId, GuidKey) 
	select									17,			@configurationGroupIdSms,N'MachineId', N'PriorityConfiguration', N'',		N'PanelCommandSending', NULL,			  'EF610B4C-CCB3-4F21-877C-E312357D3E8A'
	
	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.*;