--[[----------------------------------------------------------------------------

NmbDeletionBacklog.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2022 Adobe
All Rights Reserved.

NOTICE: All information contained herein is, and remains
the property of Adobe and its suppliers, if any. The intellectual
and technical concepts contained herein are proprietary to Adobe
and its suppliers and are protected by all applicable intellectual
property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material
is strictly forbidden unless prior written permission is obtained
from Adobe.

----------------------------------------------------------------------------]]--

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local WFCloseSupport = require 'WFCloseSupport'
local WFWorkerThread = require 'WFWorkerThread'
local KSJsonEncoder = require 'KSJsonEncoder'
local WFRxRunUntilDone = require 'WFRxRunUntilDone'

--------------------------------------------------------------------------------

local runUntilDone = WFRxRunUntilDone

local check = assert( WFArguments.check )


--============================================================================--

-- @mark NmbDeletionBacklog namespace

local NmbDeletionBacklog = {

	_deletionBacklogDatabaseImp = false,
		-- (WFWorkerThread)

	WFCloseSupport.addCloseSupportMethods,	
	
}

--------------------------------------------------------------------------------

--[[!

	\brief Creates a persistent SQLite database for non-syncing persistent data,
		specifically for the use of the deletion backlog.

	\param dbPath (string) The path to the db.

	\return NmbDeletionBacklog instance object
	
--]]

function NmbDeletionBacklog:__init( docStorePath, dbPath, optArgs )

	check( 'NmbDeletionBacklog', '+string', '+string', '?table' )

	-- Only pass in the relevant deletion backlog arguments to reduce the risk
	-- of creating multiple deletion backlog worker threads when there are
	-- multiple calls to create deletion backlog objects.

	local args = optArgs and {
		writeInterval = optArgs.writeInterval,
		processBacklogDelay = optArgs.processBacklogDelay
	}

	local workerThreadArgs = KSJsonEncoder.encodeOzUploadJson( 
								{ 
									docStorePath,
									dbPath,
									args
								} )
								
	local databaseThreadName = "NmbDeletionBacklogMessageProcessor.lua"

	self:setOwned {

		_deletionBacklogDatabaseImp = WFWorkerThread( databaseThreadName, workerThreadArgs )

	}
	
end

--------------------------------------------------------------------------------

function NmbDeletionBacklog:close()
										
	check( 'NmbDeletionBacklog' )
	
end

--------------------------------------------------------------------------------

function NmbDeletionBacklog:haltAndDelete()

	return self._deletionBacklogDatabaseImp:request( 'haltAndDelete' )

end

--==============================================================================

-- Deletion Backlog APIs

--------------------------------------------------------------------------------

function NmbDeletionBacklog:addToDeletionBacklog( volumeGuid, relativePath, localId, binaryType )

	check( 'NmbDeletionBacklog', 'string', 'string', '?number', '?string' )
	
	return self._deletionBacklogDatabaseImp:request( 'addToDeletionBacklog',
			volumeGuid, relativePath, localId, binaryType )
			:call( runUntilDone )
	
end

--------------------------------------------------------------------------------

function NmbDeletionBacklog:removeFromDeletionBacklog( volumeGuid, relativePath )

	check( 'NmbDeletionBacklog', 'string', 'string' )
	
	return self._deletionBacklogDatabaseImp:request( 'removeFromDeletionBacklog', 
			volumeGuid, relativePath )
			:call( runUntilDone )
	
end

--------------------------------------------------------------------------------

function NmbDeletionBacklog:bulkAddToDeletionBacklog( localAssetsToDelete )

	check( 'NmbDeletionBacklog', 'table' )
	return self._deletionBacklogDatabaseImp:request( 'bulkAddToDeletionBacklog', localAssetsToDelete )
			:call( runUntilDone )
	
end

--------------------------------------------------------------------------------

function NmbDeletionBacklog:bringDeletionBacklogUpToDate()

	check( 'NmbDeletionBacklog' )
	return self._deletionBacklogDatabaseImp:request( 'bringDeletionBacklogUpToDate' )
			:call( runUntilDone )
			
end

--------------------------------------------------------------------------------

function NmbDeletionBacklog:processDeletionBacklog()

	check( 'NmbDeletionBacklog' )
	return self._deletionBacklogDatabaseImp:request( 'processDeletionBacklog' )
			:call( runUntilDone )
			
end

--------------------------------------------------------------------------------

--[[!

	\brief TEST USE ONLY - dump the contents of the deletionBacklogV2 table.

	\param dontBringUpToDate (optional boolean) If true, this will not update the DB before duping the contents.
	
	\return Contents of the deletionBacklogV2 table.
	
--]]

function NmbDeletionBacklog:DEBUG_dumpDeletionBacklogContents( dontBringUpToDate )

	check( 'NmbDeletionBacklog', '?boolean' )
	return self._deletionBacklogDatabaseImp:request( 'DEBUG_dumpDeletionBacklogContents', dontBringUpToDate )

end

--------------------------------------------------------------------------------

--[[!

	\brief TEST USE ONLY - dump the contents of the deletionBacklog table.

	\return Contents of the deletionBacklog table.
	
--]]

function NmbDeletionBacklog:DEBUG_dumpDeletionBacklogContentsV1()

	check( 'NmbDeletionBacklog' )
	return self._deletionBacklogDatabaseImp:request( 'DEBUG_dumpDeletionBacklogContentsV1' )

end

--------------------------------------------------------------------------------

--[[!

	\brief TEST USE ONLY
	
--]]

function NmbDeletionBacklog:DEBUG_processedDeletionBacklogStream()

	check( 'NmbDeletionBacklog' )
	return self._deletionBacklogDatabaseImp:request( 'DEBUG_processedDeletionBacklogStream' )
	
end

--------------------------------------------------------------------------------

--[[!

	\brief TEST USE ONLY
	
--]]

function NmbDeletionBacklog:DEBUG_buildDeletionBacklogWhereClause( onlineVolumeGuids )

	check( 'NmbDeletionBacklog', 'table' )
	return self._deletionBacklogDatabaseImp:request( 'DEBUG_buildDeletionBacklogWhereClause', onlineVolumeGuids )

end

--------------------------------------------------------------------------------

return Rx.Class 'NmbDeletionBacklog'( NmbDeletionBacklog )
