-- ============================================================================
-- Housing Decor Guide - UI Framework
-- Reusable UI components with theme support
-- Uses Theme Registry pattern for live theme switching
-- ============================================================================

local addonName, privateDB = ...

-- Use the addon's global namespace (created in HDG_Core.lua)
local HDG = _G.HousingDecorGuide

HDG.UI = {}

-- ============================================================================
-- CENTRALIZED BACKDROP CONSTANTS
-- ============================================================================

local BACKDROP_FLAT = {
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8",
    edgeSize = 1,
    insets = { left = 0, right = 0, top = 0, bottom = 0 }
}

local BACKDROP_BORDERLESS = {
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = nil,
    tile = false,
}

-- Expose backdrops for external use
HDG.UI.BACKDROP_FLAT = BACKDROP_FLAT
HDG.UI.BACKDROP_BORDERLESS = BACKDROP_BORDERLESS

-- ============================================================================
-- HELPER FUNCTIONS (delegating to ThemeEngine canonical helpers)
-- ============================================================================

local function GetScheme() return HDG.Theme.GetCurrentScheme() end
local function RegisterWidget(widget, widgetType) HDG.Theme.RegisterWidget(widget, widgetType) end

-- ============================================================================
-- WINDOW (Main Frame)
-- ============================================================================

function HDG.UI:Window(parent, width, height, titleText, dbKey)
    local scheme = GetScheme()
    local defaultWidth = width or 800
    local defaultHeight = height or 600
    local minWidth = math.floor(defaultWidth * 0.5)
    local minHeight = math.floor(defaultHeight * 0.5)
    local maxWidth = math.floor(defaultWidth * 2)
    local maxHeight = math.floor(defaultHeight * 2)

    local frame = CreateFrame("Frame", nil, parent or UIParent, "BackdropTemplate")
    frame:SetSize(defaultWidth, defaultHeight)
    frame:SetPoint("CENTER")
    frame:EnableMouse(true)
    frame:SetMovable(true)
    frame:RegisterForDrag("LeftButton")
    frame:SetFrameStrata("HIGH")  -- Consistent with main window (HDG_UI.lua)
    frame:SetToplevel(true)  -- Raise to top when clicked
    frame:SetClampedToScreen(true)

    -- Enable resizing
    frame:SetResizable(true)
    frame:SetResizeBounds(minWidth, minHeight, maxWidth, maxHeight)

    -- Store dbKey for layout persistence
    frame._dbKey = dbKey

    -- Restore saved layout if dbKey provided
    if dbKey and HDG.Store then
        local state = HDG.Store:GetState()
        if state and state.layout and state.layout[dbKey] then
            local layout = state.layout[dbKey]
            frame:ClearAllPoints()
            frame:SetPoint(layout.point or "CENTER", UIParent, layout.relPoint or "CENTER", layout.x or 0, layout.y or 0)
            frame:SetSize(layout.width or defaultWidth, layout.height or defaultHeight)
        end
    end

    -- Save layout on drag stop
    frame:SetScript("OnDragStart", function(self)
        self:StartMoving()
    end)
    frame:SetScript("OnDragStop", function(self)
        self:StopMovingOrSizing()
        if self._dbKey and HDG.Store then
            local point, _, relPoint, x, y = self:GetPoint()
            HDG.Store:Dispatch("UPDATE_LAYOUT", {
                frame = self._dbKey,
                width = self:GetWidth(),
                height = self:GetHeight(),
                point = point,
                relPoint = relPoint,
                x = x,
                y = y,
            })
        end
    end)

    frame:SetBackdrop(BACKDROP_FLAT)
    if scheme then
        frame:SetBackdropColor(scheme.bg.r, scheme.bg.g, scheme.bg.b, scheme.bg.a)
        frame:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
    end

    -- Title text
    if titleText then
        local title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        title:SetPoint("TOP", 0, -8)
        if scheme then
            HDG.Theme.ApplyFont(title, scheme, "header")
            title:SetTextColor(scheme.text_header.r, scheme.text_header.g, scheme.text_header.b, scheme.text_header.a)
        else
            title:SetFontObject("GameFontNormalLarge")
        end
        title:SetText(titleText)
        frame.title = title
    end

    -- ========================================================================
    -- RESIZE GRIP (Bottom-right corner)
    -- ========================================================================
    local resizeGrip = CreateFrame("Button", nil, frame)
    resizeGrip:SetSize(16, 16)
    resizeGrip:SetPoint("BOTTOMRIGHT", -2, 2)
    resizeGrip:EnableMouse(true)

    -- Resize grip texture (diagonal lines)
    local gripTexture = resizeGrip:CreateTexture(nil, "OVERLAY")
    gripTexture:SetAllPoints()
    gripTexture:SetTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Up")
    resizeGrip.texture = gripTexture

    -- Hover texture
    resizeGrip:SetScript("OnEnter", function(self)
        self.texture:SetTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Highlight")
    end)
    resizeGrip:SetScript("OnLeave", function(self)
        self.texture:SetTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Up")
    end)

    -- Resize behavior
    resizeGrip:SetScript("OnMouseDown", function(self, button)
        if button == "LeftButton" then
            frame:StartSizing("BOTTOMRIGHT")
        end
    end)
    resizeGrip:SetScript("OnMouseUp", function(self, button)
        frame:StopMovingOrSizing()
        if frame._dbKey and HDG.Store then
            local point, _, relPoint, x, y = frame:GetPoint()
            HDG.Store:Dispatch("UPDATE_LAYOUT", {
                frame = frame._dbKey,
                width = frame:GetWidth(),
                height = frame:GetHeight(),
                point = point,
                relPoint = relPoint,
                x = x,
                y = y,
            })
        end
        -- Trigger event for any listeners that need to respond to size change
        if HDG.EventBus then
            HDG.EventBus:Trigger("HDG_FRAME_RESIZED", { frame = frame._dbKey })
        end
    end)

    frame.resizeGrip = resizeGrip

    -- Register with theme engine
    RegisterWidget(frame, "Frame")

    return frame
