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

WFCatSearchByEdited.lua

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

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.

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

local KSTableUtils = require 'KSTableUtils'
local KSDate = require 'KSDate'
	-- WF.oz_client.managed_catalog
local WFCatIndexUtils = require 'WFCatIndexUtils'

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

local derefOpt = KSTableUtils.derefOpt

local isValidSearchableAssetDocument = assert( require 'WFCatIndexUtils'.isValidSearchableAssetDocument )

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

local WFCatSearchByEdited = {}

--[[
	Index definitions for finding edited assets. The SQLite index only;
	Queries for the above finding assets by edit status.
--]]

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

function WFCatSearchByEdited.generateIndexSchema( indexStoreSchema )

	indexStoreSchema:addTable {

		sql = [[CREATE TABLE editedDate( editedDate, completeTime )]],
		
		relevantTypeSubtypeTuples = {
			{ 'asset', 'image' },
			{ 'asset', 'video' }
		},
		
		map = function( docValue, emit )
			
			local isSearchable, searchableDoc = isValidSearchableAssetDocument( docValue )
			
			if isSearchable and searchableDoc then
			
				local isVideo = WFCatIndexUtils.isVideoDocument( docValue )

				local userUpdated
				
				if isVideo then
					userUpdated = KSDate.getLatestDate(
										-- for videos, the userUpdated field is located in both
										-- adjustments and temporal tabels in the develop section
										derefOpt( searchableDoc, 'develop', 'adjustments', 'userUpdated' ),
										derefOpt( searchableDoc, 'develop', 'temporal', 'userUpdated' )
									)
				else
					userUpdated = derefOpt( searchableDoc, 'develop', 'userUpdated' )
				end
				
				local userUpdatedDateAsTimeInterval
				
				local success
				
				if userUpdated == '0000-00-00T00:00:00' then
					
					-- iso8601ToTimeIntervalSinceReference fails on the zero-date, so use
					-- use a very old default time interval that should come before any
					-- provided capture date.
					
					success = true
					userUpdatedDateAsTimeInterval = -10000000000
					
				elseif userUpdated then
					
					success, userUpdatedDateAsTimeInterval = pcall( KSDate.iso8601ToTimeIntervalSinceReference, userUpdated )
					
				end
				
				if success and userUpdatedDateAsTimeInterval then
					emit( userUpdated, userUpdatedDateAsTimeInterval )
				end
				
			end
			
		end,
		
		useLocalId = true,
		hasOneRowPerDocument = true,
		
	}
	
	indexStoreSchema:addIndex
		[[CREATE INDEX editedDate_complete ON editedDate( completeTime, docId )]]
		
end

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

function WFCatSearchByEdited.addSearchToSearchSchema( searchSchema )
	
	-- Search query for edited assets
	searchSchema:addSearchableData {
		dataType = 'fullTextSearch',
		sql = [[SELECT docId FROM editedDate ORDER BY docId]],
		term = 'edited',
	}

	-- Search query for unedited assets
	searchSchema:addSearchableData {
		dataType = 'fullTextSearch',
		sql = [[SELECT assets.docId
				FROM assets
				LEFT JOIN editedDate ON assets.docId == editedDate.docId
				WHERE assets.subtype IN ( 'image', 'video' ) AND editedDate.docId IS NULL
				ORDER BY assets.docId]],
		term = 'unedited',
	}

end

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

return WFCatSearchByEdited
