require "LuaOrm.Date"
local m_cassandra            = require "lua_cassandra"
local m_languageDetector     = require "languageDetector"
local m_languageDictionary = languageDetector.LoadDictionary()

module(..., package.seeall)

local m_repository = nil
function SetRepository (repository)
	assert(repository)
	m_repository = repository
end

local function DetectLanguague (text)
	local defaultLanguague = "es"
	local language = m_languageDetector.DetectLanguage(text, m_languageDictionary)
	LogInfo("[REPORTER.EXPORT_JSON:DetectLanguague] Detected language: '%s', text: '%s'.", language, text)
	return language or defaultLanguague
end

--[[
{
	 "format": "2.1",
	 "job": {
	 },
	 "metadata": {
	  "created_at": "2019-03-05T09:38:10.631Z",
	  "transcription_config": {
	   "channel_diarization_labels": [
		"Agent",
		"Caller"
	   ],
	   "diarization": "channel",
	   "language": "es"
	  },
	  "type": "transcription"
	 },
	 "filterdata": {
		"start_date":"2019-01-10 10:32:01.290",
		"end_date":"2019-01-10 10:32:05.490",
		"channel":"CALL",
		"campaign":"clhogar",
		"account":"clhogar_account",
		"disposition":"1",
		"agent":"jhondoe",
		"origin":"in"
	 }
	 "results": []
}
]]--
local function GetTemplateJsonExtended (params)
	interaction = params.interaction or {}
	language = params.language or "es"

--	LogDebug("[REPORTER.EXPORT_JSON:GetTemplateJsonExtended] params: '%s'.", json.encode(params))

	local function GetInitiative ()
		local origin = "in"
		if (interaction["Interaction.Initiative"] or ""):upper() == "OUTBOUND" then
			origin = "out"
		end
		return origin
	end

	local function GetFirstAgent ()
		if interaction["Interaction.FirstTakeAgent"] then
			--LogDebug("[REPORTER.EXPORT_JSON:GetTemplateJsonExtended.GetFirstAgent] Interaction.FirstTakeAgent: '%s'.", interaction["Interaction.FirstTakeAgent"])
			return interaction["Interaction.FirstTakeAgent"]
		end
		if interaction["Interaction.FirstAgent"] then
			--LogDebug("[REPORTER.EXPORT_JSON:GetTemplateJsonExtended.GetFirstAgent] Interaction.FirstAgent: '%s'.", interaction["Interaction.FirstAgent"])
			return interaction["Interaction.FirstAgent"]
		end
		--LogDebug("[REPORTER.EXPORT_JSON:GetTemplateJsonExtended.GetFirstAgent] 'EMPTY'.")
		return nil
	end

	local function GetLastAgent ()
		if interaction["Interaction.LastTakeAgent"] then
			--LogDebug("[REPORTER.EXPORT_JSON:GetTemplateJsonExtended.GetLastAgent] Interaction.LastTakeAgent: '%s'.", interaction["Interaction.LastTakeAgent"])
			return interaction["Interaction.LastTakeAgent"]
		end
		--LogDebug("[REPORTER.EXPORT_JSON:GetTemplateJsonExtended.GetLastAgent] 'EMPTY'.")
		return nil
	end

	local now = LuaOrm.Date()
	local created_at = now:toString("%Y-%m-%d %H:%M:%S") --"2019-03-05T09:38:10.631Z"

	local template = {
		format = "2.1",
		job = {
			created_at = created_at
		},
		metadata = {
			created_at = created_at,
			transcription_config = {
				channel_diarization_labels = {
					"Agent",
					"Caller"
				},
				diarization = "channel",
				language = language
			},
			type = "transcription"
		},
		filterdata = {
			start_date      = interaction["Interaction.StartDate"],
			end_date        = interaction["Interaction.EndDate"],
			channel         = interaction["Interaction.Type"],
			campaign        = interaction["Interaction.Campaign"],
			account         = interaction["Interaction.Account"],
			disposition     = interaction["Interaction.DispositionCode"],
			agent           = GetFirstAgent(),
			last_take_agent = GetLastAgent(),
			origin          = GetInitiative()
		},
		results = {}
	}

	return template
end

