-- select * from   mnrRepBranchesStatements
Create OR ALTER PROC [dbo].[mnrPrcAddSubsecriber]
                            @Login       NVARCHAR(215)='helen',
							@Password    NVARCHAR(215)='1'
AS
    declare @ID uniqueidentifier 
     SET @ID = NEWID()

			IF EXISTS(SELECT TOP 1 id FROM mnrRepCurrentProp)
			BEGIN
				SELECT @ID = id FROM mnrRepCurrentProp;

				 delete from mnrRepCurrentProp;
			END

	if NOT EXISTS( select top 1 id from  mnrRepCurrentProp )
	BEGIN
		insert into mnrRepCurrentProp (ID , Name , LoginName , Pwd,Dir,IsRun )
		values ( @ID,'ManaraWeb_Main',@Login,@Password, 0,0  )
		

		--select * from mnrRepCurrentProp
	END

	-- delete  mnrrepsubscribers
	declare @Msg varchar(500) 
    declare @SubscriberID uniqueidentifier 
     

			IF NOT EXISTS(SELECT TOP 1 * FROM mnrrepsubscribers)
			begin
			   SET @SubscriberID = NEWID()
			end
			else
			BEGIN
				SELECT @SubscriberID = id FROM mnrrepsubscribers;

				 delete from mnrRepBranchesStatements where  SubscriberID=@SubscriberID;
				 delete from mnrrepsubscribers;
				  
				 
			END

			print @SubscriberID;
   
	
	-- check subscriber existence 
	if NOT EXISTS( select top 1 * from mnrrepsubscribers )
	BEGIN
		-- Insert into Subscribers table
		insert into mnrrepsubscribers
		(ID ,Number, Name , FilePathIn , FilePAthOut,PointExport,TargetFilePathIn,TargetFilePathOut,TblDirty,FailPoint )
		values (@SubscriberID,1 , 'ManaraWeb_Subsecriber' , 'C:\rep\in' ,'C:\rep\out',0, 'C:\rep\in' ,'C:\rep\out',1,0  )
 
		-- Insert related branch statements for this subscriber
		insert into mnrRepBranchesStatements 
		(Number ,mnrRepTableMainID ,  Dir , SubscriberID, ID,Name,Condition,ActionBeforExport,ActionAfterExport,ActionBeforImport,ActionAfterImport )
		select Number, ID , 0, @SubscriberID, NEWID(),'','','','','',''
		from [dbo].[mnrRepTableMain] ORDER BY Number
 
		SET @Msg = 'Subscriber and branch statements inserted successfully.'
 
		-- Select @Msg
 
	END
	ELSE
	BEGIN
        SET @Msg = 'Load skipped: mnrRepSubscribers already contains data.'
		-- Select @Msg
    END
 
 -- #END#