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

WFCatSearchByEnhancedTest.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.

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

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.core.utils
local WFCloseSupport = require 'WFCloseSupport'

	-- WF.oz_client.managed_catalog.search
local WFCatSearchQuery = require 'WFCatSearchQuery'
local WFCatSearchSchema = require 'WFCatSearchSchema'

	-- WF.sqlite.doc_store
local WFRxDocStore = require 'WFRxDocStore'

	-- WF.sqlite.index_store
local WFRxIndexStore = require 'WFRxIndexStore'

local WFOzTestUtils = require 'WFOzTestUtils'

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

local WFCatSearchByEnhancedTest = {}

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

local sCoveragePath

function WFCatSearchByEnhancedTest.setUp( inCoveragePath )
	sCoveragePath = inCoveragePath
end

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

local function addEnhanceToAssetFn( )
	local counter = 1

	return function( docValue )
		
		docValue.importSource.originalHeight = counter + 1
		
		-- only mark alternate documents as edited
		if math.mod( counter, 2 ) == 0 then
			docValue.xmp = {
				aux = {
					EnhanceDetailsAlreadyApplied = true
				}
			}
		end
		
		counter = counter + 1
		
		return docValue
	end

end

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

local function makeSearcher( rxTest, docStore, indexStore, searchSchema )
	return function( predicates, optSql )
		local terms = WFCatSearchQuery.createQueryParamsStream( searchSchema, {
			predicates = predicates
		} )

		local query2 = WFCatSearchQuery.privateUnitTestExports.uncalmedResultSetQuery( indexStore,
			[[SELECT docId FROM assets @JOIN ORDER BY height DESC]],
			terms,
			'assets.docId'
		)

		return rxTest:observe( query2 )
	end

end

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

function WFCatSearchByEnhancedTest.rxTestSavedEnhancedSearch( rxTest )

	rxTest:setTimeLimit( 20 )

	local docStore = WFRxDocStore( ':memory:' )
	local indexStore = WFRxIndexStore( docStore, 'WFCatIndexDefinitions.lua' )

	rxTest:finally( function()

		if sCoveragePath and indexStore and not WFCloseSupport.hasBeenOrWillBeClosed( indexStore ) then
			indexStore:DEBUG_dumpCoverage( sCoveragePath )
		end

		indexStore:close()
		indexStore = nil

		docStore:close()
		docStore = nil

	end )

	local searchSchema = WFCatSearchSchema()

	require 'WFCatSearchByEnhanced'.addSearchToSearchSchema( searchSchema )

	local addAssetFn = WFOzTestUtils.v2.makeAdderOfAsset( rxTest, docStore, addEnhanceToAssetFn() )

	for i = 1, 10 do
		addAssetFn( )
	end

	local observeQueryOfTermsFromSavedSearch = makeSearcher( rxTest, docStore, indexStore, searchSchema )

	observeQueryOfTermsFromSavedSearch {
		type = 'enhanced',
	}
	:expectDataWithinTime( 5, { { 10 }, { 8 }, { 6 }, { 4 }, { 2 } } )

end

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

function WFCatSearchByEnhancedTest.rxTestSavedUnenhancedSearch( rxTest )
	
	rxTest:setTimeLimit( 20 )

	local docStore = WFRxDocStore( ':memory:' )
	local indexStore = WFRxIndexStore( docStore, 'WFCatIndexDefinitions.lua' )

	rxTest:finally( function()

		if sCoveragePath and indexStore and not WFCloseSupport.hasBeenOrWillBeClosed( indexStore ) then
			indexStore:DEBUG_dumpCoverage( sCoveragePath )
		end

		indexStore:close()
		indexStore = nil

		docStore:close()
		docStore = nil

	end )

	local searchSchema = WFCatSearchSchema()

	require 'WFCatSearchByEnhanced'.addSearchToSearchSchema( searchSchema )

	local addAssetFn = WFOzTestUtils.v2.makeAdderOfAsset( rxTest, docStore, addEnhanceToAssetFn() )

	for i = 1, 10 do
		addAssetFn( )
	end

	local observeQueryOfTermsFromSavedSearch = makeSearcher( rxTest, docStore, indexStore, searchSchema )

	observeQueryOfTermsFromSavedSearch {
		type = 'unenhanced',
	}
	:expectDataWithinTime( 5, { { 9 }, { 7 }, { 5 }, { 3 }, { 1 } } )

end

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

return WFRxTestCase.wrapSuite( 'WFCatSearchByEnhancedTest', WFCatSearchByEnhancedTest )