end

-- ============================================================================
-- PANEL
-- ============================================================================

function HDG.UI:Panel(parent, config)
    config = config or {}
    local scheme = GetScheme()

    local panel = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    if config.width and config.height then
        panel:SetSize(config.width, config.height)
    end

    panel:SetBackdrop(BACKDROP_FLAT)
    if scheme then
        panel:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
        panel:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
    end

    -- Register with theme engine
    RegisterWidget(panel, "Panel")

    return panel
end

-- ============================================================================
-- BUTTON
-- ============================================================================

function HDG.UI:Button(parent, text, width, height, onClick)
    local scheme = GetScheme()

    local btn = CreateFrame("Button", nil, parent, "BackdropTemplate")
    btn:SetSize(width or 100, height or 24)
    btn:SetText(text or "")

    btn:SetBackdrop(BACKDROP_FLAT)
    if scheme then
        btn:SetBackdropColor(scheme.button_normal.r, scheme.button_normal.g, scheme.button_normal.b, scheme.button_normal.a)
        btn:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)

        local fs = btn:GetFontString()
        if fs then
            fs:SetTextColor(scheme.button_text_norm.r, scheme.button_text_norm.g, scheme.button_text_norm.b, scheme.button_text_norm.a)
            HDG.Theme.ApplyFont(fs, scheme, "body")
        end

        -- Store scheme reference for hover scripts
        btn._scheme = scheme
    end

    -- Hover effects
    btn:SetScript("OnEnter", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.button_hover.r, c.button_hover.g, c.button_hover.b, c.button_hover.a)
        end
    end)
    btn:SetScript("OnLeave", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.button_normal.r, c.button_normal.g, c.button_normal.b, c.button_normal.a)
        end
    end)

    if onClick then
        btn:SetScript("OnClick", onClick)
    end

    -- Register with theme engine
    RegisterWidget(btn, "Button")

    return btn
end

-- ============================================================================
-- LABEL (Text)
-- ============================================================================

function HDG.UI:Label(parent, text, isHeader)
    local scheme = GetScheme()

    local label = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    label.isHeader = isHeader

    if scheme then
        if isHeader then
            HDG.Theme.ApplyFont(label, scheme, "header")
            label:SetTextColor(scheme.text_header.r, scheme.text_header.g, scheme.text_header.b, scheme.text_header.a)
        else
            HDG.Theme.ApplyFont(label, scheme, "body")
            label:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b, scheme.text.a)
        end
    else
        label:SetFontObject(isHeader and "GameFontNormalLarge" or "GameFontNormal")
    end
    label:SetText(text or "")

    -- Register with theme engine
    RegisterWidget(label, "Text")

    return label
end

-- ============================================================================
-- PROGRESS BAR
-- ============================================================================

