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

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

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

local WFArguments = require 'WFArguments'

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

local check = assert( WFArguments.check )

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

-- @mark LrCommentsIndexDefinitions namespace

local LrCommentsIndexDefinitions = {}

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

function LrCommentsIndexDefinitions.generateIndexSchema( indexStoreSchema )

	check( 'WFRxIndexStoreSchema' )

	-- table comment

	indexStoreSchema:addTable {

		sql = [[CREATE TABLE comment( assetId, spaceId, body, created, updateTimeStamp, author_first_name, author_last_name, author_full_name, author_id )]],

		relevantTypeSubtypeTuples = {
			{ 'comment', 'user' }
		},

		map = function( docValue, emit )

			if docValue and not docValue._deleted
			  and docValue.type == 'comment'
			then
				
				local spm = docValue._serverProvidedMetadata or {}
				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 spm.updated
				local created = docValue.userCreated or spm.created or ( localOnly.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 body = docValue.body
				local authorInfo = docValue.author or spm.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 spaceId and created then
										
					emit( assetId, spaceId, body, created, updateTimeStamp, author_first_name, author_last_name, author_full_name, author_id )
					
				end
					
			end
						
		end,
		
		hasOneRowPerDocument = true,
	}


	indexStoreSchema:addIndex [[CREATE INDEX comment_asset_space ON comment( assetId, spaceId )]]
	indexStoreSchema:addIndex [[CREATE INDEX comment_space ON comment( spaceId )]]

end

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

return LrCommentsIndexDefinitions
