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

WFCatSearchByKeywords.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated 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 Systems Incorporated.

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

local KSTableUtils = require 'KSTableUtils'
local KSStringUtils = require 'KSStringUtils'

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

local derefOpt = KSTableUtils.derefOpt

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


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

local WFCatSearchByKeywords = {}

--[[
	Index definitions for finding assets by keywords.  The SQLite index only;
		the table definition is shared (``asset`` table).
	Queries for the above finding assets by date.
--]]

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

function WFCatSearchByKeywords.generateIndexSchema( indexStoreSchema )

	indexStoreSchema:addTable {

		sql = [[CREATE TABLE keywords( keyword TEXT, keywordLC TEXT, isCatalogDocument )]],

		useLocalId = true,
		
		relevantTypeSubtypeTuples = {
			{ 'asset', '*' }
		},

		map = function( docValue, emit )

			local isSearchable, searchableDoc = isValidSearchableAssetDocument( docValue )
			if isSearchable and searchableDoc then
				
				local isCatalogDocument = derefOpt( searchableDoc, "_spaceId" ) and 0 or 1
				
				local keywords = derefOpt( searchableDoc, 'xmp', 'dc', 'subject' ) or {}
				
				for keyword, _ in pairs( keywords ) do
				
					local keywordLC = ''
					
					if keyword then
				
						keywordLC = KSStringUtils.lower( keyword )
						keywordLC = KSStringUtils.trim( keywordLC )
						
					end
				
					emit( keyword, keywordLC, isCatalogDocument )
					
				end
				
			end

		end,

	}

	indexStoreSchema:addIndex
		[[CREATE INDEX keywords_keyword ON keywords( keywordLC, docId )]]
	indexStoreSchema:addIndex 
		[[CREATE INDEX keywords_keywordLC ON keywords( keywordLC COLLATE i18n_collate )]]
	indexStoreSchema:addIndex 
		[[CREATE INDEX keywords_isCatalogDocument ON keywords( isCatalogDocument )]]
		
end

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

function WFCatSearchByKeywords.addSearchToSearchSchema( searchSchema )

	searchSchema:addSearchableData {
		term = 'keywords',
		dataType = 'keywordsTerm',
		sql = [[SELECT docId FROM keywords WHERE keywordLC = LOWER(?) ORDER BY docId]],
		query_params = {
			'keyword',
		},
	}

end

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

function WFCatSearchByKeywords.collectKeywordDetailsOfRequiredAssets( wfCatalog, assetIds )
	
	return wfCatalog:indexStore():query {
		sql = [[SELECT DISTINCT keywords.keywordLC
						FROM keywords  
						INNER JOIN TEMP_ids USING( docId )
						ORDER BY keywordLC COLLATE i18n_collate ASC]],
		tempIds = assetIds,
		calm = 5,
	}

end

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

function WFCatSearchByKeywords.collectLCKeywordDetailsOfAllAssets( wfCatalog )
	
	return wfCatalog:indexStore():query {
		sql = [[SELECT DISTINCT keywords.keywordLC
						FROM keywords 
						WHERE isCatalogDocument = 1
						ORDER BY keywordLC COLLATE i18n_collate ASC]],
		calm = 5,
	}
	
end

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

function WFCatSearchByKeywords.collectKeywordDetailsOfAllAssets( wfCatalog )
	
	return wfCatalog:indexStore():query {
		sql = [[SELECT DISTINCT keyword
						FROM keywords 
						WHERE isCatalogDocument = 1
						ORDER BY keyword COLLATE i18n_collate ASC]],
		calm = 5,
	}
	
end

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

return WFCatSearchByKeywords
