local m_helpers = require "helpers"
local common = require "interactionmanip.common"
local voicemail = {}

function voicemail.module_type ()
	return "voicemail"
end

function voicemail.updateInteraction (interaction)
	voicemail.isTransferred(interaction)
	voicemail.getLastStep(interaction)
	return interaction
end

function voicemail.getSearchableContent (interaction)
	return ""
end

--[[
	La funcion queda comentada ya que la lista "interaction.interactionData.voicemailRecord", no tiene los atributos "endRecording" y "startRecording", para poder realizar los calculos.
]]--
function voicemail.calculateConversationTimeSec (interaction)
	assert(type(interaction) == "table")
	--LogDebug("[INTERACTIONMAINIP.voicemail.calculateConversationTimeSec] --> ENTRA interaction: '%s'", interaction.id)
	--LogDebug("[INTERACTIONMAINIP.voicemail.calculateConversationTimeSec] --> interaction: '%s'", m_helpers.table:JSON_Encode(interaction))

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

	return 0
end

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

return voicemail