module(..., package.seeall)

local utils = require "qna_maker.utils"
local m_datasource

function GetName ()
	return "VCCs"
end

function GetDefinition (datasource)
	m_datasource = datasource
	return _M
end

function VCCExists (vcc, keyspace)
	return utils.GetColumnValue({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCs",
		key = vcc,
		column = "Id"
	})
end

-- --[[
	-- KNOWLEDGEBASE -------------------------------------
-- ]]--

function GetKnowledgeBase (vcc, kbId, keyspace)
	return utils.GetColumnValue({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCQnAMakerKnowledgeBases",
		key = vcc,
		column = kbId
	})
end

function LoadKnowledgeBases (vcc, keyspace)
	return utils.GetAllColumns({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCQnAMakerKnowledgeBases",
		key = vcc,
		columns = {}
	}) or {}
end

local function SaveKnowledgeBase (vcc, knowledgeBases, keyspace)
	local rows = {}
	rows[vcc] = knowledgeBases
	utils.SaveRows({ keyspace = keyspace or "Agent", columnFamily = "VCCQnAMakerKnowledgeBases", rows = rows })
end

function UpdateKnowledgeBase (vcc, kbId, info, keyspace)
	LogDebug("[QNA_MAKER_API.VCC:UpdateKnowledgeBase] Updating Data Base Vcc '%s', kbId: '%s', Info: '%s'", vcc, kbId, json.encode(info or {}))
	local knowledgeBases = LoadKnowledgeBases(vcc, keyspace)
	knowledgeBases[kbId] = knowledgeBases[kbId] or {}
	for name, value in pairs(info or {}) do
		knowledgeBases[kbId][name] = value
	end
	SaveKnowledgeBase(vcc, knowledgeBases, keyspace)
end

function RemoveKnowledgeBase (vcc, kbId, keyspace)
	LogDebug("[QNA_MAKER_API.VCC:RemoveKnowledgeBase] Vcc '%s', kbId: '%s'", vcc, kbId)
	local knowledgeBases = {}
	table.insert(knowledgeBases, kbId)
	utils.RemoveColumns({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCQnAMakerKnowledgeBases",
		rows = {
			[vcc] = knowledgeBases
		}
	})
end


--[[
	SUBSCRIPTIONS -------------------------------------
]]--

function GetSubscriptionsKeysQnAMaker (vcc, keyspace)
	return utils.GetAllColumns({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCQnAMakerSubscriptionsKeys",
		key = vcc,
		columns = {}
	}) or {}
end

function GetSubscriptionsKeysQnAMakerV2 (vcc, keyspace) -- fede
	return utils.GetAllColumns({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCQnAMakerV2SubscriptionsKeys",
		key = vcc,
		columns = {}
	}) or {}
end

local function SaveSubscriptionsKeysMsCognitiveServices (vcc, vccSubscriptionsKeys, keyspace)
	local rows = {}
	rows[vcc] = vccSubscriptionsKeys
	utils.SaveRows({ keyspace = keyspace or "Agent", columnFamily = "VCCQnAMakerSubscriptionsKeys", rows = rows })
end

local function SaveSubscriptionsKeysMsCognitiveServicesV2 (vcc, vccSubscriptionsKeys, keyspace) -- fede
	local rows = {}
	rows[vcc] = vccSubscriptionsKeys
	utils.SaveRows({ keyspace = keyspace or "Agent", columnFamily = "VCCQnAMakerV2SubscriptionsKeys", rows = rows })
end

local function AddVCCBySubscriptionKey (vcc, key, keyspace)
	local close_keyspace = false
	if not keyspace then
		keyspace = utils.GetDatasource():CassandraConnection("Agent")
		close_keyspace = true
	end
	keyspace:InsertColumn(key, "VCCsByQnAMakerSubscriptionKey", vcc, "")
	if close_keyspace then
		keyspace:Close()
	end
end

local function AddVCCBySubscriptionKeyV2 (vcc, key, keyspace) -- fede
	local close_keyspace = false
	if not keyspace then
		keyspace = utils.GetDatasource():CassandraConnection("Agent")
		close_keyspace = true
	end
	keyspace:InsertColumn(key, "VCCsByQnAMakerV2SubscriptionKey", vcc, "")
	if close_keyspace then
		keyspace:Close()
	end
end