--[[
Sample:
  {
   "alternatives": [
    {
     "confidence": 1.0,
     "content": "eh",
     "language": "es"
    }
   ],
   "channel": "Agent",
   "end_time": 28.53,
   "start_time": 28.49,
   "type": "word"
  },
]]--
local function MakeJsonExtendedResultItem (params)
	local alternatives = {}
	table.insert(alternatives, {
		confidence = 1.0, -- 0.59
		content = params.content, -- parseo palabra
		language = params.language
	})
	return {
		alternatives = alternatives,
		channel = params.channel,
		end_time = params.elapsedTime, --4.76,
		start_time = params.elapsedTime, --4.49,
		type = params.type, --"word"
		occ_data = params.occ_data or {}
	}
end

local function UpdateMetaData (info, params)
	--LogDebug("[REPORTER.EXPORT_JSON:UpdateMetaData] params: '%s'.", json.encode(params))
	info.metadata.occ_data = params
end

local function ParseLine (params)
	local interactionId = params.interactionId
	local channel = params.channel
	local language = params.language

	--LogDebug("--1 params.msgTime: '%s'", params.msgTime)
	--LogDebug("--2 params.interactionStartTime: '%s'", params.interactionStartTime)
	local msgTime = LuaOrm.Date(params.msgTime)
	local interactionStartTime = LuaOrm.Date(params.interactionStartTime)
	--LogDebug("--3 msgTime: '%s'", msgTime:toString())
	--LogDebug("--4 interactionStartTime: '%s'", interactionStartTime)
	--LogDebug("--4.1 msgTime:time() '%s'", msgTime:time())
	--LogDebug("--4.5 interactionStartTime:time() '%s'", interactionStartTime:time())
	local elapsedTime = msgTime:time() - interactionStartTime:time()
	elapsedTime = elapsedTime >= 0 and elapsedTime or 0
	--LogDebug("--5 elapsedTime: '%s'", elapsedTime)

	local SanitizeWord = params.SanitizeWord or function(word) return word end

	local function Round (num, numDecimalPlaces) return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num)) end
	elapsedTime = Round(elapsedTime, 2)

	local function GetBasicExtendedInfo ()
		return {
			channel = channel,
			language = language,
			elapsedTime = elapsedTime,
			occ_data = {
				msgNumber = params.number,
				msgTime   = params.msgTime,
				msgDate   = msgTime:toString()
			}
		}
	end

	local function ParsePunctuation (text, words_counter)
		local continue = true
		local results = {}
		--local i = 0

		while continue do
			--i = i + 1
			--LogDebug("-%d- Parse Punctuation---------", i)
			--LogDebug(text)
			local punctuation = "."
			local start, finish = string.find(text, punctuation, nil, true)
			local word
			if start then
				word = string.sub(text, 0, start - 1)
				word = SanitizeWord(word)
				text = string.sub(text, start + 1)

				if word:len() > 0 then
					local extendedInfo = GetBasicExtendedInfo()
					extendedInfo.content = word
					extendedInfo.type = "word"
					words_counter = words_counter + 1
					extendedInfo.occ_data.word_number = words_counter

					local resultItem = MakeJsonExtendedResultItem(extendedInfo)
					table.insert(results, resultItem)
				end

				local extendedInfo = GetBasicExtendedInfo()
				extendedInfo.content = punctuation
				extendedInfo.type = "punctuation"
				words_counter = words_counter + 1
				extendedInfo.occ_data.word_number = words_counter

				local resultItem = MakeJsonExtendedResultItem(extendedInfo)
				table.insert(results, resultItem)
			else
				word = text
				word = SanitizeWord(word)
				text = ""
				if word:len() > 0 then
					local extendedInfo = GetBasicExtendedInfo()
					extendedInfo.content = word
					extendedInfo.type = "word"
					words_counter = words_counter + 1
					extendedInfo.occ_data.word_number = words_counter

					local resultItem = MakeJsonExtendedResultItem(extendedInfo)
					table.insert(results, resultItem)
				end
			end

			if text:len() == 0 then
				continue = false
			end
		end

		return results, words_counter
	end

	local function ParseWord (text)
		local results = {}
		local continue = true
		local words_counter = 0

		while continue do
			--words_counter = words_counter + 1
			--LogDebug("-%d- Parse Word---------", words_counter)
			--LogDebug(text)
			local whiteSpace = " "
			local start, finish = string.find(text, whiteSpace, nil, true)
			local word
			if start then
				word = string.sub(text, 0, start - 1)
				text = string.sub(text, start + 1)
			else
				word = text
				text = ""
			end
			--LogDebug(word)
			local punctuationResult
			punctuationResult, words_counter = ParsePunctuation(word, words_counter)

			for _, v in ipairs(punctuationResult) do
				table.insert(results, v)
			end
			if text:len() == 0 then
				continue = false
			end
		end

		return results
	end

	return ParseWord(params.text)
