local addonName, NS = ...
local GR = NS.GR -- Reference the Core Frame

-- =========================================================================
-- UI: SHARED HELPERS
-- =========================================================================

local function CreateBg(f, r, g, b, a)
    local t = f:CreateTexture(nil, "BACKGROUND")
    t:SetAllPoints()
    t:SetColorTexture(r, g, b, a)
    f.bg = t
end

local function CreateBorder(f, r, g, b, a)
    -- Surgical Adjustment: Ghost Frame Implementation
    -- Prevents background overlap by using a transparent center child frame
    
    -- Cleanup legacy texture if present
    if f.border and not f.border.SetBackdrop then
        f.border:Hide()
        f.border = nil
    end

    if not f.border then
        local border = CreateFrame("Frame", nil, f, "BackdropTemplate")
        border:SetAllPoints()
        border:SetBackdrop({
            bgFile = nil,
            edgeFile = "Interface\\Buttons\\WHITE8x8",
            edgeSize = 1,
        })
        f.border = border
    end
    
    f.border:SetBackdropBorderColor(r, g, b, a)
end

-- [[ NEW: THICK BORDER HELPER (2px) ]]
local function CreateThickBorder(f, r, g, b, a, thickness)
    thickness = thickness or 2
    if f.thickBorder then 
        for _, t in ipairs(f.thickBorder) do t:Hide() end 
    end
    
    local lines = {}
    
    -- Top
    local t = f:CreateTexture(nil, "BORDER")
    t:SetPoint("TOPLEFT", 0, 0)
    t:SetPoint("TOPRIGHT", 0, 0)
    t:SetHeight(thickness)
    t:SetColorTexture(r,g,b,a)
    table.insert(lines, t)
    
    -- Bottom
    local b_line = f:CreateTexture(nil, "BORDER")
    b_line:SetPoint("BOTTOMLEFT", 0, 0)
    b_line:SetPoint("BOTTOMRIGHT", 0, 0)
    b_line:SetHeight(thickness)
    b_line:SetColorTexture(r,g,b,a)
    table.insert(lines, b_line)
    
    -- Left
    local l = f:CreateTexture(nil, "BORDER")
    l:SetPoint("TOPLEFT", 0, 0)
    l:SetPoint("BOTTOMLEFT", 0, 0)
    l:SetWidth(thickness)
    l:SetColorTexture(r,g,b,a)
    table.insert(lines, l)
    
    -- Right
    local ri = f:CreateTexture(nil, "BORDER")
    ri:SetPoint("TOPRIGHT", 0, 0)
    ri:SetPoint("BOTTOMRIGHT", 0, 0)
    ri:SetWidth(thickness)
    ri:SetColorTexture(r,g,b,a)
    table.insert(lines, ri)
    
    f.thickBorder = lines
end

local function SkinButton(btn)
    if not btn then return end
    
    btn:SetNormalTexture("")
    btn:SetHighlightTexture("")
    btn:SetPushedTexture("")
    btn:SetDisabledTexture("")
    
    if btn.Left then btn.Left:SetAlpha(0) end
    if btn.Middle then btn.Middle:SetAlpha(0) end
    if btn.Right then btn.Right:SetAlpha(0) end
    if btn.Top then btn.Top:SetAlpha(0) end
    if btn.Bottom then btn.Bottom:SetAlpha(0) end
    
    CreateBg(btn, 0.2, 0.2, 0.2, 1)
    CreateBorder(btn, 0, 0, 0, 1)
    
    btn:HookScript("OnEnter", function(s) 
        if s.bg then s.bg:SetColorTexture(0.3, 0.3, 0.3, 1) end 
    end)
    btn:HookScript("OnLeave", function(s) 
        if s.bg then s.bg:SetColorTexture(0.2, 0.2, 0.2, 1) end 
    end)
    btn:HookScript("OnMouseDown", function(s) 
        if s.bg then s.bg:SetColorTexture(0.1, 0.1, 0.1, 1) end 
    end)
    btn:HookScript("OnMouseUp", function(s) 
        if s.bg then s.bg:SetColorTexture(0.3, 0.3, 0.3, 1) end 
    end)
    
    local fs = btn:GetFontString()
    if fs then fs:SetTextColor(1, 1, 1) end
end

-- Export helpers to NS for global access
NS.CreateBg = CreateBg
NS.CreateBorder = CreateBorder
NS.CreateThickBorder = CreateThickBorder
NS.SkinButton = SkinButton