local SUI, L, Lib = SUI, SUI.L, SUI.Lib
---@class SUI.Handler.Profiles : SUI.Module
local module = SUI:NewModule('Handler.Profiles')
----------------------------------------------------------------------------------------------------

-- SpartanUI addon ID for LibAT ProfileManager
local SPARTANUI_ADDON_ID = 'spartanui'

-- Namespace blacklist
local namespaceblacklist = { 'LibDualSpec-1.0' }

---Get list of all SpartanUI module namespaces for registration
---@return string[] namespaces List of namespace names
local function GetNamespaceList()
	local namespaces = {}

	if SpartanUIDB and SpartanUIDB.namespaces then
		for namespaceName, _ in pairs(SpartanUIDB.namespaces) do
			if not SUI:IsInTable(namespaceblacklist, namespaceName) then
				table.insert(namespaces, namespaceName)
			end
		end
	end

	-- Sort alphabetically for consistent display
	table.sort(namespaces)

	return namespaces
end

---Open the LibAT ProfileManager in import mode for SpartanUI
function module:ImportUI()
	if LibAT and LibAT.ProfileManager then
		LibAT.ProfileManager:ShowImport(SPARTANUI_ADDON_ID)
	else
		SUI:Error('LibAT ProfileManager not available')
	end
end

---Open the LibAT ProfileManager in export mode for SpartanUI
function module:ExportUI()
	if LibAT and LibAT.ProfileManager then
		LibAT.ProfileManager:ShowExport(SPARTANUI_ADDON_ID)
	else
		SUI:Error('LibAT ProfileManager not available')
	end
end

function module:OnEnable()
	-- Register SpartanUI with LibAT ProfileManager
	if LibAT and LibAT.ProfileManager then
		local namespaces = GetNamespaceList()

		LibAT.ProfileManager:RegisterAddon({
			id = SPARTANUI_ADDON_ID,
			name = 'SpartanUI',
			db = SUI.SpartanUIDB,
			namespaces = namespaces,
			icon = 'Interface\\AddOns\\SpartanUI\\images\\Spartan-Helm',
		})
	else
		SUI:Error('LibAT ProfileManager not available - profile import/export disabled')
	end

	-- Register chat commands
	SUI:AddChatCommand('export', module.ExportUI, 'Export your settings')
	SUI:AddChatCommand('import', module.ImportUI, 'Import settings')
end
