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

WFCatSearchByRating.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 WFCatSearchByRating = {}

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

--------------------------------------------------------------------------------
local defaultRating  = 0
local defaultUser = "NO_USER"
function WFCatSearchByRating.generateIndexSchema( indexStoreSchema )

	indexStoreSchema:addTable {

		sql = [[CREATE TABLE ratings( rating INTEGER, userId, captureDate, filenameLC )]],

		useLocalId = true,
		
		relevantTypeSubtypeTuples = {
			{ 'asset', '*' }
		},
	
		map = function( docValue, emit )

			local isSearchable, searchableDoc = isValidSearchableAssetDocument( docValue )
			if isSearchable and searchableDoc then
				
				local captureDate = derefOpt( searchableDoc, 'captureDate' )
				local filename = derefOpt( searchableDoc, 'importSource', 'fileName' )
				
				local filenameLC = ""
				
				if filename and type( filename ) == "string" then
				
					filenameLC = KSStringUtils.lower( filename )
					filenameLC = KSStringUtils.trim( filenameLC )
					
				end
				
				local ratings = derefOpt( searchableDoc, 'ratings' ) or {}
				
				local ratingPresent = false

				for user, ratingT in pairs( ratings ) do
					local rating = derefOpt( ratingT, 'rating' )
					if rating then
						ratingPresent = true
						emit( rating, user, captureDate, filenameLC )
						--breaking due to https://jira.corp.adobe.com/browse/NMB-1700
						break
					end
				end
					
				if ratingPresent == false then
					--https://jira.corp.adobe.com/browse/NMB-703
					emit( defaultRating, defaultUser, captureDate, filenameLC )
				end
				
			end

		end,

	}

	indexStoreSchema:addIndex
		[[CREATE INDEX ratings_userId_rating ON ratings( userId, rating, docId )]]

	indexStoreSchema:addIndex
		[[CREATE INDEX ratings_userId_docId_rating ON ratings( userId, docId, rating )]]

	indexStoreSchema:addIndex
		[[CREATE INDEX ratings_ratingCaptureDateFilenameLCDocId ON ratings( rating, captureDate, filenameLC COLLATE i18n_collate, docId )]]

end

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

function WFCatSearchByRating.addSearchToSearchSchemaWithUserId( searchSchema )

	searchSchema:addSearchableData {
		term = 'rating',
		dataType = 'ratingTerm',
		sql = [[SELECT docId FROM ratings WHERE userId = ? AND rating %OPERATOR ? ORDER BY docId]],
		query_params = {
			'user',
			'rating',
		},
	}

end


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

function WFCatSearchByRating.addSearchToSearchSchema( searchSchema )

	searchSchema:addSearchableData {
		term = 'rating',
		dataType = 'ratingTerm',
		sql = [[SELECT docId FROM ratings WHERE rating %OPERATOR ? ORDER BY docId]],
		query_params = {
			'rating',
		},
	}

end


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

return WFCatSearchByRating