end

-----------------------------------------------------------------------
--
local function GetJsonChatCommonContent (id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetJsonChatCommonContent] id: '%s', options: '%s'.", id, options)

	local keyspace = m_cassandra:CassandraConnection("Interactions")

	local interactionEvents = keyspace:GetSliceRange(id,
		"Interactions",
		"", {
			column_names = {"events"},
			slice_range = {}
		}
	)
	keyspace:Close()

	local events
	if interactionEvents then
		for _, v in ipairs(interactionEvents) do
			events = v.value
		end
	end

	if not events then
		return
	end

	events = json.decode(events)

	local response
	local language = options.language
	local messages = {}

	for i, event in ipairs(events) do
		--LogDebug("[REPORTER.EXPORT_JSON:GetJsonChatCommonContent] i: '%d', event: '%s'.", i, json.encode(event))
		local message = event.message or {}
		local messageText = message.text or ""
		local messageTime = message.timestamp

		if not language then
			language = DetectLanguague(messageText)
		end
		if not response then
			response = GetTemplateJsonExtended({interaction = options.interaction, language = language, interactionId = id})
		end

		local channel = ""
		if event.creator == "agent" then
			channel = "Agent"
		elseif event.creator == "bot" then
			channel = "Bot" -- Caller
		elseif event.creator == "contact" then
			channel = "Caller"
		end

		local parsedMessage = ParseLine({
			number               = i,
			interactionId        = id,
			text                 = messageText,
			channel              = channel,
			language             = language,
			msgTime              = messageTime,
			interactionStartTime = options.interaction["Interaction.StartDate"]
		})

		for _, wordInfo in ipairs(parsedMessage) do
			table.insert(response.results, wordInfo)
		end

		table.insert(messages, {
			msgNumber = i,
			fullMsg   = messageText,
			msgTime   = messageTime,
			msgDate   = LuaOrm.Date(messageTime):toString()
		})
	end

	if not response then
		response = GetTemplateJsonExtended({interaction = options.interaction})
	end

	UpdateMetaData(response, {
		interactionId = id,
		messages = messages
	})

	return json.encode(response, true)
end
---------------

------------------------------------------------------------
-- Función en común,utilizada por Facebook e Instagram
--
local function GetJsonFBCommonContent (id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetJsonFBCommonContent] id: '%s', options: '%s'.", id, options)

	local keyspace = m_cassandra:CassandraConnection("Interactions")

	local interactionEvents = keyspace:GetSliceRange(id,
		"Interactions",
		"", {
			column_names = {"events"},
			slice_range = {}
		}
	)
	keyspace:Close()

	local encodedEvents
	if interactionEvents then
		for _, v in ipairs(interactionEvents) do
			encodedEvents = v.value
			--LogDebug("---> %d - encodedEvents: '%s'", _, encodedEvents)
		end
	end

	if not encodedEvents then
		return
	end

	local events = json.decode(encodedEvents)
	local sortedEvents = options.BuildEventsHierarchyFn(events)

	if not sortedEvents then
		return
	end
	--LogDebug("---> sortedEvents: '%s'", json.encode(sortedEvents))

	local response
	local language = options.language
	local messages = {}
	local messages_counter = 0

	local function RecursiveFill (currentEvents)
		for _, event in ipairs(currentEvents or {}) do
			--LogDebug("[REPORTER.EXPORT_JSON:GetJsonFBCommonContent] messages_counter: '%d', event: '%s'.", messages_counter, json.encode(event))
			messages_counter = messages_counter + 1
			local messageText = event.text or ""
			local messageTime = event.timestamp

			if not language then
				language = DetectLanguague(messageText)
			end
			if not response then
				response = GetTemplateJsonExtended({interaction = options.interaction, language = language, interactionId = id})
			end

			local channel = ""
			if event.creator == "agent" then
				channel = "Agent"
			elseif event.creator == "bot" then
				channel = "Bot" -- Caller
			elseif event.creator == "contact" then
				channel = "Caller"
			end

			local parsedMessage = ParseLine({
				number               = messages_counter,
				interactionId        = id,
				text                 = messageText,
				channel              = channel,
				language             = language,
				msgTime              = messageTime,
				interactionStartTime = options.interaction["Interaction.StartDate"]
			})

			for _, wordInfo in ipairs(parsedMessage) do
				table.insert(response.results, wordInfo)
			end

			table.insert(messages, {
				msgNumber = messages_counter,
				fullMsg   = messageText,
				msgTime   = messageTime,
				msgDate   = LuaOrm.Date(messageTime):toString()
			})

			RecursiveFill(event.childrens)
		end
	end

	RecursiveFill(sortedEvents)

	if not response then
		response = GetTemplateJsonExtended({interaction = options.interaction})
	end

	UpdateMetaData(response, {
		interactionId = id,
		messages = messages
	})

	return json.encode(response, true)
