local json = require "json"
local request = require "qna_maker.request"

module(..., package.seeall)

local function GetBaseURI (id)
	id = id or "ENDPOINTKEYS"
	local api = require "qna_maker"
	return api.GetBaseURI(id)
end

local function MakeCompleteURI (uriBaseId, uri, ...)
	uriBaseId = uriBaseId or "ENDPOINTKEYS"
	local api = require "qna_maker"
	return api.MakeCompleteURI(uriBaseId, uri, ...)
end

--[[
-----------------------------
KNOWLEDGEBASE - GET ENDPOINT KEYS
-----------------------------
	Get Endpoint Keys

	Request URL
	------------
		https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/endpointkeys

	Request parameters
	------------
		....

	Request headers
	------------
		Ocp-Apim-Subscription-Key string Subscription key which provides access to this API. Found in your Cognitive Services accounts.

	Request body
	------------
		...
	Response 200
	------------
		response with endpoint info in it.
		{
		  "primaryEndpointKey": "73e88a14-694a-44d5-883b-184a68aa8530",
		  "secondaryEndpointKey": "b2c98c16-ca31-4294-8626-6c57454a5063",
		  "installedVersion": "4.1.0",
		  "lastStableVersion": "4.2.0"
		}

	Response 400
	------------
		application/json
		{
		  "error": {
			"code": "BadArgument",
			"message": "Invalid input. See details.",
			"details": [
			  {
				"code": "ValidationFailure",
				"message": "'Name' should not be empty.",
				"target": "Name"
			  }
			]
		  }
		}
	Response 401
	------------
		application/json
		{
		  "error": {
			"code": "Unauthorized",
			"message": "Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key."
		  }
		}
	Response 403
	------------
		application/json
		{
		  "error": {
			"code": "Forbidden",
			"message": "You do not have permissions to view the requested resource."
		  }
		}
	Response 404
	------------
		application/json
		{
		  "error": {
			"code": "NotFound",
			"message": "The requested knowledge base is not found."
		  }
		}
	Response 500
	------------
		application/json
		{
		  "error": {
			"code": "Unspecified",
			"message": "Something happened. Please retry after some time."
		  }
		}

	Referencia: https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/endpointkeys_getendpointkeys
]]--

function GetEndpointKeys (ocpApimSubscriptionKey)
	assert(type(ocpApimSubscriptionKey) == "string")
	local uri = GetBaseURI("ENDPOINTKEYS")

	local success, response = request.Get(uri, ocpApimSubscriptionKey)
	if not success then
		LogError("[QNA_MAKER_ENDPOINTKEYS:GetEndpointKeys] Retrieving Endpoint Keys. Error: '%s'", json.encode(response))
		return false, response
	end

	LogInfo("[QNA_MAKER_ENDPOINTKEYS:GetEndpointKeys] Retrieved Endpoint Keys.")

	local encodedEndpointKeys = table.concat(response)

	--LogDebug("[QNA_MAKER_ENDPOINTKEYS:GetEndpointKeys] Retrieving Endpoint Keys, data: '%s'", encodedEndpointKeys)
	return true, encodedEndpointKeys
end

function GetEndpointKeysV2 (ocpApimSubscriptionKey) -- fede
	assert(type(ocpApimSubscriptionKey) == "string")
	local uri = GetBaseURI("ENDPOINTKEYS")

	local success, response = request.Get(uri, ocpApimSubscriptionKey)
	if not success then
		LogError("[QNA_MAKER_ENDPOINTKEYS:GetEndpointKeysV2] Retrieving Endpoint Keys. Error: '%s'", json.encode(response))
		return false, response
	end

	LogInfo("[QNA_MAKER_ENDPOINTKEYS:GetEndpointKeysV2] Retrieved Endpoint Keys.")

	local encodedEndpointKeys = table.concat(response)

	--LogDebug("[QNA_MAKER_ENDPOINTKEYS:GetEndpointKeysV2] Retrieving Endpoint Keys, data: '%s'", encodedEndpointKeys)
	return true, encodedEndpointKeys
end
