-- Who.lua

local function UpdateWhoButtonVisibility()
    if not BlackBox_WhoButton then return end
    if IsInGroup() and UnitIsGroupLeader("player") then
        BlackBox_WhoButton:Show()
    else
        BlackBox_WhoButton:Hide()
    end
end

-- Create the Who button
local whoButton = CreateFrame("Button", "BlackBox_WhoButton", TimerFrame, "UIPanelButtonTemplate")
whoButton:SetSize(40, 20)
whoButton:SetPoint("LEFT", titleText, "RIGHT", 6, 0)
whoButton:SetText("Who")
whoButton:SetNormalFontObject("GameFontNormalSmall")
whoButton:Hide() -- Hide it by default

-- What happens when the button is clicked (you guessed it, it Who's)
whoButton:SetScript("OnClick", function()
    SendWho("pvp") -- You can change the filter here
end)

-- Tooltip (because the button wasn’t obvious enough)
whoButton:SetScript("OnEnter", function(self)
    GameTooltip:SetOwner(self, "ANCHOR_CURSOR")
    GameTooltip:SetText("Click to run /who 'pvp'. Only visible to group leaders.", 1, 1, 1, true)
    GameTooltip:Show()
end)

whoButton:SetScript("OnLeave", function(self)
    GameTooltip:Hide()
end)

-- Monitor group changes so the button can gracefully vanish when you're demoted
local whoMonitor = CreateFrame("Frame")
whoMonitor:RegisterEvent("GROUP_ROSTER_UPDATE")
whoMonitor:SetScript("OnEvent", UpdateWhoButtonVisibility)

-- Also update on load
UpdateWhoButtonVisibility()