function HDG.UI:ProgressBar(parent, options)
    options = options or {}
    local width = options.width or 200
    local height = options.height or 16
    local scheme = GetScheme()

    local container = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    container:SetSize(width, height)
    container:SetBackdrop(BACKDROP_FLAT)

    if scheme then
        container:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
        container:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
    end

    -- Fill bar
    local fill = container:CreateTexture(nil, "ARTWORK")
    fill:SetTexture("Interface\\Buttons\\WHITE8x8")
    fill:SetPoint("TOPLEFT", 2, -2)
    fill:SetPoint("BOTTOMLEFT", 2, 2)
    fill:SetWidth(1)
    if scheme then
        fill:SetVertexColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, scheme.accent.a)
    end
    container.fill = fill

    -- Progress text
    local text = container:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    text:SetPoint("CENTER")
    if scheme then
        HDG.Theme.ApplyFont(text, scheme, "small")
        text:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b, scheme.text.a)
    else
        text:SetFontObject("GameFontNormalSmall")
    end
    container.text = text

    -- SetProgress method
    function container:SetProgress(current, max)
        local pct = max > 0 and (current / max) or 0
        pct = math.min(1, math.max(0, pct))

        local fillWidth = math.max(1, (self:GetWidth() - 4) * pct)
        self.fill:SetWidth(fillWidth)
        self.text:SetText(string.format("%d / %d", current, max))
    end

    -- Register with theme engine
    RegisterWidget(container, "ProgressBar")

    return container
end

-- ============================================================================
-- SCROLL FRAME
-- ============================================================================

function HDG.UI:ScrollFrame(parent)
    local scheme = GetScheme()

    local scrollFrame = CreateFrame("ScrollFrame", nil, parent, "UIPanelScrollFrameTemplate")
    scrollFrame:SetPoint("TOPLEFT", 0, 0)
    scrollFrame:SetPoint("BOTTOMRIGHT", -1, 0)

    -- Style scrollbar
    local scrollBar = scrollFrame.ScrollBar
    if scrollBar then
        scrollBar:SetWidth(8)

        local thumb = scrollBar:GetThumbTexture()
        if thumb then
            if scheme and scheme.atlas and scheme.atlas.scrollThumb then
                thumb:SetAtlas(scheme.atlas.scrollThumb)
            else
                thumb:SetTexture("Interface\\Buttons\\WHITE8x8")
                if scheme then
                    thumb:SetVertexColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, scheme.accent.a)
                end
            end
            thumb:SetSize(6, 40)
        end

        -- Hide scroll buttons
        if scrollBar.ScrollUpButton then
            scrollBar.ScrollUpButton:SetAlpha(0)
            scrollBar.ScrollUpButton:EnableMouse(false)
        end
        if scrollBar.ScrollDownButton then
            scrollBar.ScrollDownButton:SetAlpha(0)
            scrollBar.ScrollDownButton:EnableMouse(false)
        end
    end

    -- Content container
    local content = CreateFrame("Frame", nil, scrollFrame)
    content:SetWidth(scrollFrame:GetWidth())
    content:SetHeight(1) -- Will be updated dynamically
    scrollFrame:SetScrollChild(content)
    scrollFrame.content = content

    -- Register with theme engine
    RegisterWidget(scrollFrame, "ScrollFrame")

    return scrollFrame, content
end

-- ============================================================================
-- CHECKBOX
-- ============================================================================

function HDG.UI:Checkbox(parent, size, labelText)
    local scheme = GetScheme()
    size = size or 16

    local container = CreateFrame("Frame", nil, parent)
    container:SetSize(size + (labelText and 100 or 0), size)

    -- Checkbox box
    local box = CreateFrame("Button", nil, container, "BackdropTemplate")
    box:SetSize(size, size)
    box:SetPoint("LEFT", 0, 0)
    box:SetBackdrop(BACKDROP_FLAT)

    -- Box background texture
    local boxBg = box:CreateTexture(nil, "BACKGROUND")
    boxBg:SetAllPoints()
    boxBg:SetTexture("Interface\\Buttons\\WHITE8x8")
    if scheme then
        boxBg:SetColorTexture(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
    end
    container.boxBg = boxBg

    -- Checkmark
    local check = box:CreateTexture(nil, "OVERLAY")
    check:SetSize(size - 4, size - 4)
    check:SetPoint("CENTER")
    check:SetTexture("Interface\\COMMON\\Indicator-Green")
    check:Hide()
    container.check = check

    -- Label
    if labelText then
        local text = container:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        text:SetPoint("LEFT", box, "RIGHT", 6, 0)
        if scheme then
            HDG.Theme.ApplyFont(text, scheme, "small")
            text:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b, scheme.text.a)
        else
            text:SetFontObject("GameFontNormalSmall")
        end
        text:SetText(labelText)
        container.text = text
    end

    -- State
    container.checked = false

    -- Toggle function
    function container:SetChecked(checked)
        self.checked = checked
        if checked then
            self.check:Show()
        else
            self.check:Hide()
        end
    end

    function container:GetChecked()
        return self.checked
    end

    -- Click handler
    box:SetScript("OnClick", function()
        container:SetChecked(not container.checked)
        if container.onChanged then
            container.onChanged(container.checked)
        end
    end)

    -- Register with theme engine
    RegisterWidget(container, "Checkbox")

    return container
end

-- ============================================================================
-- HDG-SPECIFIC: RECIPE ROW
-- ============================================================================

