/*
Configuration Tables
Add configuration for Users policy
*/
--01 - entity type
	DECLARE @ConfigurationEntityTypes TABLE ([Id] [int] NOT NULL,[Title] [nvarchar](max) NOT NULL);

	INSERT into @ConfigurationEntityTypes ([Id], [Title]) VALUES (10, N'RulePolicy');
	INSERT into @ConfigurationEntityTypes ([Id], [Title]) VALUES (11, N'RulePolicyDefinitionType');
	INSERT into @ConfigurationEntityTypes ([Id], [Title]) VALUES (12, N'RulePolicyRuleType');

	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;

--02 - parameter types
	DECLARE @ConfigurationParameterTypes TABLE (Id int NOT NULL,Title nvarchar(max) NOT NULL);

	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (43, N'MaximumNumberOfRules')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (44, N'MaximumNumberOfPeriods')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (45, N'Manual')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (46, N'Calculated')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (47, N'Wandering')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (48, N'OutOfHome')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (49, N'UnexpectedEntryExit')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (50, N'DoorOpen')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (51, N'Presence')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (52, N'ExcessiveNumOfDetections')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (53, N'UnexpectedPresence')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (54, N'HighNumOfVisits')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (55, N'LongStay')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (56, N'ShortStay')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (57, N'ShortStayBedroom')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (58, N'LowNumOfVisits')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (59, N'LowNumOfDetections')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (60, N'NoActivityDetected')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (61, N'Absent')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (62, N'Inactivity')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (63, N'SustainedActivity')


	SET IDENTITY_INSERT dbo.ConfigurationParameterTypes ON

	MERGE INTO dbo.ConfigurationParameterTypes AS TARGET  
		USING @ConfigurationParameterTypes 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.ConfigurationParameterTypes OFF

--03- group parameters
--	RulePolicy
	DECLARE @ConfigurationEntityParameters TABLE (Id int NOT NULL,EntityId int NOT NULL,ParameterId int not null);

	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (61, 10, 1)	--Name
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (62, 10, 43)	--MaximumNumberOfRules
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (63, 10, 44)	--MaximumNumberOfPeriods


	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.*;

--	RulePolicyDefinitionType - 
--	for rule/period- distinguished by Type parameter value
	delete from @ConfigurationEntityParameters

	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (64, 11, 1)	--Name
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (65, 11, 7)	--Type
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (66, 11, 39)	--Group
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (67, 11, 45)	--Manual
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (68, 11, 46)	--Calculated

	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.*;

--	RulePolicyRuleType
	delete from @ConfigurationEntityParameters

	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (69, 12, 1)	--Name
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (70, 12, 39)	--Group
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (71, 12, 47)  --Wandering
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (72, 12, 48)  --OutOfHome
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (73, 12, 49)  --UnexpectedEntryExit
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (74, 12, 50)  --DoorOpen
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (75, 12, 51)  --Presence
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (76, 12, 52)  --ExcessiveNumOfDetections
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (77, 12, 53)  --UnexpectedPresence
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (78, 12, 54)  --HighNumOfVisits
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (79, 12, 55)  --LongStay
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (80, 12, 56)  --ShortStay
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (81, 12, 57)  --ShortStayBedroom
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (82, 12, 58)  --LowNumOfVisits
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (83, 12, 59)  --LowNumOfDetections
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (84, 12, 60)  --NoActivityDetected
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (85, 12, 61)  --Absent
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (86, 12, 62)  --Inactivity
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES (87, 12, 63)  --SustainedActivity


	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.*;

