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

WFCatSearchByAutoTags.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 WFConfiguration = require 'WFConfiguration'

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

local derefOpt = KSTableUtils.derefOpt

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

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

local WFCatSearchByAutoTags = {}

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

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

function WFCatSearchByAutoTags.generateIndexSchema( indexStoreSchema )

	if WFConfiguration.Features.enableAutoTagsPanel then

		indexStoreSchema:addTable {

			sql = 'CREATE TABLE autoTags( autoTag STRING, confidence INTEGER )',

			useLocalId = true,

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

			map = function( docValue, emit )
				
				local isSearchable, searchableDoc = isValidSearchableAssetDocument( docValue )
				if isSearchable and searchableDoc then

					local autoTags = derefOpt( searchableDoc, 'autoTags' ) or {}

					if autoTags and autoTags.tags then
						for autoTag, confidence in pairs( autoTags.tags ) do
							
							assert( confidence ~= nil, 'nil confidence value of an autotag' )
							
							if type( confidence ) ~= 'number' then -- convert any non number confidence to 1 or 0
								confidence = confidence and 100 or 0
							end
							
							emit( autoTag, confidence )
						end
					end
				end

			end,

		}

		indexStoreSchema:addIndex
			'CREATE INDEX autoTags_autoTag ON autoTags( autoTag, docId )'

	else
	
		-- SPAM [jandrew 2018-05-14]: AutoTag search behavior is online only. No
		-- need to register or index them locally, at least for now.
		
		indexStoreSchema:dropTable( 'autoTags' )
	
	end

end

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

function WFCatSearchByAutoTags.addSearchToSearchSchema( searchSchema )

	if WFConfiguration.Features.enableAutoTagsPanel then

		searchSchema:addSearchableData {
			term = 'autoTags',
			dataType = 'autoTagsTerm',
			sql = 'SELECT docId FROM autoTags WHERE autoTag =? ORDER BY docId',
			query_params = {
				'autoTag',
			},
		}

	end

end

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

return WFCatSearchByAutoTags
