-- Sklop Connect: a generic Roblox-to-HTTPS service client.
-- This module never calls Sklop endpoints or reads Sklop catalog, purchase, or account data.
-- Use only from a server Script. Put any service credential in Roblox Secrets.

local HttpService = game:GetService("HttpService")

local SklopConnect = {}
SklopConnect.__index = SklopConnect

local function validateOptions(options)
	if typeof(options) ~= "table" then
		return nil, "Pass a configuration table."
	end
	if typeof(options.baseUrl) ~= "string" or not options.baseUrl:match("^https://") then
		return nil, "baseUrl must be an HTTPS URL for your own API."
	end
	local baseUrl = options.baseUrl:gsub("/+$", "")
	if baseUrl:find("%s") then
		return nil, "baseUrl cannot contain spaces."
	end
	if options.secretName ~= nil and (typeof(options.secretName) ~= "string" or options.secretName == "") then
		return nil, "secretName must be a Roblox Secret name."
	end
	return {
		baseUrl = baseUrl,
		secretName = options.secretName,
		authHeader = options.authHeader or "Authorization",
		authPrefix = options.authPrefix == nil and "Bearer " or options.authPrefix,
	}
end

function SklopConnect.new(options)
	local config, err = validateOptions(options)
	if not config then
		error(err, 2)
	end
	return setmetatable(config, SklopConnect)
end

function SklopConnect:Request(method, path, payload, extraHeaders)
	method = string.upper(method or "GET")
	if not ({ GET = true, POST = true, PUT = true, PATCH = true, DELETE = true })[method] then
		return nil, "Unsupported HTTP method."
	end
	if typeof(path) ~= "string" or path:sub(1, 1) ~= "/" or path:sub(1, 2) == "//" or path:find("%s") then
		return nil, "path must be a relative API path beginning with one slash."
	end
	if extraHeaders ~= nil and typeof(extraHeaders) ~= "table" then
		return nil, "extraHeaders must be a table."
	end

	local headers = { Accept = "application/json" }
	if payload ~= nil then
		headers["Content-Type"] = "application/json"
	end
	for name, value in pairs(extraHeaders or {}) do
		if typeof(name) ~= "string" or typeof(value) ~= "string" then
			return nil, "Header names and values must be strings."
		end
		headers[name] = value
	end

	if self.secretName then
		local ok, secret = pcall(function()
			return HttpService:GetSecret(self.secretName)
		end)
		if not ok or not secret then
			return nil, "Could not read the configured Roblox Secret."
		end
		headers[self.authHeader] = secret:AddPrefix(self.authPrefix)
	end

	local ok, response = pcall(function()
		return HttpService:RequestAsync({
			Url = self.baseUrl .. path,
			Method = method,
			Headers = headers,
			Body = payload ~= nil and HttpService:JSONEncode(payload) or nil,
		})
	end)
	if not ok then
		return nil, "Request failed: " .. tostring(response)
	end
	if not response.Success then
		return nil, "Remote API returned HTTP " .. tostring(response.StatusCode) .. "."
	end
	if response.Body == "" then
		return {}, nil
	end
	local decoded, data = pcall(function()
		return HttpService:JSONDecode(response.Body)
	end)
	if not decoded then
		return nil, "Remote API returned invalid JSON."
	end
	return data, nil
end

function SklopConnect:Get(path, headers)
	return self:Request("GET", path, nil, headers)
end

function SklopConnect:Post(path, payload, headers)
	return self:Request("POST", path, payload, headers)
end

function SklopConnect:Put(path, payload, headers)
	return self:Request("PUT", path, payload, headers)
end

function SklopConnect:Delete(path, headers)
	return self:Request("DELETE", path, nil, headers)
end

return SklopConnect
