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

NmbPresetDocumentProcessor.lua

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

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

NOTICE: All information contained herein is, and remain 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 may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

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

local WFArguments = require 'WFArguments'
local KSTableUtils = require 'KSTableUtils'
local WFCatFileSpec = require 'WFCatFileSpec'

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

local check = assert( WFArguments.check )
local derefOpt = assert( KSTableUtils.derefOpt )

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

local function isOriginalLinkInvalid( docValue )

	return derefOpt( docValue, '_serverProvidedMetadata', 'links', '/rels/master', 'invalid' )

end
--------------------------------------------------------------------------------

local NmbPresetDocumentProcessor = {}

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

function NmbPresetDocumentProcessor.generateIndexSchema( indexStoreSchema )

        check( 'WFRxIndexStoreSchema' )

        indexStoreSchema:addTable {
		
                sql = [[CREATE TABLE presetsAndProfiles( name, waitingForBinary, ozHasMaster )]],
				
				relevantTypeSubtypeTuples = {
					{ 'asset', 'preset' },
					{ 'asset', 'profile' },
				},

                map = function( docValue, emit )
				
						if not docValue._deleted == true and docValue.type == "asset" 
							and ( docValue.subtype == "preset" or docValue.subtype == "profile" ) then
						
							local ozSha256 = KSTableUtils.derefOpt( docValue, "importSource", "sha256" )
							local localSha256 = KSTableUtils.derefOpt( docValue, "_localOnly", "files", "original", "master", "sha256" )

--							local volumeGuid = KSTableUtils.derefOpt( docValue, "_localOnly", "files", "original", "master", "volumeGuid" )
--							local relativePath = KSTableUtils.derefOpt( docValue, "_localOnly", "files", "original", "master", "relativePath" )
--							
--							local fullPath = relativePath and volumeGuid
--								and WFFileUtils.createFullPathFromRelativePathAndGuid( relativePath, volumeGuid )
--								or nil
							
							local name = docValue.name
							
							-- We need to consider that oz has master, only if
							-- it is not marked as invalid
							local ozHasMaster = 
								WFCatFileSpec.getOriginalLinkFromDocValue( docValue )
								and not isOriginalLinkInvalid( docValue )
								and 1 or 0

							if ( ozSha256 and localSha256 and ozSha256 ~= localSha256 ) -- local file is stale
								or ( not localSha256 and ozSha256 ) -- no local binary, but binary available on Oz
							then
								emit( name, 1, ozHasMaster )
							else
								emit( name, 0, ozHasMaster )
							end
					
						end
						
                end,
				
				hasOneRowPerDocument = true,

        }

	indexStoreSchema:addIndex(
		  [[CREATE INDEX presetsAndProfiles_name
				  ON presetsAndProfiles( name, docId )]] )

	indexStoreSchema:addIndex(
		  [[CREATE INDEX presetsAndProfiles_waiting
				  ON presetsAndProfiles( waitingForBinary, ozHasMaster )]] )

end

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

return NmbPresetDocumentProcessor