function HDG.UI:RecipeRow(parent, recipe)
    local scheme = GetScheme()

    local row = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    row:SetHeight(24)
    row:SetBackdrop(BACKDROP_FLAT)

    row.isKnown = recipe and recipe.known or false

    if scheme then
        if row.isKnown then
            row:SetBackdropColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, 0.15)
        else
            row:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
        end
        row:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
        row._scheme = scheme
    end

    -- Recipe name
    local nameText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameText:SetPoint("LEFT", 8, 0)
    nameText:SetPoint("RIGHT", -80, 0)
    nameText:SetJustifyH("LEFT")
    if scheme then
        HDG.Theme.ApplyFont(nameText, scheme, "small")
        if row.isKnown then
            nameText:SetTextColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, scheme.accent.a)
        else
            nameText:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b, scheme.text.a)
        end
    else
        nameText:SetFontObject("GameFontNormalSmall")
    end
    if recipe then
        nameText:SetText(recipe.name or "Unknown")
    end
    row.nameText = nameText

    -- Profession text
    local professionText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    professionText:SetPoint("RIGHT", -8, 0)
    professionText:SetJustifyH("RIGHT")
    if scheme then
        HDG.Theme.ApplyFont(professionText, scheme, "small")
        professionText:SetTextColor(scheme.text_dim.r, scheme.text_dim.g, scheme.text_dim.b, scheme.text_dim.a)
    else
        professionText:SetFontObject("GameFontNormalSmall")
    end
    if recipe then
        professionText:SetText(recipe.profession or "")
    end
    row.professionText = professionText

    -- Hover effects
    row:EnableMouse(true)
    row:SetScript("OnEnter", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.button_hover.r, c.button_hover.g, c.button_hover.b, 0.3)
        end
    end)
    row:SetScript("OnLeave", function(self)
        local c = self._scheme or GetScheme()
        if c then
            if self.isKnown then
                self:SetBackdropColor(c.accent.r, c.accent.g, c.accent.b, 0.15)
            else
                self:SetBackdropColor(c.panel.r, c.panel.g, c.panel.b, c.panel.a)
            end
        end
    end)

    -- Register with theme engine
    RegisterWidget(row, "RecipeRow")

    return row
end

-- ============================================================================
-- HDG-SPECIFIC: MATERIAL ROW
-- ============================================================================

function HDG.UI:MaterialRow(parent, material)
    local scheme = GetScheme()

    local row = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    row:SetHeight(20)
    row:SetBackdrop(BACKDROP_FLAT)

    row.hasEnough = material and material.have >= material.need or false

    if scheme then
        row:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
        row:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
        row._scheme = scheme
    end

    -- Material name
    local nameText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameText:SetPoint("LEFT", 8, 0)
    nameText:SetPoint("RIGHT", -60, 0)
    nameText:SetJustifyH("LEFT")
    if scheme then
        HDG.Theme.ApplyFont(nameText, scheme, "small")
        nameText:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b, scheme.text.a)
    else
        nameText:SetFontObject("GameFontNormalSmall")
    end
    if material then
        nameText:SetText(material.name or "Unknown")
    end
    row.nameText = nameText

    -- Count text (have/need)
    local countText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    countText:SetPoint("RIGHT", -8, 0)
    countText:SetJustifyH("RIGHT")
    if scheme then
        HDG.Theme.ApplyFont(countText, scheme, "small")
        if row.hasEnough then
            countText:SetTextColor(scheme.success.r, scheme.success.g, scheme.success.b, scheme.success.a)
        else
            countText:SetTextColor(scheme.error.r, scheme.error.g, scheme.error.b, scheme.error.a)
        end
    else
        countText:SetFontObject("GameFontNormalSmall")
    end
    if material then
        countText:SetText(string.format("%d/%d", material.have or 0, material.need or 0))
    end
    row.countText = countText

    -- Hover effects
    row:EnableMouse(true)
    row:SetScript("OnEnter", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.button_hover.r, c.button_hover.g, c.button_hover.b, 0.3)
        end
    end)
    row:SetScript("OnLeave", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.panel.r, c.panel.g, c.panel.b, c.panel.a)
        end
    end)

    -- Register with theme engine
    RegisterWidget(row, "MaterialRow")

    return row
end

-- ============================================================================
-- HDG-SPECIFIC: LUMBER ROW
-- ============================================================================

