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

WFCatSearchByReview.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 derefOpt = KSTableUtils.derefOpt

local isValidSearchableAssetDocument = assert( require 'WFCatIndexUtils'.isValidSearchableAssetDocument )
local NO_USER = "NO_USER"
--------------------------------------------------------------------------------

local kFlagEncoding = {
	reject = "1",
	unflagged = "2",
	pick = "3",
}


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

local WFCatSearchByReview = {}

--[[
	Index definitions for finding assets by review.
	Queries for the above finding assets by date.
--]]

--------------------------------------------------------------------------------
function WFCatSearchByReview.getValueForReviewStatus(reviewStatus)
	return kFlagEncoding[reviewStatus]
end


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

function WFCatSearchByReview.generateIndexSchema( indexStoreSchema )

	indexStoreSchema:addTable {

		sql = [[CREATE TABLE reviews( flag, userId )]],

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

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

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

				local reviewInfoPresent = false
				for user, reviewT in pairs( reviews ) do
					reviewInfoPresent = true
					local flag = derefOpt( reviewT, 'flag' )
					flag = kFlagEncoding[ flag ] or kFlagEncoding.unflagged
					emit( flag, user )
					--breaking due to  https://jira.corp.adobe.com/browse/NMB-1700
					break
				end
				
				-- since currently we don't have user based review
				-- adding a temp string for the user representation
				if reviewInfoPresent == false then
					emit( kFlagEncoding.unflagged, NO_USER )
				end
			end

		end,

	}

	indexStoreSchema:addIndex
		[[CREATE INDEX reviews_userId_flag ON reviews( userId, flag, docId )]]

	indexStoreSchema:addIndex
		[[CREATE INDEX reviews_userId_docId ON reviews( userId, docId, flag )]]

end

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

function WFCatSearchByReview.addSearchToSearchSchema1( searchSchema )

	searchSchema:addSearchableData {
		term = 'review',
		dataType = 'reviewTerm',
		sql = [[SELECT docId FROM reviews WHERE userId = ? AND flag %OPERATOR ? ORDER BY docId]],
		query_params = {
			'user',
			'flag',
		},
		values = {
			flag = kFlagEncoding,
		},
	}

end





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

function WFCatSearchByReview.addSearchToSearchSchema( searchSchema )

	searchSchema:addSearchableData {
		term = 'review',
		dataType = 'reviewTerm',
		sql = [[SELECT docId FROM reviews WHERE flag IN (?,?,?) ORDER BY docId]],
		query_params = {
			'flag',
		},
	}

	
end
--==============================================================================

return WFCatSearchByReview
