local lua_cassandra = require "lua_cassandra"
local utils = require "qna_maker.utils"

module(..., package.seeall)

local m_dataSource
local m_models = {}
local m_loaded = false

--recupera la lista de modelos del directorio models
local function GetModelsFromFilesystem()
	return {vccs = true}
end

--///////////////////////////////////////////////////////////////////////////////////
-- arma la tabla de modelos definidos
local function CreateModelTable() 
	LogInfo("[QNA_MAKER.MODELS:CreateModelTable] Creating cassandra models")
	if not m_loaded then
		for key in pairs(GetModelsFromFilesystem()) do
			local model = require("qna_maker.models." .. key)
			if model.GetDefinition then
				m_models[model.GetName()] = model.GetDefinition(m_dataSource)
			end
		end
		m_loaded = true
	else
		--if model modules were already loaded just overwrite their inner datasource
		--with the newly changed one
		for modelName, model in pairs(m_models) do
			model.GetDefinition(m_datasource)
		end
	end
end

--///////////////////////////////////////////////////////////////////////////////////
-- 
function GetModels()
	return m_models
end

--///////////////////////////////////////////////////////////////////////////////////
-- 
function BuildModels()
	--init local datasource table
	m_dataSource = {
		cassandra = lua_cassandra
	}
	utils.SetupConnections({ cassandra = lua_cassandra })
	CreateModelTable()
	LogInfo("[QNA_MAKER.MODELS:BuildModels] Qna Maker model built")
	return true
end

BuildModels()