function HDG.UI:LumberRow(parent, lumberData)
    local scheme = GetScheme()

    local row = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    row:SetHeight(20)
    row:SetBackdrop(BACKDROP_FLAT)

    local have = lumberData and lumberData.have or 0
    local need = lumberData and lumberData.need or 0
    row.hasEnough = have >= need

    if scheme then
        row:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
        row:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
        row._scheme = scheme
    end

    -- Lumber name
    local nameText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameText:SetPoint("LEFT", 8, 0)
    nameText:SetWidth(100)
    nameText:SetJustifyH("LEFT")
    if scheme then
        HDG.Theme.ApplyFont(nameText, scheme, "small")
        nameText:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b, scheme.text.a)
    else
        nameText:SetFontObject("GameFontNormalSmall")
    end
    if lumberData then
        nameText:SetText(lumberData.name or "Unknown")
    end
    row.nameText = nameText

    -- Needed text
    local neededText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    neededText:SetPoint("LEFT", nameText, "RIGHT", 8, 0)
    neededText:SetWidth(50)
    neededText:SetJustifyH("RIGHT")
    if scheme then
        HDG.Theme.ApplyFont(neededText, scheme, "small")
        neededText:SetTextColor(scheme.text_dim.r, scheme.text_dim.g, scheme.text_dim.b, scheme.text_dim.a)
    else
        neededText:SetFontObject("GameFontNormalSmall")
    end
    if lumberData then
        neededText:SetText(string.format("Need: %d", need))
    end
    row.neededText = neededText

    -- Have text
    local haveText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    haveText:SetPoint("RIGHT", -8, 0)
    haveText:SetJustifyH("RIGHT")
    if scheme then
        HDG.Theme.ApplyFont(haveText, scheme, "small")
        if row.hasEnough then
            haveText:SetTextColor(scheme.success.r, scheme.success.g, scheme.success.b, scheme.success.a)
        else
            haveText:SetTextColor(scheme.warning.r, scheme.warning.g, scheme.warning.b, scheme.warning.a)
        end
    else
        haveText:SetFontObject("GameFontNormalSmall")
    end
    if lumberData then
        haveText:SetText(string.format("Have: %d", have))
    end
    row.haveText = haveText

    -- Hover effects
    row:EnableMouse(true)
    row:SetScript("OnEnter", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.button_hover.r, c.button_hover.g, c.button_hover.b, 0.3)
        end
    end)
    row:SetScript("OnLeave", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.panel.r, c.panel.g, c.panel.b, c.panel.a)
        end
    end)

    -- Register with theme engine
    RegisterWidget(row, "LumberRow")

    return row
end

-- ============================================================================
-- HDG-SPECIFIC: FILTER BUTTON
-- ============================================================================

function HDG.UI:FilterButton(parent, text, isActive)
    local scheme = GetScheme()

    local btn = CreateFrame("Button", nil, parent, "BackdropTemplate")
    btn:SetSize(80, 22)
    btn:SetText(text or "Filter")
    btn:SetBackdrop(BACKDROP_FLAT)

    btn.isActive = isActive or false

    if scheme then
        if btn.isActive then
            btn:SetBackdropColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, 0.3)
            btn:SetBackdropBorderColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, 0.8)
        else
            btn:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
            btn:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
        end

        local fs = btn:GetFontString()
        if fs then
            if btn.isActive then
                fs:SetTextColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, scheme.accent.a)
            else
                fs:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b, scheme.text.a)
            end
            HDG.Theme.ApplyFont(fs, scheme, "small")
        end

        btn._scheme = scheme
    end

    -- SetActive method
    function btn:SetActive(active)
        self.isActive = active
        local c = self._scheme or GetScheme()
        if c then
            if active then
                self:SetBackdropColor(c.accent.r, c.accent.g, c.accent.b, 0.3)
                self:SetBackdropBorderColor(c.accent.r, c.accent.g, c.accent.b, 0.8)
            else
                self:SetBackdropColor(c.panel.r, c.panel.g, c.panel.b, c.panel.a)
                self:SetBackdropBorderColor(c.border.r, c.border.g, c.border.b, c.border.a)
            end
            local fs = self:GetFontString()
            if fs then
                if active then
                    fs:SetTextColor(c.accent.r, c.accent.g, c.accent.b, c.accent.a)
                else
                    fs:SetTextColor(c.text.r, c.text.g, c.text.b, c.text.a)
                end
            end
        end
    end

    -- Hover effects (only when not active)
    btn:SetScript("OnEnter", function(self)
        if not self.isActive then
            local c = self._scheme or GetScheme()
            if c then
                self:SetBackdropColor(c.button_hover.r, c.button_hover.g, c.button_hover.b, 0.3)
            end
        end
    end)
    btn:SetScript("OnLeave", function(self)
        if not self.isActive then
            local c = self._scheme or GetScheme()
            if c then
                self:SetBackdropColor(c.panel.r, c.panel.g, c.panel.b, c.panel.a)
            end
        end
    end)

    -- Register with theme engine
    RegisterWidget(btn, "FilterButton")

    return btn
end

-- ============================================================================
-- HDG-SPECIFIC: TAB BUTTON
-- ============================================================================

