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

LrFavoritesIndexDefinitions.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2018 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.

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

	-- WF.core.utils
local KSTableUtils = require 'KSTableUtils'
local WFArguments = require 'WFArguments'

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

local derefOpt = assert( KSTableUtils.derefOpt )

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

local check = assert( WFArguments.check )

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

-- @mark LrFavoritesIndexDefinitions namespace

local LrFavoritesIndexDefinitions = {}

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

function LrFavoritesIndexDefinitions.generateIndexSchema( indexStoreSchema )

	check( 'WFRxIndexStoreSchema' )
	
	--------------------------------------------------------------------------------
	-- NOTE: This table definition is shared across all Lightroom clients so changes 
	-- to it will affect other clients.
	--------------------------------------------------------------------------------

	indexStoreSchema:addTable {

		sql = [[CREATE TABLE spacefavorite( assetId, spaceId, created, updateTimeStamp, author_first_name, author_last_name, author_full_name, author_id )]],
		
		relevantTypeSubtypeTuples = {
			{ 'favorite', 'user' }
		},

		map = function( docValue, emit )
			
			local docType = docValue.type
			if docValue and not docValue._deleted and docType == 'favorite' then
				
				local localOnly = docValue._localOnly or {}
				local spaceId = ( docValue.space and docValue.space.id ) or docValue._spaceId or localOnly.spaceId
				local assetId = ( docValue.asset and docValue.asset.id ) or ( localOnly.assetId )
				local updateTimeStamp = docValue.userUpdated or docValue.updated or derefOpt( docValue, '_serverProvidedMetadata', 'updated' )
				local created = docValue.userCreated or docValue.created or localOnly.created or derefOpt( docValue, '_serverProvidedMetadata', 'created' )
					-- docValue.userCreated should be the correct place for a local indication of when the document
					-- was created. The rest are old impls/fallbacks in case one doesn't exist

				local authorInfo = docValue.author or 
					( docValue._serverProvidedMetadata and docValue._serverProvidedMetadata.author )
					or localOnly.author
					
				local author_first_name, author_last_name, author_full_name, author_id
				
				if authorInfo then
				
					author_first_name, author_last_name, author_full_name, author_id =
						authorInfo.first_name,
						authorInfo.last_name,
						authorInfo.full_name,
						authorInfo.id
				
				end
				
				if assetId and created then
					
					emit( assetId, spaceId, created, updateTimeStamp, author_first_name, author_last_name, author_full_name, author_id )
					
				end
					
			end
						
		end,
		
		hasOneRowPerDocument = true,

	}

	indexStoreSchema:addIndex [[CREATE INDEX spacefavorite_asset_space_author ON spacefavorite( assetId, spaceId, author_id )]]

	indexStoreSchema:addIndex [[CREATE INDEX spacefavorite_space ON spacefavorite( spaceId )]]
	
end

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

return LrFavoritesIndexDefinitions