
    -- Sets manabar color for default unit frames.

local function CustomManaColor(manaBar)
    local powerType = UnitPowerType(manaBar.unit)

    if powerType == 0 then
        manaBar:SetStatusBarColor(0.0, 0.55, 1.0)
    end
end

if UnitFrameManaBar_UpdateType then
    hooksecurefunc("UnitFrameManaBar_UpdateType", CustomManaColor)
end

    -- Set faction colors for Repuation Frame and tracking bar.

local TOOLTIP_FACTION_COLORS = {
    [1] = {r = 1, g = 0, b = 0},
    [2] = {r = 1, g = 0, b = 0},
    [3] = {r = 1, g = 1, b = 0},
    [4] = {r = 1, g = 1, b = 0},
    [5] = {r = 0, g = 1, b = 0},
    [6] = {r = 0, g = 1, b = 0},
    [7] = {r = 0, g = 1, b = 0},
    [8] = {r = 0, g = 1, b = 0},
}

local CUSTOM_FACTION_BAR_COLORS = {
    [1] = {r = 0.63, g = 0, b = 0},
    [2] = {r = 0.63, g = 0, b = 0},
    [3] = {r = 0.63, g = 0, b = 0},
    [4] = {r = 0.82, g = 0.67, b = 0},
    [5] = {r = 0.32, g = 0.67, b = 0},
    [6] = {r = 0.32, g = 0.67, b = 0},
    [7] = {r = 0.32, g = 0.67, b = 0},
    [8] = {r = 0, g = 0.75, b = 0.44},
}

-- Only hook if the function exists (legacy reputation frame)
if ReputationFrame_Update then
    hooksecurefunc("ReputationFrame_Update", function(showLFGPulse)
        local numFactions = GetNumFactions()
        local factionOffset = FauxScrollFrame_GetOffset(ReputationListScrollFrame)

        for i=1, NUM_FACTIONS_DISPLAYED, 1 do
            local factionIndex = factionOffset + i
            local factionBar = _G["ReputationBar"..i.."ReputationBar"]

            if factionIndex <= numFactions then
                local _, _, standingID, _, _, _, _, _, _, _, _, _, _, factionID = GetFactionInfo(factionIndex)

                local colorIndex = standingID

                local friendID = GetFriendshipReputation(factionID)

                if friendID ~= nil then
                    colorIndex = 5
                end

                local color = CUSTOM_FACTION_BAR_COLORS[colorIndex]
                factionBar:SetStatusBarColor(color.r, color.g, color.b)
            end
        end
    end)
end

if ReputationBarMixin and ReputationBarMixin.Update then
    hooksecurefunc(ReputationBarMixin, "Update", function(self)
        local _, reaction, _, _, _, factionID = GetWatchedFactionInfo()
        local colorIndex = reaction
        local friendshipID = GetFriendshipReputation(factionID)

        if friendshipID then
            colorIndex = 5
        end

        local color = CUSTOM_FACTION_BAR_COLORS[colorIndex]
        self:SetBarColor(color.r, color.g, color.b, 1)
    end)
end

-- ============================================================
-- Custom unit color logic (shared between override and post-hook)
-- ============================================================
local function GetCustomUnitColor(unit)
    local r, g, b

    if not unit then return 0.75, 0.75, 0.75 end
    if issecretvalue and issecretvalue(unit) then
        return 0.75, 0.75, 0.75
    end

    if UnitIsDead(unit) or UnitIsGhost(unit) or UnitIsTapDenied(unit) then
        r, g, b = 0.5, 0.5, 0.5
    elseif UnitIsPlayer(unit) then
        local _, class = UnitClass(unit)
        if class then
            if issecretvalue and issecretvalue(class) then
                r, g, b = 0.75, 0.75, 0.75
            elseif RAID_CLASS_COLORS[class] then
                r = RAID_CLASS_COLORS[class].r
                g = RAID_CLASS_COLORS[class].g
                b = RAID_CLASS_COLORS[class].b
            else
                r, g, b = 0.60, 0.60, 0.60
            end
        else
            if UnitIsFriend(unit, "player") then
                r, g, b = 0.60, 0.60, 0.60
            else
                r, g, b = 1, 0, 0
            end
        end
    elseif UnitPlayerControlled(unit) then
        if UnitCanAttack(unit, "player") then
            if not UnitCanAttack("player", unit) then
                r, g, b = 157/255, 197/255, 255/255
            else
                r, g, b = 1, 0, 0
            end
        elseif UnitCanAttack("player", unit) then
            r, g, b = 1, 1, 0
        elseif UnitIsPVP(unit) then
            r, g, b = 0, 1, 0
        else
            r, g, b = 157/255, 197/255, 255/255
        end
    else
        local reaction = UnitReaction(unit, "player")
        if reaction then
            if issecretvalue and issecretvalue(reaction) then
                r, g, b = 0.75, 0.75, 0.75
            elseif TOOLTIP_FACTION_COLORS[reaction] then
                r = TOOLTIP_FACTION_COLORS[reaction].r
                g = TOOLTIP_FACTION_COLORS[reaction].g
                b = TOOLTIP_FACTION_COLORS[reaction].b
            else
                r, g, b = 0.75, 0.75, 0.75
            end
        else
            r, g, b = 157/255, 197/255, 255/255
        end
    end

    return r, g, b
end

-- ============================================================
-- WoW 12.0+ (issecretvalue exists): Do NOT override GameTooltip_UnitColor.
-- Overriding it taints the entire tooltip data pipeline and causes:
--   "attempt to index local 'color' (a secret table value tainted by 'nCastbar')"
--
-- Instead, post-hook the tooltip via TooltipDataProcessor to
-- recolor the unit name line AFTER Blizzard's secure code has finished.
--
-- Pre-12.0: safe to override the global function directly.
-- ============================================================

if issecretvalue then
    -- 12.0+ path: post-hook tooltip unit display
    if TooltipDataProcessor and Enum and Enum.TooltipDataType then
        TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, function(tooltip, data)
            if tooltip ~= GameTooltip then return end
            local _, unit = tooltip:GetUnit()
            if not unit then return end
            local r, g, b = GetCustomUnitColor(unit)
            if not r then return end
            local textLeft1 = tooltip.TextLeft1 or _G[tooltip:GetName() .. "TextLeft1"]
            if textLeft1 then
                textLeft1:SetTextColor(r, g, b)
            end
        end)
    end
else
    -- Pre-12.0 path: safe to override globally
    function GameTooltip_UnitColor(unit) -- luacheck: ignore
        return GetCustomUnitColor(unit)
    end
end

    -- Override the name background on default unit frames.

if TargetFrame_CheckFaction then
    hooksecurefunc("TargetFrame_CheckFaction", function(self)
        if not self.unit then return end
        if issecretvalue and issecretvalue(self.unit) then return end
        if UnitPlayerControlled(self.unit) then
            local r, g, b = GetCustomUnitColor(self.unit)
            if r and self.nameBackground then
                self.nameBackground:SetVertexColor(r, g, b)
            end
        end
    end)
end
