--[[--------------------------------------------------------------------
  PaladinHolyPower – ui/components/checkbox.lua
  Modern Checkbox Widget (ElvUI style: inside-border fill, no gap)
  Added CreateEnableCheckbox for enable toggles with green/red colors.
----------------------------------------------------------------------]]

local PHP = _G.PaladinHolyPower
if not PHP then return end

local CONST = {
    BOX_SIZE      = 15,
    BOX_COLOR     = { 0.102, 0.102, 0.102, 1 },
    BORDER_COLOR  = { 0, 0, 0, 1 },
    CHECK_COLOR   = { 0.22,  0.20,  0.14,  1 },
    LABEL_COLOR   = { 1, 1, 1 },
    LABEL_FONT    = "GameFontNormal",
    LABEL_OFFSET  = 1,
    CONTAINER_PAD = 4,
    MIN_WIDTH     = 80,
    BORDER_SIZE   = 1,
}

function PHP.CreateModernCheckbox(parent, labelText, checked, x, y, onClick, tooltipTitle, tooltipText)
    -- Measure label
    local measureFont = parent:CreateFontString(nil, "OVERLAY", CONST.LABEL_FONT)
    measureFont:SetText(labelText or "")
    local labelWidth = measureFont:GetStringWidth()
    measureFont:Hide()

    -- Container
    local containerWidth = CONST.BOX_SIZE + CONST.LABEL_OFFSET + labelWidth + CONST.CONTAINER_PAD * 2
    containerWidth = math.max(containerWidth, CONST.MIN_WIDTH)
    local container = CreateFrame("Button", nil, parent, "BackdropTemplate")
    container:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y)
    container:SetSize(containerWidth, CONST.BOX_SIZE + CONST.CONTAINER_PAD * 2)
    container:EnableMouse(true)

    -- Checkbox box
    local box = CreateFrame("Frame", nil, container, "BackdropTemplate")
    box:SetSize(CONST.BOX_SIZE, CONST.BOX_SIZE)
    box:SetPoint("LEFT", container, "LEFT", CONST.CONTAINER_PAD, 0)
    box:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = CONST.BORDER_SIZE,
    })
    box:SetBackdropColor(unpack(CONST.BOX_COLOR))
    box:SetBackdropBorderColor(unpack(CONST.BORDER_COLOR))

    -- Checkmark (fill: perfectly fits inside border)
    local checkInset = CONST.BORDER_SIZE
    local checkSize = CONST.BOX_SIZE - 2 * checkInset
    box.checkMark = box:CreateTexture(nil, "ARTWORK")
    box.checkMark:SetTexture("Interface\\Buttons\\WHITE8x8")
    box.checkMark:SetVertexColor(unpack(CONST.CHECK_COLOR))
    box.checkMark:SetSize(checkSize, checkSize)
    box.checkMark:SetPoint("CENTER", box, "CENTER", 0, 0)
    box.checkMark:Hide()

    -- Label
    local label = container:CreateFontString(nil, "OVERLAY", CONST.LABEL_FONT)
    label:SetPoint("LEFT", box, "RIGHT", CONST.LABEL_OFFSET, 0)
    label:SetText(labelText or "")
    label:SetTextColor(unpack(CONST.LABEL_COLOR))

    -- State logic
    container.checked = checked and true or false
    local function updateCheck()
        if container.checked then
            box.checkMark:Show()
        else
            box.checkMark:Hide()
        end
    end
    updateCheck()

    container:SetScript("OnClick", function()
        container.checked = not container.checked
        updateCheck()
        if onClick then onClick(container.checked) end
    end)

    -- Tooltip logic
    local function ShowTooltip()
        if PHP.Tooltip and PHP.Tooltip.Show and tooltipTitle then
            PHP.Tooltip.Show(container, tooltipTitle, tooltipText or "")
        end
    end
    local function HideTooltip()
        if PHP.Tooltip and PHP.Tooltip.Hide then
            PHP.Tooltip.Hide()
        end
    end
    container:SetScript("OnEnter", ShowTooltip)
    container:SetScript("OnLeave", HideTooltip)

    -- Public API (matches slider)
    container.checkbox = box
    container.label    = label
    container.GetValue = function() return container.checked end
    container.SetValue = function(_, v)
        container.checked = v and true or false
        updateCheck()
    end

    return container
end

-- New: CreateEnableCheckbox with green/red color toggling
function PHP.CreateEnableCheckbox(parent, labelText, checked, x, y, onToggle, tooltipTitle, tooltipText)
    local checkbox -- declare early for closure

    local function onClick(state)
        if checkbox.SetEnableCheckboxColors then
            checkbox:SetEnableCheckboxColors(state)
        end
        if onToggle then
            onToggle(state)
        end
    end

    checkbox = PHP.CreateModernCheckbox(parent, labelText, checked, x, y, onClick, tooltipTitle, tooltipText)

    function checkbox:SetEnableCheckboxColors(enabled)
        if enabled then
            self.checkbox.checkMark:SetVertexColor(0.31, 0.78, 0.47) -- green fill
            self.checkbox.checkMark:Show()
            self.label:SetTextColor(0.31, 0.78, 0.47)                -- green text
        else
            self.checkbox.checkMark:Hide()
            self.label:SetTextColor(0.91, 0.38, 0.37)                -- red text
        end
    end

    -- Initial color setup
    checkbox:SetEnableCheckboxColors(checked)

    return checkbox
end