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

module(..., package.seeall)

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

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

--[[
-----------------------------
KNOWLEDGEBASE - GET OPERATION DETAILS
-----------------------------
	Gets details of a specific long running operation.

	Request URL
	------------
		https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/operations/{operationId}

	Request parameters
	------------
		operationId string Operation id.

	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
	------------
		{
		  "operationState": "Succeeded",
		  "createdTimestamp": "2018-03-19T07:38:46Z",
		  "lastActionTimestamp": "2018-03-19T07:39:29Z",
		  "resourceLocation": "https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/9d091697-fb8c-4ed5-9ac0-35bf8273bfff",
		  "userId": "86bb8390-56c0-42c2-9f81-3de161981191",
		  "operationId": "03a4f4ce-30a6-4ec6-b436-02bcdf6153e1"
		}

	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/operations_getoperationdetails
]]--

function GetOperationDetails (operationId, ocpApimSubscriptionKey)
	assert(type(ocpApimSubscriptionKey) == "string")
	assert(type(operationId) == "string")

	local uri = MakeCompleteURI("OPERATIONS", "%s", operationId) --/{operationId}/

	local success, response = request.Get(uri, ocpApimSubscriptionKey)
	if not success then
		LogError("[QNA_MAKER_OPERATIONS:GetOperationDetails] Retrieving Knowledgebase Operation '%s' Details. Error: '%s'", operationId, json.encode(response))
		return false, response
	end

	LogInfo("[QNA_MAKER_OPERATIONS:GetOperationDetails] Success Retrieved Knowledgebase Operation '%s' Details.", operationId)

	local encodedOperationDetail = table.concat(response)

	--LogDebug("[QNA_MAKER_OPERATIONS:GetOperationDetails] Retrieved Knowledgebase Operation '%s' Details, data: '%s'", operationId, encodedOperationDetail)
	return true, encodedOperationDetail
end
