-------------
-- ROLES
-------------
	USE [IHBoxSystem]
	GO
	CREATE ROLE wtr_read AUTHORIZATION dbo 
	GO  

	USE [IHBoxSystem]
	GO
	EXEC sp_addrolemember 'wtr_read', 'admin'
	GO

	USE [IHBoxSystem]
	GO
	EXEC sp_addrolemember 'wtr_read', 'SDI/australtek'
	GO



-------------------
-- Menu and Route
-------------------
	declare @parent int, @mesId int, @result_routeId INT, @weatherSortOrder int
	-- Weather submenu under MES: ensure "Weather" parent exists, then add this as child
	select @mesId = id from [FrontEnd].[Menu] where Parent is null and [Name] = 'MES'
	select @parent = id from [FrontEnd].[Menu] where [Name] = 'Weather' and Parent = @mesId
	if @parent is null
	begin
		select @weatherSortOrder = ISNULL((SELECT MAX([SortOrder]) FROM [FrontEnd].[Menu] WHERE [Parent] = @mesId), 0) + 1
		INSERT INTO [FrontEnd].[Menu]
			   ([Parent], [Name], [RouteID], [SortOrder], [Enabled], [Visible], [RequiredRole], [Group])
		VALUES (@mesId, 'Weather', NULL, @weatherSortOrder, 1, 1, NULL, NULL)
		SET @parent = SCOPE_IDENTITY()
	end

	INSERT INTO [FrontEnd].[Routes]
			   ([Route]
			   ,[ModuleName]
			   ,[ModulePath]
			   ,[RequiredRole]
		   )
		 VALUES 
			   (
			   '!/weather-tracking(/:start)(/:end)(/:heatName)(/)'
			   , 'weather-tracking'
			   , 'pages/mes/weather-tracking/'
			   , 'wtr_read'           
			   )
			   SET @result_routeId = @@IDENTITY
	INSERT INTO [FrontEnd].[Menu]
			   ([Parent]
			   ,[Name]
			   ,[RouteID]
			   ,[SortOrder]
			   ,[Enabled]
			   ,[Visible]
			   ,[RequiredRole]
			   ,[Group]
			  )
		 VALUES
			   (
			   @parent
			   ,'Tracking'
			   ,@result_routeId 
			   ,1
			   ,1
			   ,1
			   ,'wtr_read'
			   ,NULL
			 )
	GO


-------------------
-- SP Permissions
-------------------
EXECUTE sys.sp_addextendedproperty 
@level0type = N'USER' -- The object type
,@level0name = [wtr_read] -- The role
,@name = N'securedsp-MES.GetWeatherTracking' -- the procedure
,@value = N'' 
GO


-------------------
--	Profiles
-------------------
	USE [IHBoxSystem]
	GO

	DECLARE @ModuleId INT

	INSERT INTO [SECURITY].[Modules]([Code],[Name])
	VALUES('wtr_read','Weather Tracking')

	SET @ModuleId = @@IDENTITY


	INSERT INTO [SECURITY].ModuleActions(ModuleId,Code,[Name],[Role])
	VALUES (@ModuleId,'read','Read','wtr_read')





