local common = require "interactionmanip.common"
local call = {}

function call.module_type ()
	return "call"
end

function call.updateInteraction (interaction)
	interaction.properties = interaction.properties or {}
	call.isTransferred(interaction)
	call.getLastAgentEndsConversation(interaction)
	call.getLastStep(interaction)
	return interaction
end

function call.getSearchableContent (interaction)
	return ""
end

function call.calculateConversationTimeSec (interaction)
	assert(type(interaction) == "table")
	--LogDebug("[INTERACTIONMAINIP.call.calculateConversationTimeSec] --> ENTRA interaction: '%s'", interaction.id)
	--LogDebug("[INTERACTIONMAINIP.call.calculateConversationTimeSec] --> interaction: '%s'", m_helpers.table:JSON_Encode(interaction))
	if not interaction.interactionData then
		--LogWarning("[INTERACTIONMAINIP.call.calculateConversationTimeSec] 1 - Invalid 'interactionData' in interaction: '%s'", interaction.id)
		return 0
	end
	if not interaction.interactionData.records then
		--LogWarning("[INTERACTIONMAINIP.call.calculateConversationTimeSec] 2 - Invalid 'interactionData.records' in interaction: '%s'", interaction.id)
		return 0
	end
	local function MustCalculate (record)
		if record.startRecording and record.endRecording then
			return true
		end
		LogWarning("[INTERACTIONMAINIP.call.calculateConversationTimeSec] 3 - Invalid interactionData.records.startRecording: '%s' or interactionData.records.endRecording: '%s' in interaction: '%s'", record.startRecording, record.endRecording, interaction.id)
		return false
	end
	local totalTimeSec = 0
	for _, record in pairs(interaction.interactionData.records) do
		--LogDebug("[INTERACTIONMAINIP.call.calculateConversationTimeSec] 4 --> record: '%s' ", json.encode(record))
		if MustCalculate(record) then
			local timeSec = record.endRecording - record.startRecording
			totalTimeSec = totalTimeSec + timeSec
		end
	end
	--LogDebug("[INTERACTIONMAINIP.call.calculateConversationTimeSec] --> SALE totalTimeSec: '%s' interaction: '%s'", totalTimeSec, interaction.id)
	return totalTimeSec
end

------------------------------------------------------
call.isTransferred                        = common.isTransferred
call.isTaken                              = common.isTaken
call.isWrappedUp                          = common.isWrappedUp
call.isFinished                           = common.isFinished
call.getConversationOwner                 = common.getConversationOwner
call.getFirstAssignedAgent                = common.getFirstAssignedAgent
call.getLastAgentEndsConversation         = common.getLastAgentEndsConversation
call.getLastEventTimestamp                = common.getLastEventTimestamp
call.getLastStep                          = common.getLastStep
call.getSocialNetworkPostId               = common.getSocialNetworkPostId
call.getBatchId                           = common.getBatchId
call.getFirstTransferringInfo             = common.getFirstTransferringInfo
call.loadFirstPendingStep                 = common.loadFirstPendingStep
call.loadStatusInteraction                = common.loadStatusInteraction
call.loadFirstTakenStep                   = common.loadFirstTakenStep
call.loadStartAttention                   = common.loadStartAttention
call.loadTicket                           = common.loadTicket
call.loadFirstAgentResponse               = common.loadFirstAgentResponse
call.loadLastAgentResponse                = common.loadLastAgentResponse
call.getConversationTotals                = common.getConversationTotals
call.calculateConversationTimeSec         = call.calculateConversationTimeSec or common.calculateConversationTimeSec
call.calculateConversationTotalCharacters = call.calculateConversationTotalCharacters or common.calculateConversationTotalCharacters

return call