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

ADOBE CONFIDENTIAL
------------------
Copyright 2021 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'
local Observable = Rx.Observable

	-- WF.core.models
local WFModelsUtils = require 'WFModelsUtils'

	-- WF.core.utils
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.oz_client.managed_catalog
local WFOzDocUtils = require 'WFOzDocUtils'
local WFCatStuckSyncUtils = require 'WFCatStuckSyncUtils'
local WFRxDocStoreUtils = require 'WFRxDocStoreUtils'

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

local LrmSyncInfoModelImp = {}

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

--[[! This model that can be used gather real-time sync diagnostic data.

	Example response structure:
		
	{
	
		metadataSyncDiagnostics = {
		
			catalogPullerSyncDiagnostics = { < diagnostic data > },
			
			catalogPusherSyncDiagnostics = { < diagnostic data > },
			
			spaceFeedsManagerSyncDiagnostics = {
			
				spacePusherSyncDiagnostics = { < diagnostic data > },
				
				feedOfFeedsPullerSyncDiagnostics = { < diagnostic data > },
				
				spacePullersSyncDiagnostics = { 
				
					{ < space 1 diagnostic data > },
					
					{ < space 2 diagnostic data > },
					
					...
				}
				
			}
			
		},
		
		binaryUploaderSyncDiagnostics = { < diagnostic data > }
		
	}
	
	For more information on the diagnostic data, please see the syncDiagnostics
	functions with the associated sync classes.

--]]

function LrmSyncInfoModelImp:syncDiagnostics( optCalm )

	local syncClient = self.catalog:getSyncClient()

	return {
		value = syncClient:syncDiagnostics( optCalm )
	}

end

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

-- Get a stream that returns asset document IDs that the system believes are in
-- a stuck sync state due to non-uploaded metadata.

function LrmSyncInfoModelImp:getDocsStuckSyncingMetadataStream( optCalm, optStuckReportingTimeThreshold )

	local syncClient = self.catalog:getSyncClient()

	local isNormalSyncRunningStream = syncClient:isNormalSyncRunningStream()

	return WFModelsUtils.createModelForFunction( self._wfCatalogStream, 
			WFCatStuckSyncUtils.getDocsStuckSyncingMetadataStream, isNormalSyncRunningStream, optCalm, optStuckReportingTimeThreshold )

end

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

-- The following function resolves to `true` if all of the assets
-- are fully synced with Oz, and there are no pending local changes
-- that have yet to sync. Otherwise, it resolves to false.

function LrmSyncInfoModelImp:areAssetsFullySyncedWithOz( assetIds )

	return WFModelsUtils.createModelForFunction( self._wfCatalogStream,
			WFOzDocUtils.areAssetsFullySyncedWithOz, assetIds )

end

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

-- The following function resolves to `true` once all of the assets
-- are fully synced with Oz.

function LrmSyncInfoModelImp:waitUntilAssetsAreFullySyncedWithOz( assetIds )

	return WFModelsUtils.createModelForFunction( self._wfCatalogStream,
			WFOzDocUtils.waitUntilAssetsAreFullySyncedWithOz, assetIds )

end

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

--[[!
	\brief Dump documents that are currently syncing or have sync errors to a new SQLite database file.

	\param dbPath (string) The path where the new SQLite database file should be created.
		If a file already exists at this path, a unique name will be generated.

	\return <code>WF Model</code> Resolves to the path of the created SQLite database file
		containing the syncing documents.
--]]

function LrmSyncInfoModelImp:dumpSyncingDocsToPath( dbPath )

	return WFModelsUtils.createModelForFunction( self._wfCatalogStream,
			WFRxDocStoreUtils.dumpSyncingDocsToPath, dbPath )

end

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

--[[!

	\brief Clears all error states and retries sync for a list of assets.

	\param assetIds (table) A list of document IDs for assets that need their errors cleared
		and sync retried.

	\return <code>Model</code> A model that will execute the error clearing and sync retry
		operation when subscribed to.

--]]

function LrmSyncInfoModelImp:clearErrorsAndRetrySync( assetIds )

	return WFModelsUtils.createModelForFunction( self._wfCatalogStream,
			WFCatStuckSyncUtils.clearErrorsAndRetrySync, assetIds )

end

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

-- Returns a model that fires with the current WF Framework version number.

function LrmSyncInfoModelImp:getWFFrameworkVersion()

	local frameworkVersion = WFPlatformInfo.getFrameworkVersion()

	return {
		value = Observable.just( frameworkVersion )
	}

end

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

--[[!
	\brief Prioritize sync for a specific space.
	
	This function pauses the normal catalog sync, triggers immediate sync for the
	specified space, and resumes normal sync after completion or timeout (30 seconds max).
	This is a fire-and-forget operation that returns immediately.
	
	\param spaceId (string) The ID of the space to prioritize for syncing
	
	\return <code>Model</code> A model that resolves immediately after triggering
		the space sync prioritization
--]]

function LrmSyncInfoModelImp:prioritizeSpaceSync( spaceId )

	local syncClient = self.catalog:getSyncClient()

	return {
		value = syncClient:prioritizeSpaceSync( spaceId )
	}

end

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

return LrmSyncInfoModelImp