end


------------------------------------------------------------
--
function GetSmsContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetSmsContent] id: '%s', options: '%s'.", id, options)
	return GetJsonChatCommonContent(id, options)
end

------------------------------------------------------------
--
function GetWhatsappContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetWhatsappContent] id: '%s', options: '%s'.", id, options)
	return GetJsonChatCommonContent(id, options)
end

------------------------------------------------------------
--
function GetInstagramContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetInstagramContent] id: '%s', options: '%s'.", id, options)
	return GetJsonFBCommonContent(id, options)
end

------------------------------------------------------------
--
function GetYoutubeContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetYoutubeContent] id: '%s', options: '%s'.", id, options)
	return GetJsonChatCommonContent(id, options)
end

------------------------------------------------------------
--
function GetAppstoreContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetAppstoreContent] id: '%s', options: '%s'.", id, options)
	return GetJsonChatCommonContent(id, options)
end

------------------------------------------------------------
--
function GetGoogleplayContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetGoogleplayContent] id: '%s', options: '%s'.", id, options)
	return GetJsonChatCommonContent(id, options)
end

------------------------------------------------------------
--
function GetRcsContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetRcsContent] id: '%s', options: '%s'.", id, options)
	return GetJsonChatCommonContent(id, options)
end

------------------------------------------------------------
--
function GetFacebookContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetFacebookContent] id: '%s', options: '%s'.", id, options)
	return GetJsonFBCommonContent(id, options)
end