function HDG.UI:TabButton(parent, text, isActive)
    local scheme = GetScheme()

    local btn = CreateFrame("Button", nil, parent, "BackdropTemplate")
    btn:SetSize(90, 24)
    btn:SetText(text or "Tab")
    btn:SetBackdrop(BACKDROP_FLAT)

    btn.isActive = isActive or false

    if scheme then
        if btn.isActive then
            btn:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
        else
            btn:SetBackdropColor(scheme.bg.r, scheme.bg.g, scheme.bg.b, 0.6)
        end
        btn:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)

        local fs = btn:GetFontString()
        if fs then
            if btn.isActive then
                fs:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b, scheme.text.a)
            else
                fs:SetTextColor(scheme.text_dim.r, scheme.text_dim.g, scheme.text_dim.b, scheme.text_dim.a)
            end
            HDG.Theme.ApplyFont(fs, scheme, "small")
        end

        btn._scheme = scheme
    end

    -- SetActive method
    function btn:SetActive(active)
        self.isActive = active
        local c = self._scheme or GetScheme()
        if c then
            if active then
                self:SetBackdropColor(c.panel.r, c.panel.g, c.panel.b, c.panel.a)
            else
                self:SetBackdropColor(c.bg.r, c.bg.g, c.bg.b, 0.6)
            end
            local fs = self:GetFontString()
            if fs then
                if active then
                    fs:SetTextColor(c.text.r, c.text.g, c.text.b, c.text.a)
                else
                    fs:SetTextColor(c.text_dim.r, c.text_dim.g, c.text_dim.b, c.text_dim.a)
                end
            end
        end
    end

    -- Hover effects (only when not active)
    btn:SetScript("OnEnter", function(self)
        if not self.isActive then
            local c = self._scheme or GetScheme()
            if c then
                self:SetBackdropColor(c.button_hover.r, c.button_hover.g, c.button_hover.b, 0.3)
            end
        end
    end)
    btn:SetScript("OnLeave", function(self)
        if not self.isActive then
            local c = self._scheme or GetScheme()
            if c then
                self:SetBackdropColor(c.bg.r, c.bg.g, c.bg.b, 0.6)
            end
        end
    end)

    -- Register with theme engine
    RegisterWidget(btn, "TabButton")

    return btn
end

-- ============================================================================
-- HDG-SPECIFIC: PROFESSION HEADER (Collapsible)
-- ============================================================================

function HDG.UI:ProfessionHeader(parent, professionName, count, isCollapsed)
    local scheme = GetScheme()

    local header = CreateFrame("Button", nil, parent, "BackdropTemplate")
    header:SetHeight(28)
    header:SetBackdrop(BACKDROP_FLAT)

    if scheme then
        header:SetBackdropColor(scheme.bg.r, scheme.bg.g, scheme.bg.b, 0.8)
        header:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
        header._scheme = scheme
    end

    -- Accent bar on left
    local accentBar = header:CreateTexture(nil, "ARTWORK")
    accentBar:SetWidth(3)
    accentBar:SetPoint("TOPLEFT", 0, 0)
    accentBar:SetPoint("BOTTOMLEFT", 0, 0)
    accentBar:SetTexture("Interface\\Buttons\\WHITE8x8")
    if scheme then
        accentBar:SetVertexColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, scheme.accent.a)
    end
    header.accentBar = accentBar

    -- Collapse indicator
    local collapseText = header:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    collapseText:SetPoint("LEFT", 8, 0)
    if scheme then
        HDG.Theme.ApplyFont(collapseText, scheme, "small")
        collapseText:SetTextColor(scheme.text_dim.r, scheme.text_dim.g, scheme.text_dim.b, scheme.text_dim.a)
    else
        collapseText:SetFontObject("GameFontNormalSmall")
    end
    collapseText:SetText(isCollapsed and "+" or "-")
    header.collapseText = collapseText

    -- Profession name
    local nameText = header:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameText:SetPoint("LEFT", collapseText, "RIGHT", 6, 0)
    if scheme then
        HDG.Theme.ApplyFont(nameText, scheme, "body")
        nameText:SetTextColor(scheme.text_header.r, scheme.text_header.g, scheme.text_header.b, scheme.text_header.a)
    else
        nameText:SetFontObject("GameFontNormal")
    end
    nameText:SetText(professionName or "Profession")
    header.nameText = nameText

    -- Recipe count
    local countText = header:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    countText:SetPoint("RIGHT", -8, 0)
    if scheme then
        HDG.Theme.ApplyFont(countText, scheme, "small")
        countText:SetTextColor(scheme.text_dim.r, scheme.text_dim.g, scheme.text_dim.b, scheme.text_dim.a)
    else
        countText:SetFontObject("GameFontNormalSmall")
    end
    countText:SetText(count and string.format("(%d)", count) or "")
    header.countText = countText

    -- Collapsed state
    header.isCollapsed = isCollapsed or false

    function header:SetCollapsed(collapsed)
        self.isCollapsed = collapsed
        self.collapseText:SetText(collapsed and "+" or "-")
    end

    -- Hover effects
    header:SetScript("OnEnter", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.button_hover.r, c.button_hover.g, c.button_hover.b, 0.3)
        end
    end)
    header:SetScript("OnLeave", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.bg.r, c.bg.g, c.bg.b, 0.8)
        end
    end)

    -- Register with theme engine
    RegisterWidget(header, "ProfessionHeader")

    return header
