--[[
    Cerebro - UISandbox Help Tab
    Extracted from UI/UISandbox.lua (Phase 2a decomposition)

    @module UI.Tabs.Help
    @version 0.12.0
]]--

local AddonName = "Cerebro"
local Cerebro = LibStub("AceAddon-3.0"):GetAddon(AddonName)
local AceGUI = LibStub("AceGUI-3.0")
local UISandbox = Cerebro:GetModule("UISandbox")

--[[=============================================
	Officer Tab: Cerebro Help
===============================================]]

function UISandbox:DrawHelpTab(container)
	if not Cerebro:IsOfficer() then
		local label = AceGUI:Create("Label")
		label:SetText("|cffff4444Officer access required.|r")
		label:SetFullWidth(true)
		container:AddChild(label)
		return
	end

	if not _G.UI_CommandRegistry then
		local label = AceGUI:Create("Label")
		label:SetText("|cffff4444Command Registry not available.|r")
		label:SetFullWidth(true)
		container:AddChild(label)
		return
	end

	-- Search bar (fixed at top, outside scroll)
	local searchGroup = AceGUI:Create("SimpleGroup")
	searchGroup:SetFullWidth(true)
	searchGroup:SetLayout("Flow")
	container:AddChild(searchGroup)

	self.helpSearchText = self.helpSearchText or ""
	local searchBox = AceGUI:Create("EditBox")
	searchBox:SetLabel("Search Commands (Enter/Esc to exit)")
	searchBox:SetWidth(300)
	searchBox:DisableButton(true)
	searchBox:SetText(self.helpSearchText)
	searchGroup:AddChild(searchBox)

	-- Main scrolling content area
	local mainGroup = AceGUI:Create("InlineGroup")
	mainGroup:SetTitle("")
	mainGroup:SetFullWidth(true)
	mainGroup:SetFullHeight(true)
	mainGroup:SetLayout("Fill")
	container:AddChild(mainGroup)

	local scroll = AceGUI:Create("ScrollFrame")
	scroll:SetFullWidth(true)
	scroll:SetFullHeight(true)
	scroll:SetLayout("Flow")
	mainGroup:AddChild(scroll)

	-- Render initial command list
	self:RenderHelpCommandList(scroll, self.helpSearchText)

	-- Store scroll reference for refresh
	self.helpScrollFrame = scroll

	-- Now set callbacks (after scroll exists)
	searchBox:SetCallback("OnTextChanged", function(widget, event, text)
		self.helpSearchText = text or ""
		-- Only refresh the scroll content, not the whole tab
		if self.helpScrollFrame then
			self.helpScrollFrame:ReleaseChildren()
			self:RenderHelpCommandList(self.helpScrollFrame, text or "")
		end
	end)
	searchBox:SetCallback("OnEnterPressed", function(widget)
		widget:ClearFocus()
	end)
	searchBox:SetCallback("OnEscapePressed", function(widget)
		widget:ClearFocus()
	end)
end

function UISandbox:RenderHelpCommandList(container, searchText)
	if not _G.UI_CommandRegistry then return end

	local commands = _G.UI_CommandRegistry:GetFiltered(searchText or "")

	for _, cmd in ipairs(commands) do
		local cmdGroup = AceGUI:Create("InlineGroup")
		cmdGroup:SetTitle(cmd.displayName or cmd.id)
		cmdGroup:SetFullWidth(true)
		cmdGroup:SetLayout("Flow")
		container:AddChild(cmdGroup)

		-- Description and slash syntax
		local descText = cmd.description or ""
		if cmd.slash and cmd.slash ~= "" then
			descText = descText .. "\n|cffaaaaaa" .. cmd.slash .. "|r"
		end

		local desc = AceGUI:Create("Label")
		desc:SetText(descText)
		desc:SetFullWidth(true)
		cmdGroup:AddChild(desc)

		-- Run button
		local runBtn = AceGUI:Create("Button")
		runBtn:SetText("Run")
		runBtn:SetWidth(100)
		runBtn:SetCallback("OnClick", function()
			-- Click-time RBAC check
			if cmd.officerOnly and not Cerebro:IsOfficerSafe() then
				Cerebro:Print("This command requires officer permissions.")
				return
			end

			local success, err = _G.UI_CommandRegistry:Execute(cmd.id, {})
			if not success then
				Cerebro:Print("Command failed: " .. tostring(err))
			end
		end)
		cmdGroup:AddChild(runBtn)
	end

	if #commands == 0 then
		local noResults = AceGUI:Create("Label")
		noResults:SetText("No commands found matching search.")
		noResults:SetColor(0.7, 0.7, 0.7)
		noResults:SetFullWidth(true)
		container:AddChild(noResults)
	end

	-- Bottom spacer
	local bottomSpacer = AceGUI:Create("Label")
	bottomSpacer:SetText(" ")
	bottomSpacer:SetFullWidth(true)
	container:AddChild(bottomSpacer)
end
