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

WFCatSearchByEnhanced.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 WFConfiguration = require 'WFConfiguration'
local WFPlatformInfo = require 'WFPlatformInfo'

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

local derefOpt = KSTableUtils.derefOpt

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

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

local WFCatSearchByEnhanced = {}

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

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

function WFCatSearchByEnhanced.generateIndexSchema( indexStoreSchema )

	if WFConfiguration.Shared.enableEnhancedFilter or WFPlatformInfo.isUnitTest() then
	
		indexStoreSchema:addTable {

			sql = [[CREATE TABLE assetEnhanced( enhanced )]],
			
			relevantTypeSubtypeTuples = {
				{ 'asset', 'image' },
				{ 'asset', 'video' }
			},
			
			map = function( docValue, emit )
				
				local isSearchable, searchableDoc = isValidSearchableAssetDocument( docValue )
				
				if isSearchable and searchableDoc then
				
					local isEnhanced = derefOpt( docValue, 'xmp', 'aux', 'EnhanceDetailsAlreadyApplied' )
					
					if isEnhanced then
						emit( isEnhanced )
					end
					
				end
				
			end,
			
			useLocalId = true,
			hasOneRowPerDocument = true,
			
		}
	end
	
end

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

function WFCatSearchByEnhanced.addSearchToSearchSchema( searchSchema )
	
	if WFConfiguration.Shared.enableEnhancedFilter or WFPlatformInfo.isUnitTest() then
	
		-- Search query for enhanced assets
		searchSchema:addSearchableData {
			dataType = 'fullTextSearch',
			sql = [[SELECT docId FROM assetEnhanced ORDER BY docId]],
			term = 'enhanced',
		}

		-- Search query for unenhanced assets
		searchSchema:addSearchableData {
			dataType = 'fullTextSearch',
			sql = [[SELECT assets.docId
					FROM assets
					WHERE assets.subtype IN ( 'image', 'video' )
					EXCEPT
					SELECT assetEnhanced.docId
					FROM assetEnhanced
					ORDER BY assets.docId]],
			term = 'unenhanced',
		}
	
	end
	
end

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

return WFCatSearchByEnhanced
