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

WFCatSpacePurgeUtils.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2020 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.utils
local WFArguments = require 'WFArguments'
local WFPlatformInfo = require 'WFPlatformInfo'

	-- WF.oz_client.managed_catalog
local WFCatPurgeHelper = require 'WFCatPurgeHelper'

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

local check = assert( WFArguments.check )

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

local WFCatSpacePurgeUtils = {}

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

-- For a given space, purge specified asset binary types

local function purgeAssetsForSpace( catalog, spaceId, needToUpdateDocValue, keepRedactedBinaries, optDataCache )

	check( 'WFCatalog', 'string', '?boolean', '?boolean', '?KSDataCache' )

	local albumAssetTable = "album_asset"
	local albumIdForAlbumTable = "docId"
	if WFPlatformInfo.isDesktop then
		albumAssetTable = "album_asset_v2"
		albumIdForAlbumTable = "albumId"
	end

	return catalog:indexStore():queryOnce{
			sql = string.format( [[SELECT assetId 
								FROM %s INNER JOIN albums ON %s.albumId == albums.%s
								WHERE albums.spaceId == ?]], albumAssetTable, albumAssetTable, albumIdForAlbumTable ),
			singleColumnQuery = true,
			spaceId
		}
		
		:flatMap( function( assetIds )
		
			if #assetIds == 0 then
				return Observable.just( true )
			end
			
			return WFCatPurgeHelper.deleteAssetFilesByDocIdsForSpace(
					catalog, spaceId, assetIds, needToUpdateDocValue, keepRedactedBinaries, optDataCache )
			
		end ) 

end

WFCatSpacePurgeUtils.DEBUG_purgeAssetsForSpace = purgeAssetsForSpace

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

-- For a given space, purge specified version binary types

local function purgeVersionsForSpace( catalog, spaceId, needToUpdateDocValue, optDataCache )

	check( 'WFCatalog', 'string', '?boolean', '?KSDataCache'  )

	return catalog:indexStore():queryOnce{
			sql = [[SELECT docId FROM versions WHERE spaceId == ?]],
			singleColumnQuery = true,
			spaceId
		}
		
		:flatMap( function( versionIds )
			
			if #versionIds == 0 then
				return Observable.just( true )
			end
			
			return WFCatPurgeHelper.deleteVersionFilesByDocIdsForSpace(
					catalog, spaceId, versionIds, needToUpdateDocValue, nil, optDataCache )
		
		end )

end

WFCatSpacePurgeUtils.DEBUG_purgeVersionsForSpace = purgeVersionsForSpace

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

function WFCatSpacePurgeUtils.purgeAllForSpace( catalog, spaceId, optDataCache )

	check( 'WFCatalog', 'string', '?KSDataCache' )
	
	-- Purge assets and versions for space with needToUpdateDocValue = false and keepRedactedBinaries = false
	-- We would delete space related docs directly from docStore, so there's no
	-- need to update docValue after file deletion
	
	return purgeAssetsForSpace( catalog, spaceId, false, false, optDataCache )
		
		:flatMap( function()
		
			return purgeVersionsForSpace( catalog, spaceId, false, optDataCache )
			
		end )

end

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

function WFCatSpacePurgeUtils.purgeEditForSpace( catalog, spaceId )

	check( 'WFCatalog', 'string' )
	
	-- Purge assets for space with needToUpdateDocValue = true and keepRedactedBinaries = true
	
	return purgeAssetsForSpace( catalog, spaceId, true, true )
		
		:flatMap( function()
		
			-- For desktop, version purge xmp_develop would be handled by PixelProviderFileCachePurgeWorker
			
			if not WFPlatformInfo.isDesktop then
			
				-- Purge version for space with needToUpdateDocValue = true
				-- We would get redated version doc update Oz when role is downgraded, so
				-- we need to update docValue
			
				return purgeVersionsForSpace( catalog, spaceId, true )
			end
			
			return Observable.just( true )
			
		end )

end

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

return WFCatSpacePurgeUtils
