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

local function getFirstTaken (interaction)
	local firstResponse
	local steps = interaction.steps or {}

	--LogDebug("-------> [INTERACTIONMAINIP.chat.getFirstTaken] steps: '%s'", json.encode(steps))

	for currentIndex, step in ipairs(steps) do
		if step.action == "TAKEN" then
			firstResponse = step
			break
		end
	end

	--LogDebug("[INTERACTIONMAINIP.chat.getFirstTaken] interaction: '%s', initiative: '%s' firstResponse '%s'", interaction.id, interaction.initiative, json.encode(firstResponse or {}))

	return firstResponse
end

local function loadStartAttention (interaction)
	assert(interaction)

	--LogDebug("[INTERACTIONMAINIP.chat.loadStartAttention] interaction: '%s',  ", json.encode(interaction))
	if interaction.initiative == "OUTBOUND" then
		return
	end
	local firstTakenStep = getFirstTaken(interaction)
	if not firstTakenStep then
		return
	end

	local startAttention = firstTakenStep.attentionTimestamp
	interaction.__startAttention = startAttention

	--LogDebug("[INTERACTIONMAINIP.chat.loadStartAttention] interaction: '%s',  interaction.__startAttention '%s'", interaction.id, interaction.__startAttention)

	return interaction
end

function chat.module_type ()
	return "chat"
end

function chat.updateInteraction (interaction)
	chat.isTransferred(interaction)
	chat.getLastAgentEndsConversation(interaction)
	chat.getLastStep(interaction)
	interaction.properties.has_attachments = chat.hasAttachments(interaction)
	return interaction
end

function chat.calculateConversationTotalCharacters (interaction)
	assert(interaction)
	--LogDebug("[INTERACTIONMAINIP.chat.calculateConversationTotalCharacters] ENTRA interaction: '%s' ", interaction.id)
	--LogDebug("[INTERACTIONMAINIP.chat.calculateConversationTotalCharacters] interaction: '%s' ", json.encode(interaction))
	--LogDebug("[INTERACTIONMAINIP.chat.calculateConversationTotalCharacters] interaction.events: '%s' ", json.encode(interaction.events or {}))
	local repository = common.GetRepository()
	local info = {id = interaction.id, section = 0, part = 0}
	local success, conversation = repository:RetrieveFile(info)
	if not success then
		LogWarning("[INTERACTIONMAINIP.chat.calculateConversationTotalCharacters] Unable to retrive repository file: '%s' ", m_helpers.table:JSON_Encode(info))
		return 0
	end
	local messages = type(conversation) == "table" and conversation.messages or {}
	return common.calculateConversationTotalCharactersCommonChats(messages, interaction)
end

function chat.hasAttachments (interaction)
	assert(interaction)
	if interaction.interactionData and interaction.interactionData.repositoryAttachments and next(interaction.interactionData.repositoryAttachments) then
		return true
	end
	
	return false
end

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