------------------------------------------------------------
-- FLOATING DEBUG CONSOLE (Final Unified Version)
------------------------------------------------------------
WDIGI.Utils = WDIGI.Utils or {}

function WDIGI.Utils.CreateDebugConsole()
    if WDIGI.DebugFrame then return end

    local f = CreateFrame("Frame", "WDIGIDebugConsole", UIParent, "BackdropTemplate")
    f:SetSize(500, 300)
    f:SetPoint("CENTER")
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")

    -- Correct drag handlers (prevents mouse getting stuck)
    f:SetScript("OnDragStart", function(self)
        self:StartMoving()
    end)

    f:SetScript("OnDragStop", function(self)
        self:StopMovingOrSizing()
    end)

    f:Hide()

    --------------------------------------------------------
    -- BACKDROP
    --------------------------------------------------------
    f:SetBackdrop({
        bgFile = "Interface/Tooltips/UI-Tooltip-Background",
        edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
        tile = true, tileSize = 16, edgeSize = 16,
        insets = { left = 4, right = 4, top = 4, bottom = 4 }
    })
    f:SetBackdropColor(0, 0, 0, 0.85)

    --------------------------------------------------------
    -- CLOSE BUTTON
    --------------------------------------------------------
    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", f, "TOPRIGHT", -4, -4)

    --------------------------------------------------------
    -- SCROLL + EDITBOX
    --------------------------------------------------------
    local scroll = CreateFrame("ScrollFrame", nil, f, "UIPanelScrollFrameTemplate")
    scroll:SetPoint("TOPLEFT", 10, -10)
    scroll:SetPoint("BOTTOMRIGHT", -30, 10)

    local edit = CreateFrame("EditBox", nil, scroll)
    edit:SetMultiLine(true)
    edit:SetFontObject(ChatFontNormal)
    edit:SetWidth(450)
    edit:SetAutoFocus(false)
    edit:SetText("") -- start empty
    scroll:SetScrollChild(edit)

    f.EditBox = edit
    f.Scroll = scroll

    WDIGI.DebugFrame = f
end

------------------------------------------------------------
-- SHOW / HIDE CONSOLE
------------------------------------------------------------
function WDIGI.Utils.ShowDebugConsole()
    WDIGI.Utils.CreateDebugConsole()

    local text = table.concat(WDIGI.DebugLog or {}, "\n")
    WDIGI.DebugFrame.EditBox:SetText(text)
    WDIGI.DebugFrame:Show()
end

function WDIGI.Utils.HideDebugConsole()
    if WDIGI.DebugFrame then
        WDIGI.DebugFrame:Hide()
    end
end

------------------------------------------------------------
-- SAFE LOG APPEND (NO FPS DROP)
------------------------------------------------------------
function WDIGI.Utils.AppendToDebugLog(msg)
    WDIGI.DebugLog = WDIGI.DebugLog or {}
    table.insert(WDIGI.DebugLog, msg)

    -- Only update UI if visible (prevents FPS drops)
    if WDIGI.DebugFrame and WDIGI.DebugFrame:IsShown() then
        WDIGI.DebugFrame.EditBox:SetText(table.concat(WDIGI.DebugLog, "\n"))
    end
end