--[[-------------------------------------------------------------------
	Sample - Chat messages (messages.messages):
		[
			{
				"user_name": "86ab846b-1451-45df-bb8e-a0c8f8b142d4",
				"display_name": "Web Client",
				"msgtype": "text",
				"session_id": "fa92efa5-de54-4968-916f-db6510eabbcd",
				"text": "hola como estas ?",
				"msgtime": 1559732866.018,
				"user_id": "86ab846b-1451-45df-bb8e-a0c8f8b142d4@emma"
			},
			{
				"user_name": "agentee1",
				"display_name": "Agente1",
				"session_id": "fa92efa5-de54-4968-916f-db6510eabbcd",
				"msgtime": 1559732876.36,
				"text": "bien y vos ?",
				"msgtype": "text",
				"user_id": "agentee1@emma"
			}
		]

	Sample Response:
			{
			 "format": "2.1",
			 "job": {
			  "created_at": "Mon Mar 04 22:47:33 2019",
			  "duration": 62,
			  "id": 20918,
			  "lang": "es",
			  "meta": "",
			  "name": "{79A1445C-3FF4-45A7-A74D-C802B8288D1A}.mp3",
			  "user_id": 1
			 },
			 "metadata": {
			  "created_at": "2019-03-05T09:38:10.631Z",
			  "transcription_config": {
			   "channel_diarization_labels": [
				"Agent",
				"Caller"
			   ],
			   "diarization": "channel",
			   "language": "es"
			  },
			  "type": "transcription"
			 },
			 "results": [
			  {
			   "alternatives": [
				{
				 "confidence": 0.59, +--1.0
				 "content": "Sí", -- parseo palabra
				 "language": "es"
				}
			   ],
			   "channel": "Agent",
			   "end_time": 1.8,
			   "start_time": 1.61,
			   "type": "word"
			  },
			  {
			   "alternatives": [
				{
				 "confidence": 0.77,
				 "content": ".",
				 "language": "es"
				}
			   ],
			   "channel": "Agent",
			   "end_time": 3.38,
			   "start_time": 1.8,
			   "type": "punctuation"
			  },
--]]
function GetWebchatContent (self, id, options)
	assert(id)
	assert(options)
	assert(options.interaction)
	assert(options.vcc)
	assert(options.utils)

	LogInfo("[REPORTER.EXPORT_JSON:GetWebchatContent] id: '%s', options: '%s'.", id, options)

	--TODO: poner ip y port del repositorio posta e intentar con varios de ellos si uno falla
	local success, encodedConversation = m_repository.retrieve_file(id, 0, 0, false)
	if not success then
		return
	end

	local function GetVCCUsers ()
		return options.utils.GetAllColumns {
			keyspace = "Agent",
			columnFamily = "VCCUsers",
			key = options.vcc,
			columnCount = 50000
		} or {}
	end

	local vccUsers
	local function GetMessageOwner (fullAgentId)
		fullAgentId = fullAgentId or ""
		if not vccUsers then
			vccUsers = GetVCCUsers()
			--LogDebug("[REPORTER.EXPORT_JSON:GetWebchatContent] --> vccUsers: %s", json.encode(vccUsers))
		end
		local agentId = options.utils.GetId(fullAgentId):lower()
		local agent = vccUsers[agentId]
		local owner = agent and "Agent" or "Caller"
		--LogDebug("[REPORTER.EXPORT_JSON:GetWebchatContent] --> owner: '%s', agentId: '%s', fullAgentId: '%s'.", owner, agentId, fullAgentId)
		return owner
	end

	local success, conversation = pcall(json.decode, encodedConversation)
	--LogDebug("[REPORTER.EXPORT_JSON:GetWebchatContent] --> encodedConversation: '%s'", encodedConversation)

	if type(conversation) == "table" and conversation.messages then
		local response
		local language = options.language
		local messages = {}

		for i, message in ipairs(conversation.messages) do
			--LogDebug("[REPORTER.EXPORT_JSON:GetWebchatContent] %d --> message: '%s'", i, json.encode(message))

			if message.msgtype == "text" then
				if not language then
					language = DetectLanguague(message.text)
				end
				if not response then
					response = GetTemplateJsonExtended({interaction = options.interaction, language = language, interactionId = id})
				end

				-- Obtengo el owner/channel (quien ha generado el mensaje el Agente o Caller)
				local owner = GetMessageOwner(message.user_id)
				local messageTime = message.msgtime

				local parsedMessage = ParseLine({
					number               = i,
					interactionId        = id,
					text                 = message.text,
					channel              = owner,
					language             = language,
					msgTime              = messageTime,
					interactionStartTime = options.interaction["Interaction.StartDate"]
				})

				for _, wordInfo in ipairs(parsedMessage) do
					table.insert(response.results, wordInfo)
				end

				table.insert(messages, {
					msgNumber = i,
					fullMsg   = message.text,
					msgTime   = messageTime,
					msgDate   = LuaOrm.Date(messageTime):toString()
				})

			end
		end

		if not response then
			response = GetTemplateJsonExtended({interaction = options.interaction})
		end

		UpdateMetaData(response, {
			interactionId = id,
			messages = messages
		})

		return json.encode(response, true)
	end
end

function GetTwitterContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetTwitterContent] id: '%s', options: '%s'.", id, options)

	local keyspace = m_cassandra:CassandraConnection("Interactions")

	local interactionEvents = keyspace:GetSliceRange(id,
		"Interactions",
		"", {
			column_names = {"events"},
			slice_range = {}
		}
	)
	keyspace:Close()

	local encodedEvents
	if interactionEvents then
		for _, v in ipairs(interactionEvents) do
			encodedEvents = v.value
			--LogDebug("---> %d - encodedEvents: '%s'", _, encodedEvents)
		end
	end

	if not encodedEvents then
		return
	end
	local events = json.decode(encodedEvents)
	local response
	local language = options.language
	local messages = {}

	for i, event in ipairs(events) do
		local messageText = event.text or ""
		local messageTime = event.timestamp

		if not language then
			language = DetectLanguague(messageText)
		end
		if not response then
			response = GetTemplateJsonExtended({interaction = options.interaction, language = language, interactionId = id})
		end

		local channel = ""
		if event.creator == "agent" then
			channel = "Agent"
		elseif event.creator == "bot" then
			channel = "Bot" -- Caller
		elseif event.creator == "contact" then
			channel = "Caller"
		end

		local parsedMessage = ParseLine({
			number               = i,
			interactionId        = id,
			text                 = messageText,
			channel              = channel,
			language             = language,
			msgTime              = messageTime,
			interactionStartTime = options.interaction["Interaction.StartDate"]
		})

		for _, wordInfo in ipairs(parsedMessage) do
			table.insert(response.results, wordInfo)
		end

		table.insert(messages, {
			msgNumber = i,
			fullMsg   = messageText,
			msgTime   = messageTime,
			msgDate   = LuaOrm.Date(messageTime):toString()
		})
	end

	if not response then
		response = GetTemplateJsonExtended({interaction = options.interaction})
	end

	UpdateMetaData(response, {
		interactionId = id,
		messages = messages
	})

	return json.encode(response, true)
end

function GetEMailContent (self, id, options)
	LogInfo("[REPORTER.EXPORT_JSON:GetEMailContent] id: '%s', options: '%s'.", id, options)

	local keyspace = m_cassandra:CassandraConnection("Interactions")

	local interactionEvents = keyspace:GetSliceRange(id,
		"Interactions",
		"", {
			column_names = {"events"},
			slice_range = {}
		}
	)
	keyspace:Close()

	local encodedEvents
	if interactionEvents then
		for _, v in ipairs(interactionEvents) do
			encodedEvents = v.value
			--LogDebug("---> %d - encodedEvents: '%s'", _, encodedEvents)
		end
	end

	if not encodedEvents then
		return
	end
	local events = json.decode(encodedEvents)
	local response
	local language = options.language

	local function GetPlainBody (body)
		local plainBody = ""
		for _, bodyInfo in ipairs(body or {}) do
			if (bodyInfo.type or ""):lower() == "text/plain" then
				plainBody = bodyInfo.content
				break
			end
		end
		return plainBody
	end

	local messages = {}

--LogDebug("1 ----------------------------------")
	for i, event in ipairs(events) do
		--LogDebug("--> %d - mail event: '%s'", _, json.encode(event))
--LogDebug("  1.1")
		local messageText = GetPlainBody(event.body)
		local messageTime = event.timestampDouble

		if not language then
			language = DetectLanguague(messageText)
		end
--LogDebug("  1.2")
		if not response then
			response = GetTemplateJsonExtended({interaction = options.interaction, language = language, interactionId = id})
		end
--LogDebug("  1.3")
		local channel = ""
		if event.creator == "agent" then
			channel = "Agent"
		elseif event.creator == "bot" then
			channel = "Bot" -- Caller
		elseif event.creator == "contact" then
			channel = "Caller"
		end
--LogDebug("  1.4")
		local parsedMessage = ParseLine({
			number               = i,
			interactionId        = id,
			text                 = messageText,
			channel              = channel,
			language             = language,
			msgTime              = messageTime,
			interactionStartTime = options.interaction["Interaction.StartDate"],
			SanitizeWord         = function(word)
				--print("----> word " .. tostring(word))
				local sanitizedWord = word:gsub("\r", "")
				sanitizedWord = sanitizedWord:gsub("\n", "")
				--print("----> sanitizedWord " .. tostring(sanitizedWord))
				return sanitizedWord
			end
		})
--LogDebug("  1.5")
		for _, wordInfo in ipairs(parsedMessage) do
			table.insert(response.results, wordInfo)
		end
--LogDebug("  1.6")
		table.insert(messages, {
			msgNumber = i,
			fullMsg   = messageText,
			msgTime   = messageTime,
			msgDate   = LuaOrm.Date(messageTime):toString()
		})
	end
--LogDebug("2 -------------------------------------------")
	if not response then
		response = GetTemplateJsonExtended({interaction = options.interaction})
	end

	UpdateMetaData(response, {
		interactionId = id,
		messages = messages
	})

	return json.encode(response, true)
end
