-- ApiVault centralized C_AddOns runtime adapter. Added in v1.3.4.
-- Uses C_AddOns as the primary Midnight API surface. Legacy globals are intentionally not used.
local addonName, ApiVault = ...

ApiVault.AddOnRuntime = ApiVault.AddOnRuntime or {}
local Adapter = ApiVault.AddOnRuntime

local function safeCall(fn, ...)
	if type(fn) ~= "function" then
		return false, nil
	end
	return pcall(fn, ...)
end

function Adapter:IsAvailable()
	return type(C_AddOns) == "table"
end

function Adapter:GetNumAddOns()
	if type(C_AddOns) == "table" and type(C_AddOns.GetNumAddOns) == "function" then
		local ok, count = safeCall(C_AddOns.GetNumAddOns)
		if ok and type(count) == "number" then
			return count
		end
	end
	return 0
end

function Adapter:GetAddOnInfo(token)
	if type(C_AddOns) == "table" and type(C_AddOns.GetAddOnInfo) == "function" then
		local ok, a, b, c, d, e, f, g = safeCall(C_AddOns.GetAddOnInfo, token)
		if ok then
			if type(a) == "table" then
				return a.name, a.title, a.notes, a.loadable, a.reason, a.security, a.newVersion
			end
			return a, b, c, d, e, f, g
		end
	end
	return nil
end

function Adapter:GetAddOnName(token)
	local name = self:GetAddOnInfo(token)
	return name
end

function Adapter:GetAddOnMetadata(token, key)
	if type(C_AddOns) == "table" and type(C_AddOns.GetAddOnMetadata) == "function" then
		local ok, value = safeCall(C_AddOns.GetAddOnMetadata, token, key)
		if ok then
			return value
		end
	end
	return nil
end

function Adapter:IsAddOnLoaded(token)
	if type(C_AddOns) == "table" and type(C_AddOns.IsAddOnLoaded) == "function" then
		local ok, loaded = safeCall(C_AddOns.IsAddOnLoaded, token)
		if ok then
			return loaded and true or false
		end
	end
	return false
end

function Adapter:GetAddOnEnableState(token)
	if type(C_AddOns) == "table" and type(C_AddOns.GetAddOnEnableState) == "function" then
		local ok, state = safeCall(C_AddOns.GetAddOnEnableState, token)
		if ok then
			return state
		end
	end
	return nil
end

function Adapter:LoadAddOn(token)
	if type(C_AddOns) == "table" and type(C_AddOns.LoadAddOn) == "function" then
		local ok, loadedOrReason, reason = safeCall(C_AddOns.LoadAddOn, token)
		return ok, loadedOrReason, reason
	end
	return false, "C_AddOns.LoadAddOn unavailable"
end

function Adapter:FindAddOn(query)
	query = tostring(query or "")
	if query == "" then
		return nil
	end

	local name, title, notes, loadable, reason, security, newVersion = self:GetAddOnInfo(query)
	if name then
		return {
			index = query,
			name = name,
			title = title,
			notes = notes,
			loadable = loadable,
			reason = reason,
			security = security,
			newVersion = newVersion,
			source = "direct",
		}
	end

	local needle = string.lower(query)
	for index = 1, self:GetNumAddOns() do
		name, title, notes, loadable, reason, security, newVersion = self:GetAddOnInfo(index)
		if name then
			local hay = string.lower(tostring(name) .. " " .. tostring(title or ""))
			if string.lower(name) == needle or string.find(hay, needle, 1, true) then
				return {
					index = index,
					name = name,
					title = title,
					notes = notes,
					loadable = loadable,
					reason = reason,
					security = security,
					newVersion = newVersion,
					source = "scan",
				}
			end
		end
	end

	return nil
end

function Adapter:GetStats()
	return self:GetNumAddOns(), self:IsAvailable()
end