end

-- ============================================================================
-- HDG-SPECIFIC: SHOPPING ITEM ROW
-- ============================================================================

function HDG.UI:ShoppingItem(parent, item)
    local scheme = GetScheme()

    local row = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    row:SetHeight(22)
    row:SetBackdrop(BACKDROP_FLAT)

    if scheme then
        row:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
        row:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
        row._scheme = scheme
    end

    -- Item name
    local nameText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameText:SetPoint("LEFT", 8, 0)
    nameText:SetPoint("RIGHT", -50, 0)
    nameText:SetJustifyH("LEFT")
    if scheme then
        HDG.Theme.ApplyFont(nameText, scheme, "small")
        nameText:SetTextColor(scheme.text.r, scheme.text.g, scheme.text.b, scheme.text.a)
    else
        nameText:SetFontObject("GameFontNormalSmall")
    end
    if item then
        nameText:SetText(item.name or "Unknown")
    end
    row.nameText = nameText

    -- Quantity text
    local qtyText = row:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    qtyText:SetPoint("RIGHT", -8, 0)
    qtyText:SetJustifyH("RIGHT")
    if scheme then
        HDG.Theme.ApplyFont(qtyText, scheme, "small")
        qtyText:SetTextColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, scheme.accent.a)
    else
        qtyText:SetFontObject("GameFontNormalSmall")
    end
    if item then
        qtyText:SetText(string.format("x%d", item.qty or 1))
    end
    row.qtyText = qtyText

    -- Hover effects
    row:EnableMouse(true)
    row:SetScript("OnEnter", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.button_hover.r, c.button_hover.g, c.button_hover.b, 0.3)
        end
    end)
    row:SetScript("OnLeave", function(self)
        local c = self._scheme or GetScheme()
        if c then
            self:SetBackdropColor(c.panel.r, c.panel.g, c.panel.b, c.panel.a)
        end
    end)

    -- Register with theme engine
    RegisterWidget(row, "ShoppingItem")

    return row
end

-- ============================================================================
-- TAB PANEL (v2.2.x - Standardized tab content panels)
-- Creates a complete panel structure for tab content
-- ============================================================================