function AssignSubscriptionKeyToVCC (vcc, key, subscripitonInfo, keyspace)
	local vccSubscriptionsKeys = GetSubscriptionsKeysQnAMaker(vcc, keyspace)
	vccSubscriptionsKeys[key] = subscripitonInfo or vccSubscriptionsKeys[key] or {}
	SaveSubscriptionsKeysMsCognitiveServices(vcc, vccSubscriptionsKeys, keyspace)
	AddVCCBySubscriptionKey(vcc, key, keyspace)
end

function AssignSubscriptionKeyToVCCV2 (vcc, key, subscripitonInfo, keyspace) -- fede
	local vccSubscriptionsKeys = GetSubscriptionsKeysQnAMakerV2(vcc, keyspace)
	vccSubscriptionsKeys[key] = subscripitonInfo or vccSubscriptionsKeys[key] or {}
	SaveSubscriptionsKeysMsCognitiveServicesV2(vcc, vccSubscriptionsKeys, keyspace)
	AddVCCBySubscriptionKeyV2(vcc, key, keyspace)
end

local function RemoveVCCBySubscriptionKey (vcc, key, keyspace)
	local close_keyspace = false
	if not keyspace then
		keyspace = utils.GetDatasource():CassandraConnection("Agent")
		close_keyspace = true
	end
	keyspace:RemoveColumn(key, "VCCsByQnAMakerSubscriptionKey", vcc)
	if close_keyspace then
		keyspace:Close()
	end
end

local function RemoveVCCBySubscriptionKeyV2 (vcc, key, keyspace) -- fede
	local close_keyspace = false
	if not keyspace then
		keyspace = utils.GetDatasource():CassandraConnection("Agent")
		close_keyspace = true
	end
	keyspace:RemoveColumn(key, "VCCsByQnAMakerV2SubscriptionKey", vcc)
	if close_keyspace then
		keyspace:Close()
	end
end

function UnAssignSubscriptionKeyOfVCC (vcc, key, keyspace)
	local vccSubscriptionsKeys = {}
	table.insert(vccSubscriptionsKeys, key)
	utils.RemoveColumns({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCQnAMakerSubscriptionsKeys",
		rows = {
			[vcc] = vccSubscriptionsKeys
		}
	})
	RemoveVCCBySubscriptionKey(vcc, key, keyspace)
end

function UnAssignSubscriptionKeyOfVCCV2 (vcc, key, keyspace) -- fede
	local vccSubscriptionsKeys = {}
	table.insert(vccSubscriptionsKeys, key)
	utils.RemoveColumns({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCQnAMakerV2SubscriptionsKeys",
		rows = {
			[vcc] = vccSubscriptionsKeys
		}
	})
	RemoveVCCBySubscriptionKeyV2(vcc, key, keyspace)
end

function LoadVCCsBySubscriptionKey (key, keyspace)
	local vccs = utils.GetAllColumns({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCsByQnAMakerSubscriptionKey",
		key = key
	}) or {}
	return vccs
end

function LoadVCCsBySubscriptionKeyV2 (key, keyspace) -- fede
	local vccs = utils.GetAllColumns({
		keyspace = keyspace or "Agent",
		columnFamily = "VCCsByQnAMakerV2SubscriptionKey",
		key = key
	}) or {}
	return vccs
end

-- Funcion invocada cuando se elimina una subscripiton
function RemoveSubscriptionKeyFromAllVCCs (key, keyspace)
	local vccs = LoadVCCsBySubscriptionKey(key, keyspace)
	for vcc in pairs(vccs) do
		UnAssignSubscriptionKeyOfVCC(vcc, key, keyspace)
	end
end

function RemoveSubscriptionKeyFromAllVCCsV2 (key, keyspace) -- fede
	local vccs = LoadVCCsBySubscriptionKeyV2(key, keyspace)
	for vcc in pairs(vccs) do
		UnAssignSubscriptionKeyOfVCCV2(vcc, key, keyspace)
	end
end

--[[
Keys programación (cuenta tito).
----
6c0aa9ff967b4833ad3018557272547e
867844dda8ac48a28d15cb31bce82c01
]]--
function GetVccOcpApimSubscriptionKey (vcc)
	assert(type(vcc) == "string")
	local subscriptionsKeys = GetSubscriptionsKeysQnAMaker(vcc)
	local total = utils.GetRowCount(subscriptionsKeys)
	return subscriptionsKeys, total
end

function GetVccOcpApimSubscriptionKeyV2 (vcc) -- fede
	assert(type(vcc) == "string")
	local subscriptionsKeys = GetSubscriptionsKeysQnAMakerV2(vcc)
	local total = utils.GetRowCount(subscriptionsKeys)
	return subscriptionsKeys, total
end
