 /*****************************************************************************
   Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
       Copyright 2026 - Volian Enterprises, Inc. All rights reserved.
*****************************************************************************/

-- =============================================
-- Author:		Matthew Schill
-- Create date: 03/20/2026
-- Description:	Script to consolidate Cover Pages for Barakah
--  by Converting multi-unit procedures with Cover Pages
--  to use 1 Library Document Cover Page
-- =============================================

 select Contents.ContentID
 	  , Contenttext = Contents.Text
	  , Items.ItemID
 	  , tblDocuments.DocID
 	  , ParentContentID
 	  , ParentItemID
 	  , LibTitle = ISNULL(LibTitle,'')
 	  , numLibCP
 	  , numCP
 	  ,BaseFlag = 0
 INTO #tmpUpdate
 from Contents 
 inner join Entries on Contents.ContentID = Entries.ContentID
 inner join tblDocuments on tblDocuments.DocID = Entries.DocID
 inner join Items on Items.ContentID = Contents.ContentID
 outer apply
 (select ParentContentID=ContentID, ParentItemID = PItm.ItemID
  FROM dbo.vefn_ParentItems(Items.ItemID) PItm
  where PItm.ItemID <> Items.ItemID
 ) parent
 outer apply
 (select numLibCP = Count(*)
  FROM dbo.vefn_ChildItems(ParentItemID) PItm
  INNER JOIN Contents on Contents.ContentID = PItm.ContentID
  INNER JOIN Entries on Contents.ContentID = Entries.ContentID
  INNER JOIN tblDocuments on tblDocuments.DocID = Entries.DocID
  where PItm.ItemID <> ParentItemID
  AND Contents.text like 'Cover Page%' and ISNULL(tblDocuments.LibTitle,'') <> ''
 ) childWithLibTitle
 outer apply
 (select numCP = Count(*)
  FROM dbo.vefn_ChildItems(ParentItemID) PItm
  INNER JOIN Contents on Contents.ContentID = PItm.ContentID
  where PItm.ItemID <> ParentItemID
  AND Contents.text like 'Cover Page%'
 ) child
 where Contents.text like 'Cover Page%'
 order by ParentContentID asc, CASE WHEN ISNULL(LibTitle,'') <> '' THEN 1 ELSE 2 END asc, Contents.Text asc

 UPDATE #tmpUpdate SET BaseFlag = 1 where LibTitle <> '' and numLibCP = 1

 UPDATE #tmpUpdate SET BaseFlag = CASE WHEN tU.LibTitle <> '' THEN 1 ELSE 2 END FROM #tmpUpdate tU
 where BaseFlag = 0 AND tU.ContentID IN
 (
 Select ContentID FROM
 (SELECT sub.ContentID,
        row_number() OVER(PARTITION BY sub.ParentContentID ORDER BY CASE WHEN ISNULL(sub.LibTitle,'') <> '' THEN 1 ELSE 2 END asc, sub.Contenttext asc) as pos
      FROM #tmpUpdate sub
     ) x
 WHERE x.pos = 1
 )

 declare @Cont TABLE
 (
 	ContentID int,
 	ItemID int,
 	xConfig xml
 )
 insert into @Cont
 SELECT tU.ContentID, ItemID, xConfig = CAST(tblContents.config AS xml) FROM 
 tblContents
 INNER JOIN 
 #tmpUpdate tU ON tU.ContentID = tblContents.ContentID
 where tU.BaseFlag > 0

 Update @Cont Set  xConfig.modify('delete //MasterSlave') From @Cont;

 	Update tblContents SET Text = 'Cover Page', Config = CAST(xConfig AS varchar(max)), 
 	   DTS = GETDATE(), UserID = 'CPVolian2026'
 	FROM 
 	@Cont CNT INNER JOIN 
 	tblContents ON CNT.ContentID = tblContents.ContentID;
 
    --Update items PreviousIds
	UPDATE tblItems Set PreviousID = IdToSwapTO.ItemID
	FROM
	tblItems
	INNER JOIN
    #tmpUpdate tU ON tblItems.PreviousID = tU.ItemID AND tU.BaseFlag = 0
 	INNER JOIN #tmpUpdate IdToSwapTO ON IdToSwapTO.ParentContentID = tU.ParentContentID AND IdToSwapTO.BaseFlag IN (1,2) 

 	UPDATE tblItems Set DeleteStatus = 1, DTS = GETDATE(), UserID = 'CPVolian2026'
 	FROM
 	#tmpUpdate tU INNER JOIN 
 	tblItems ON tU.ContentID = tblItems.ContentID
 	WHERE tU.BaseFlag = 0;
 
 	UPDATE tblContents Set DeleteStatus = 1, DTS = GETDATE(), UserID = 'CPVolian2026'
 	FROM
 	#tmpUpdate tU INNER JOIN 
 	tblContents ON tU.ContentID = tblContents.ContentID
 	WHERE tU.BaseFlag = 0;

 	DELETE FROM
	tblEntries
	FROM
	tblEntries
	INNER JOIN 
 	#tmpUpdate tU ON tU.ContentID = tblEntries.ContentID
 	WHERE tU.BaseFlag in (0,2);
 
 	INSERT INTO [dbo].[tblEntries]
 			   ([ContentID]
 			   ,[DocID]
 			   ,[DTS]
 			   ,[UserID]
 			   ,[DeleteStatus])
 	SELECT
 		DISTINCT tU.ContentID,
 		766,  -- docid 766 "Cover Page 1"
 		GETDATE(),
 		'CPVolian2026',
 		0
 	FROM
 	#tmpUpdate tU 
 	INNER JOIN 
 	@Cont CNT ON tU.ContentID = CNT.ContentID
 	WHERE tU.BaseFlag = 2;

 drop table #tmpUpdate;

IF (@@Error = 0) SELECT '[Barakah Cover Page Consolidation] Succeeded'
ELSE SELECT '[Barakah Cover Page Consolidation] Error'
go