function HDG.UI:TabPanel(parent, config)
    config = config or {}
    local scheme = GetScheme()
    local CONFIG = privateDB.CONFIG

    -- Get constants from CONFIG.TAB_PANEL
    local TAB = CONFIG.TAB_PANEL
    local BANNER_HEIGHT = CONFIG.BANNER_HEIGHT or 34
    local TAB_HEIGHT = CONFIG.TAB_HEIGHT or 32

    -- Calculate panel positioning
    local topOffset = -(BANNER_HEIGHT + TAB_HEIGHT + (TAB.TOP_OFFSET or 8))
    local sidePadding = TAB.SIDE_PADDING or CONFIG.SPACING.PANEL_PADDING or 4
    local bottomMargin = TAB.BOTTOM_MARGIN or 24

    -- Result object to return
    local result = {}

    -- ========================================================================
    -- MAIN PANEL FRAME
    -- ========================================================================
    local panel = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    panel:SetBackdrop(BACKDROP_FLAT)

    -- Handle single vs multi-column layout
    if config.layout == "columns" and config.columns then
        -- Multi-column: position based on columnIndex
        local colIndex = config.columnIndex or 1
        local colGap = TAB.COLUMN_GAP or 8
        local xOffset = sidePadding

        -- Calculate x offset based on previous columns
        for i = 1, colIndex - 1 do
            xOffset = xOffset + (config.columns[i].width or 200) + colGap
        end

        local colWidth = config.columns[colIndex].width or 200
        panel:SetPoint("TOPLEFT", xOffset, topOffset)
        panel:SetPoint("BOTTOMLEFT", xOffset, bottomMargin)
        panel:SetWidth(colWidth)
    else
        -- Single panel: full width
        panel:SetPoint("TOPLEFT", sidePadding, topOffset)
        panel:SetPoint("BOTTOMRIGHT", -sidePadding, bottomMargin)
    end

    if scheme then
        panel:SetBackdropColor(scheme.panel.r, scheme.panel.g, scheme.panel.b, scheme.panel.a)
        panel:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
    end

    RegisterWidget(panel, "Panel")
    result.frame = panel

    -- ========================================================================
    -- HEADER BAR (optional)
    -- ========================================================================
    if config.hasHeaderBar ~= false and config.title then
        local headerHeight = TAB.HEADER_HEIGHT or 24

        local headerBg = CreateFrame("Frame", nil, panel, "BackdropTemplate")
        headerBg:SetHeight(headerHeight)
        headerBg:SetPoint("TOPLEFT", 0, 0)
        headerBg:SetPoint("TOPRIGHT", 0, 0)
        headerBg:SetBackdrop(BACKDROP_FLAT)

        if scheme then
            local ph = scheme.panel_header or scheme.panel
            headerBg:SetBackdropColor(ph.r, ph.g, ph.b, ph.a or 1)
            headerBg:SetBackdropBorderColor(scheme.border.r, scheme.border.g, scheme.border.b, scheme.border.a)
        end

        RegisterWidget(headerBg, "PanelHeader")
        result.headerBg = headerBg

        -- Title text with wood sign background
        local title = headerBg:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        title:SetPoint("CENTER", 0, 0)
        if scheme then
            HDG.Theme.ApplyFont(title, scheme, "body")
            title:SetTextColor(scheme.text_header.r, scheme.text_header.g, scheme.text_header.b, scheme.text_header.a or 1)
        end
        title:SetText(config.title)

        -- Add wood sign background (fills header bar)
        HDG.Theme.StyleHeaderTitle(title, headerBg)
        result.title = title
    end

    -- ========================================================================
    -- SCROLL FRAME (optional)
    -- ========================================================================
    if config.hasScroll ~= false then
        local scrollTopOffset = config.hasHeaderBar ~= false and config.title
            and (TAB.SCROLL_TOP_WITH_HEADER or -30)
            or (TAB.SCROLL_TOP_NO_HEADER or -8)

        local scrollFrame = CreateFrame("ScrollFrame", nil, panel, "UIPanelScrollFrameTemplate")
        scrollFrame:SetPoint("TOPLEFT", TAB.SCROLL_LEFT_MARGIN or 8, scrollTopOffset)
        scrollFrame:SetPoint("BOTTOMRIGHT", TAB.SCROLL_RIGHT_MARGIN or -28, TAB.SCROLL_BOTTOM_MARGIN or 8)

        -- Style scrollbar
        local scrollBar = scrollFrame.ScrollBar
        if scrollBar then
            scrollBar:SetWidth(8)
            local thumb = scrollBar:GetThumbTexture()
            if thumb then
                if scheme and scheme.atlas and scheme.atlas.scrollThumb then
                    thumb:SetAtlas(scheme.atlas.scrollThumb)
                else
                    thumb:SetTexture("Interface\\Buttons\\WHITE8x8")
                    if scheme then
                        thumb:SetVertexColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, scheme.accent.a)
                    end
                end
                thumb:SetSize(6, 40)
            end
            -- Hide scroll buttons
            if scrollBar.ScrollUpButton then
                scrollBar.ScrollUpButton:SetAlpha(0)
                scrollBar.ScrollUpButton:EnableMouse(false)
            end
            if scrollBar.ScrollDownButton then
                scrollBar.ScrollDownButton:SetAlpha(0)
                scrollBar.ScrollDownButton:EnableMouse(false)
            end
        end

        -- Scroll child
        local scrollChild = CreateFrame("Frame", nil, scrollFrame)
        scrollChild:SetWidth(scrollFrame:GetWidth() or 200)
        scrollChild:SetHeight(1) -- Will be updated dynamically
        scrollFrame:SetScrollChild(scrollChild)

        RegisterWidget(scrollFrame, "ScrollFrame")
        result.scrollFrame = scrollFrame
        result.scrollChild = scrollChild
    end

    return result
end

-- ============================================================================
-- SECTION TITLE (v2.2.x - Standardized section headers)
-- Creates an accent-colored section title
-- ============================================================================

function HDG.UI:SectionTitle(parent, text, yOffset)
    local scheme = GetScheme()

    local title = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    if yOffset then
        title:SetPoint("TOPLEFT", 10, yOffset)
    end

    if scheme then
        HDG.Theme.ApplyFont(title, scheme, "header")
        title:SetTextColor(scheme.accent.r, scheme.accent.g, scheme.accent.b, scheme.accent.a or 1)
    end
    title:SetText(text or "")

    RegisterWidget(title, "SectionHeader")

    return title
end

-- ============================================================================
-- EXPOSE TO PRIVATEDB FOR OTHER MODULES
-- ============================================================================

privateDB.HDG_UI = HDG.UI
