-- =============================================================
-- File: modules/Auras/TriBadges.lua (with Test Mode & CORRECTED NAMESPACE)
-- =============================================================

-- Get the main addon table, ensuring it exists.
local AC = _G.ArenaCore
if not AC then return end

local existing = AC.TriBadges or {}
local M

if type(AC.RegisterModule) == "function" then
    M = AC:RegisterModule("TriBadges", existing)
else
    M = existing
end

AC.TriBadges = M

-- Test mode dummy icons
local DUMMY_ICONS = {
  "Interface\\Icons\\spell_deathknight_classicon", -- Slot 1 (Burst)
  "Interface\\Icons\\ability_warrior_defensivestance",   -- Slot 2 (Defensive)
  "Interface\\Icons\\spell_mage_arcaneorb",          -- Slot 3 (Control)
}

-- FIXED: Database function that works with ArenaCore's actual database structure
local function DB() 
  -- Try multiple paths to find the TriBadges settings
  if AC.DB and AC.DB.profile then
    -- Path 1: classPacks table (your actual structure)
    if AC.DB.profile.classPacks then
      return AC.DB.profile.classPacks
    end
    
    -- Path 2: Try triBadges directly
    if AC.DB.profile.triBadges then
      return AC.DB.profile.triBadges
    end
    
    -- Path 3: Try auras.triBadges
    if AC.DB.profile.auras and AC.DB.profile.auras.triBadges then
      return AC.DB.profile.auras.triBadges
    end
    
    -- Path 4: Try moreGoodies.triBadges
    if AC.DB.profile.moreGoodies and AC.DB.profile.moreGoodies.triBadges then
      return AC.DB.profile.moreGoodies.triBadges
    end
    
    -- Path 5: Create default settings if none exist - use classPacks path
    AC.DB.profile.classPacks = AC.DB.profile.classPacks or {
      enabled = true, -- Default to enabled
      size = 18,
      spacing = 2,
      anchor = "TOPLEFT",
      offsetX = -20,
      offsetY = -2
    }
    return AC.DB.profile.classPacks
  end
  
  -- Fallback: return default settings
  return {
    enabled = true,
    size = 18,
    spacing = 2,
    anchor = "TOPLEFT", 
    offsetX = -20,
    offsetY = -2
  }
end

local FONT = "Interface\\AddOns\\ArenaCore\\Media\\Fonts\\arenacore.ttf"
if not FONT or FONT == "" then FONT = STANDARD_TEXT_FONT end

local framesByUnit = {}
local UpdateAll -- Forward declaration so lifecycle handlers can call it

-- =====================================================================
-- Module lifecycle helpers
-- =====================================================================

function M:OnInit()
  local db = DB()
  if type(db) == "table" then
    if db.enabled == nil then db.enabled = true end
    db.size = db.size or 18
    db.spacing = db.spacing or 2
    db.anchor = db.anchor or "TOPLEFT"
    db.offsetX = db.offsetX or -20
    db.offsetY = db.offsetY or -2
  end
end

local function HideAllBadges()
  for unit, frame in pairs(framesByUnit) do
    if frame and frame.TriBadges then
      for i = 1, 3 do
        local badge = frame.TriBadges[i]
        if badge then
          if badge.timerUpdate then
            badge.timerUpdate:Cancel()
            badge.timerUpdate = nil
          end
          badge.cooldown:Clear()
          if badge.timer then badge.timer:SetText("") end
          badge:Hide()
          M:HideGlow(badge)
          M:StopTestPulse(badge)
        end
      end
    end
  end
end

local function TryAutoAttach()
  if not AC.GetFrameForUnit then
    return
  end

  local attachedCount = 0
  for i = 1, 5 do
    local unit = "arena" .. i
    local frame = AC.GetFrameForUnit(unit)
    if frame then
      M:Attach(frame, unit)
      attachedCount = attachedCount + 1
    end
  end
end

function M:_EnsureEventFrame()
  if self._eventFrame then
    return self._eventFrame
  end

  local frame = CreateFrame("Frame")
  frame:SetScript("OnEvent", function(_, event, ...)
    self:HandleEvent(event, ...)
  end)
  self._eventFrame = frame
  return frame
end

function M:_RegisterEvents()
  local frame = self:_EnsureEventFrame()
  frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  frame:RegisterEvent("ARENA_PREP_OPPONENT_SPECIALIZATIONS")
  frame:RegisterUnitEvent("UNIT_AURA", "arena1", "arena2", "arena3", "arena4", "arena5")
end

function M:_UnregisterEvents()
  if self._eventFrame then
    self._eventFrame:UnregisterAllEvents()
  end
end

function M:OnEnable()
  self:_RegisterEvents()

  -- Run an initial attach to cover test mode and immediate login scenarios.
  C_Timer.After(0.25, function()
    if AC.testModeEnabled then
      self:RefreshAll()
      return
    end
    TryAutoAttach()
    self:RefreshAll()
  end)
end

function M:OnDisable()
  self:_UnregisterEvents()
  HideAllBadges()
end

function M:OnProfileChanged()
  self:RefreshAll()
  self:UpdateTimerFontSize()
end

function M:HandleEvent(event, ...)
  if event == "PLAYER_ENTERING_WORLD" or event == "ARENA_PREP_OPPONENT_SPECIALIZATIONS" then
    C_Timer.After(0.25, function()
      if AC.testModeEnabled then
        self:RefreshAll()
        return
      end
      TryAutoAttach()
      self:RefreshAll()
    end)
  elseif event == "UNIT_AURA" then
    if AC.testModeEnabled then return end
    local unit = ...
    local frame = framesByUnit[unit] or (AC.GetFrameForUnit and AC.GetFrameForUnit(unit))
    if frame then
      framesByUnit[unit] = frame
      UpdateAll(frame, unit)
    end
  end
end

-- NEW: Animation helper for test mode pulse
function M:ApplyTestPulse(badge)
  if not badge or badge.animGroup then return end
  local ag = badge.icon:CreateAnimationGroup()
  ag:SetLooping("BOUNCE")
  local alpha = ag:CreateAnimation("Alpha")
  alpha:SetDuration(1.2)
  alpha:SetFromAlpha(0.6)
  alpha:SetToAlpha(1.0)
  alpha:SetSmoothing("IN_OUT")
  ag:Play()
  badge.animGroup = ag
end

function M:StopTestPulse(badge)
  if badge and badge.animGroup then
    badge.animGroup:Stop()
    badge.icon:SetAlpha(1.0)
    badge.animGroup = nil
  end
end

-- NEW FEATURE: Countdown timer system (like trinkets)
function M:StartCountdownTimer(badge, expirationTime)
  if not badge or not badge.timer or not expirationTime then return end
  
  -- Cancel existing timer if any
  if badge.timerUpdate then
    badge.timerUpdate:Cancel()
    badge.timerUpdate = nil
  end
  
  -- Update timer text every 0.1 seconds
  local function UpdateTimerText()
    if not badge or not badge.timer then return end
    
    local remaining = expirationTime - GetTime()
    if remaining > 0 then
      -- Format: M:SS for times over 60s, just seconds for under 60s
      local minutes = math.floor(remaining / 60)
      local seconds = math.floor(remaining % 60)
      local timeText
      if minutes > 0 then
        timeText = string.format("%d:%02d", minutes, seconds)
      else
        timeText = string.format("%.0f", remaining)
      end
      badge.timer:SetText(timeText)
      
      -- Schedule next update
      badge.timerUpdate = C_Timer.NewTimer(0.1, UpdateTimerText)
    else
      -- Timer expired
      badge.timer:SetText("")
      badge.timerUpdate = nil
    end
  end
  
  -- Start the timer
  UpdateTimerText()
end

-- NEW FEATURE: Update font size for all timer texts
function M:UpdateTimerFontSize()
  local fontSize = (AC.DB and AC.DB.profile and AC.DB.profile.classPacks and AC.DB.profile.classPacks.fontSize) or 10
  
  for unit, frame in pairs(framesByUnit) do
    if frame.TriBadges then
      for i = 1, 3 do
        local badge = frame.TriBadges[i]
        if badge and badge.timer then
          badge.timer:SetFont(FONT, fontSize, "OUTLINE")
        end
      end
    end
  end
end


local function CreateBadge(parent)
  -- Create styled badge using new classic system
  local f = AC:CreateStyledIcon(parent, 18, true, true) -- Default size, will be updated in Layout()
  
  -- Keep icon reference for compatibility
  f.icon = f.icon -- Already created by CreateStyledIcon
  
  -- Add cooldown frame (using helper to block OmniCC)
  local cd = AC:CreateCooldown(f, nil, "CooldownFrameTemplate")
  cd:SetAllPoints()
  cd:SetHideCountdownNumbers(true)
  cd:SetDrawEdge(false)
  -- CRITICAL: Make spiral very faint so icon is visible
  cd:SetSwipeColor(0, 0, 0, 0.3) -- Very faint black (30% opacity)
  
  -- CRITICAL: Exclude from OmniCC
  if _G.ArenaCore_ExcludeFromOmniCC then
    _G.ArenaCore_ExcludeFromOmniCC(cd, f)
  else
    -- Fallback if helper not loaded yet
    cd.noCooldownCount = true
    cd.noOCC = true
  end
  f.cooldown = cd
  
  -- Add stack count text (bottom-right corner)
  local count = f:CreateFontString(nil, "OVERLAY")
  count:SetFont(FONT, 10, "OUTLINE")
  count:SetPoint("BOTTOMRIGHT", -1, 1)
  count:SetJustifyH("RIGHT")
  f.count = count
  
  -- Add countdown timer text (center of icon) - NEW FEATURE
  local timer = cd:CreateFontString(nil, "OVERLAY")
  local fontSize = (AC.DB and AC.DB.profile and AC.DB.profile.classPacks and AC.DB.profile.classPacks.fontSize) or 10
  timer:SetFont(FONT, fontSize, "OUTLINE")
  timer:SetPoint("CENTER", cd, "CENTER", 0, 0)
  timer:SetTextColor(1, 1, 1, 1)
  cd.Text = timer
  f.timer = timer
  
  return f
end

local function Layout(frame)
  if not frame.TriBadges then return end
  local db = DB(); if not db then return end
  local size, spacing = db.size or 18, db.spacing or 2
  local growthDirection = db.growthDirection or "Vertical"
  
  for i=1,3 do
    local b = frame.TriBadges[i]
    b:SetSize(size, size)
    b:ClearAllPoints()
  end

  -- Default to the top-left anchor
  local anchor = db.anchor or "TOPLEFT"
  local ox, oy = db.offsetX or -20, db.offsetY or -2 -- Use current database values

  -- Anchor the first badge to the frame
  frame.TriBadges[1]:SetPoint(anchor, frame, anchor, ox, oy)
  
  -- Position remaining badges based on growth direction
  if growthDirection == "Horizontal" then
    -- Horizontal: Stack badges to the right
    frame.TriBadges[2]:SetPoint("LEFT", frame.TriBadges[1], "RIGHT", spacing, 0)
    frame.TriBadges[3]:SetPoint("LEFT", frame.TriBadges[2], "RIGHT", spacing, 0)
  else
    -- Vertical: Stack badges below (default)
    frame.TriBadges[2]:SetPoint("TOP", frame.TriBadges[1], "BOTTOM", 0, -spacing)
    frame.TriBadges[3]:SetPoint("TOP", frame.TriBadges[2], "BOTTOM", 0, -spacing)
  end
end

local function EnsureWidgets(frame)
  if frame.TriBadges then return end
  
  -- Create TriBadgesHolder first (for Z-order policy)
  if not frame.TriBadgesHolder then
    frame.TriBadgesHolder = CreateFrame("Frame", nil, frame)
    frame.TriBadgesHolder:SetAllPoints(frame)
    frame.TriBadgesHolder:SetFrameStrata("HIGH")
    frame.TriBadgesHolder:SetFrameLevel((frame:GetFrameLevel() or 10) + 30)
    frame.TriBadgesHolder:SetToplevel(false)
  end
  
  -- Parent badges to holder instead of frame
  frame.TriBadges = { 
    CreateBadge(frame.TriBadgesHolder), 
    CreateBadge(frame.TriBadgesHolder), 
    CreateBadge(frame.TriBadgesHolder) 
  }
  
  -- Set strata/level for each badge
  for i = 1, 3 do
    local b = frame.TriBadges[i]
    b:SetFrameStrata(frame.TriBadgesHolder:GetFrameStrata())
    b:SetFrameLevel(frame.TriBadgesHolder:GetFrameLevel() + 1)
  end
  
  Layout(frame)
  
  -- Hide all badges initially
  for i=1,3 do frame.TriBadges[i]:Hide() end
end

---Update all TriBadge icons for a specific arena frame.
---@param frame Frame
---@param unit string
UpdateAll = function(frame, unit)
  if not frame or not unit then return end
  EnsureWidgets(frame)

  -- Avoid re-running layout unless settings changed; RefreshAll handles layout updates.
  local db = DB()
  local isEnabled = false

  if AC.DB and AC.DB.profile then
    if AC.DB.profile.classPacks and AC.DB.profile.classPacks.enabled ~= false then
      isEnabled = true
    elseif not AC.DB.profile.classPacks then
      isEnabled = true
    end
  else
    isEnabled = true
  end

  if not isEnabled then
    if frame.TriBadges then
      for i = 1, 3 do
        frame.TriBadges[i]:Hide()
        M:StopTestPulse(frame.TriBadges[i])
        M:HideGlow(frame.TriBadges[i])
      end
    end
    return
  end

  local _, instanceType = IsInInstance()
  local isInArena = instanceType == "arena"
  local isInPrepRoom = isInArena and (GetNumArenaOpponentSpecs() > 0 and not UnitExists(unit))

  if isInPrepRoom then
    if frame.TriBadges then
      for i = 1, 3 do
        frame.TriBadges[i]:Hide()
        M:StopTestPulse(frame.TriBadges[i])
        M:HideGlow(frame.TriBadges[i])
      end
    end
    return
  end

  if AC.testModeEnabled then
    local arenaIndex = tonumber(unit:match("arena(%d)")) or 1
    local testSetups = {
      [1] = {class = "DEATHKNIGHT", spec = 3},
      [2] = {class = "MAGE", spec = 2},
      [3] = {class = "HUNTER", spec = 2}
    }
    local testSetup = testSetups[arenaIndex] or testSetups[1]

    local classData = nil
    if AC.DB and AC.DB.profile and AC.DB.profile.classPacks then
      classData = AC.DB.profile.classPacks[testSetup.class]
    end
    if not classData and AC.ClassPacks then
      classData = AC.ClassPacks[testSetup.class]
    end

    local specData = classData and classData[testSetup.spec]

    if specData then
      for i = 1, 3 do
        local b = frame.TriBadges[i]
        if b then
          local categoryData = specData[i]
          if categoryData and #categoryData > 0 then
            local spellEntry = categoryData[1]
            local spellID = type(spellEntry) == "table" and spellEntry[1] or spellEntry
            if spellID then
              local spellInfo = C_Spell.GetSpellInfo(spellID)
              if spellInfo and spellInfo.iconID then
                b.icon:SetTexture(spellInfo.iconID)
                b.count:SetText("")
                b:SetAlpha(1)
                b:Show()
                M:ApplyTestPulse(b)

                if arenaIndex == 3 and i == 2 and spellID == 186265 then
                  local function StartTestCountdown()
                    if not AC.testModeEnabled or not b or not b.cooldown then return end
                    local duration = 8
                    local startTime = GetTime()
                    local expirationTime = startTime + duration
                    b.cooldown:SetCooldown(startTime, duration)
                    M:StartCountdownTimer(b, expirationTime)
                    C_Timer.After(duration + 0.5, function()
                      if AC.testModeEnabled and b and b:IsVisible() then
                        StartTestCountdown()
                      end
                    end)
                  end
                  StartTestCountdown()
                else
                  if b.cooldown then b.cooldown:Clear() end
                  if b.timer then b.timer:SetText("") end
                end
              else
                b.icon:SetTexture(136071)
                b.count:SetText("")
                b:SetAlpha(1)
                b:Show()
                M:ApplyTestPulse(b)
              end
            else
              b:Hide()
            end
          else
            b:Hide()
          end
        end
      end
    else
      for i = 1, 3 do
        local b = frame.TriBadges[i]
        if b then
          b.icon:SetTexture(136071)
          b.count:SetText("")
          b:SetAlpha(1)
          b:Show()
          M:ApplyTestPulse(b)
        end
      end
    end
    return
  end

  for i = 1, 3 do
    M:StopTestPulse(frame.TriBadges[i])
    M:HideGlow(frame.TriBadges[i])
  end

  M:UpdateLiveArenaIcons(frame, unit)
end

local function UpdateOne(frame, unit, slot)
  -- Check if AC.PickTriBadge exists, if not create a simple implementation
  if not AC.PickTriBadge then
    AC.PickTriBadge = function(unit, slot)
      -- Simple implementation: return test data or basic spell info
      if AC.testModeEnabled then
        local testSpells = {
          [1] = {icon = "Interface\\Icons\\spell_deathknight_classicon", duration = 0, expirationTime = 0, applications = 1}, -- Burst
          [2] = {icon = "Interface\\Icons\\ability_warrior_defensivestance", duration = 0, expirationTime = 0, applications = 1}, -- Defensive  
          [3] = {icon = "Interface\\Icons\\spell_mage_arcaneorb", duration = 0, expirationTime = 0, applications = 1}  -- Control
        }
        return testSpells[slot]
      end
      
      -- For live mode, try to get class pack data
      local class = UnitClass(unit)
      if class and AC.ClassPacks and AC.ClassPacks[class] then
        -- Find the spec
        local specID = GetArenaOpponentSpec(tonumber(unit:match("arena(%d+)")) or 1)
        if specID then
          local _, _, _, _, _, specClass = GetSpecializationInfoByID(specID)
          if specClass == class then
            -- Look for spec-specific data
            for specKey, specData in pairs(AC.ClassPacks[class]) do
              if type(specData) == "table" and specData[slot] then
                local spellData = specData[slot]
                if type(spellData) == "table" and spellData[1] then
                  local spellID = spellData[1]
                  local spellInfo = C_Spell.GetSpellInfo(spellID)
                  if spellInfo then
                    return {
                      icon = spellInfo.iconID,
                      duration = 0,
                      expirationTime = 0,
                      applications = spellData[2] or 1
                    }
                  end
                end
              end
            end
          end
        end
      end
      
      return nil -- No aura found
    end
  end
  
  local aura = AC.PickTriBadge(unit, slot)
  local b = frame.TriBadges[slot]
  if aura then
    b.icon:SetTexture(aura.icon)
    local start = (aura.expirationTime or 0) - (aura.duration or 0)
    if (aura.duration or 0) > 0 then 
      b.cooldown:SetCooldown(start, aura.duration)
      -- Start countdown timer update (shows duration in center)
      M:StartCountdownTimer(b, aura.expirationTime)
    else 
      b.cooldown:Clear()
      if b.timer then b.timer:SetText("") end
    end
    -- FIXED: Don't show stack counts - countdown timers are the primary display
    -- Stack counts would overlap/interfere with countdown timers
    b.count:SetText("")
    b:Show()
  else
    b:Hide()
    if b.timer then b.timer:SetText("") end
  end
end

-- FIXED: UpdateAll function with proper database handling
local function UpdateAll(frame, unit)
  if not frame or not unit then return end
  -- UpdateAll called for " .. unit
  EnsureWidgets(frame)
  -- CRITICAL FIX: Don't call Layout() on every update - only call it when settings change
  -- Layout(frame) is now only called from RefreshAll() when user changes settings

  -- FIXED: Check enabled state from multiple possible database paths
  local db = DB()
  local isEnabled = false
  
  -- Check multiple possible database paths for enabled state
  if AC.DB and AC.DB.profile then
    -- Path 1: Direct classPacks.enabled
    if AC.DB.profile.classPacks and AC.DB.profile.classPacks.enabled ~= false then
      isEnabled = true
    -- Path 2: Default enabled if no setting exists
    elseif not AC.DB.profile.classPacks then
      isEnabled = true -- Default to enabled
    end
  else
    -- No database yet, default to enabled
    isEnabled = true
  end
  
  -- Enabled state: " .. tostring(isEnabled)
  
  if not isEnabled then
    -- TriBadges disabled, hiding badges
    if frame.TriBadges then 
      for i=1,3 do 
        frame.TriBadges[i]:Hide()
        M:StopTestPulse(frame.TriBadges[i])
        M:HideGlow(frame.TriBadges[i])
      end 
    end
    return
  end

  -- CRITICAL: Check arena state - hide in prep room, show only in test mode or when spells are active in live arena
  local _, instanceType = IsInInstance()
  local isInArena = instanceType == "arena"
  local isInPrepRoom = isInArena and (GetNumArenaOpponentSpecs() > 0 and not UnitExists(unit))
  
  -- PREP ROOM: Hide completely (user requirement)
  if isInPrepRoom then
    if frame.TriBadges then
      for i=1,3 do
        frame.TriBadges[i]:Hide()
        M:StopTestPulse(frame.TriBadges[i])
        M:HideGlow(frame.TriBadges[i])
      end
    end
    return
  end
  
  -- TEST MODE: Show static icons for preview (user requirement - keep as-is)
  if AC.testModeEnabled then
    -- Layout is handled by RefreshAll() when settings change, not on every update
    
    -- Test mode - showing real Class Pack spell icons matching class portraits
    local arenaIndex = tonumber(unit:match("arena(%d)")) or 1
    local testSetups = {
        [1] = {class = "DEATHKNIGHT", spec = 3}, -- Unholy Death Knight
        [2] = {class = "MAGE", spec = 2},        -- Fire Mage  
        [3] = {class = "HUNTER", spec = 2}       -- Marksmanship Hunter
    }
    local testSetup = testSetups[arenaIndex] or testSetups[1]

    -- Get class pack data from the real Class Packs system
    local classData = nil
    
    -- Try database first (user's customized data)
    if AC.DB and AC.DB.profile and AC.DB.profile.classPacks then
        classData = AC.DB.profile.classPacks[testSetup.class]
    end
    
    -- Fallback to default Class Packs data
    if not classData and AC.ClassPacks then
        classData = AC.ClassPacks[testSetup.class]
    end
    
    -- Get spec data
    local specData = classData and classData[testSetup.spec]
    
    if specData then
        -- Test mode using class and spec for frame
        
        for i=1,3 do
            local b = frame.TriBadges[i]
            if b then
                local categoryData = specData[i] -- i = slot (1=Burst, 2=Defensive, 3=Control)
                if categoryData and #categoryData > 0 then
                    -- Show the first (highest priority) spell in each category
                    local spellEntry = categoryData[1]
                    local spellID = type(spellEntry) == "table" and spellEntry[1] or spellEntry
                    local priority = type(spellEntry) == "table" and spellEntry[2] or 1
                    
                    if spellID then
                        local spellInfo = C_Spell.GetSpellInfo(spellID)
                        if spellInfo and spellInfo.iconID then
                            b.icon:SetTexture(spellInfo.iconID)
                            -- FIXED: Don't show priority numbers - timers will show duration instead
                            b.count:SetText("")
                            b:SetAlpha(1)
                            b:Show()
                            M:ApplyTestPulse(b)
                            
                            -- NEW FEATURE: Test mode countdown for Arena 3, slot 2 (Aspect of the Turtle)
                            -- This allows users to preview timer font size and see countdown in action
                            if arenaIndex == 3 and i == 2 and spellID == 186265 then
                                -- Aspect of the Turtle: 8 second duration with looping countdown
                                local function StartTestCountdown()
                                    if not AC.testModeEnabled then return end
                                    if not b or not b.cooldown then return end
                                    
                                    local duration = 8
                                    local startTime = GetTime()
                                    local expirationTime = startTime + duration
                                    
                                    -- Start cooldown spiral
                                    b.cooldown:SetCooldown(startTime, duration)
                                    
                                    -- Start countdown timer
                                    M:StartCountdownTimer(b, expirationTime)
                                    
                                    -- Loop the countdown after it expires (for continuous preview)
                                    C_Timer.After(duration + 0.5, function()
                                        if AC.testModeEnabled and b and b:IsVisible() then
                                            StartTestCountdown()
                                        end
                                    end)
                                end
                                
                                -- Start the looping countdown
                                StartTestCountdown()
                            else
                                -- Clear any existing cooldowns for other badges
                                if b.cooldown then
                                    b.cooldown:Clear()
                                end
                                if b.timer then
                                    b.timer:SetText("")
                                end
                            end
                            -- Spell icon set successfully
                        else
                            -- Fallback to sheep icon if spell not found
                            b.icon:SetTexture(136071)
                            b.count:SetText("")
                            b:SetAlpha(1)
                            b:Show()
                            M:ApplyTestPulse(b)
                            -- Using fallback icon
                        end
                    else
                        b:Hide()
                    end
                else
                    b:Hide()
                end
            end
        end
    else
        -- Fallback to old dummy icons if no class data found
        -- No spec data found, using fallback icons
        for i=1,3 do
            local b = frame.TriBadges[i]
            if b then
                b.icon:SetTexture(136071) -- Polymorph icon for testing
                b.count:SetText("")
                b:SetAlpha(1)
                b:Show()
                M:ApplyTestPulse(b)
            end
        end
    end
    return
  end

  -- Stop any leftover animations from test mode
  for i=1,3 do 
    M:StopTestPulse(frame.TriBadges[i])
    M:HideGlow(frame.TriBadges[i])
  end
  
  -- LIVE ARENA MODE: Only show icons when spells are ACTIVELY USED (user requirement)
  -- Icons start hidden and only appear when enemy uses the spell
  -- This is handled by OnSpellCast() which tracks active spells
  M:UpdateLiveArenaIcons(frame, unit)
end

-- ============================================================================
-- LIVE ARENA REAL-TIME SPELL TRACKING SYSTEM
-- ============================================================================

-- Track active spells per unit per category with priority system
-- Structure: activeSpells[unit][category] = {spellID = expirationTime}
local activeSpells = {}

-- Native WoW-style glow effect functions (manual implementation)
function M:ShowGlow(badge, color)
  if not badge then return end
  
  -- Create glow frame if it doesn't exist
  if not badge.glow then
    badge.glow = CreateFrame("Frame", nil, badge)
    -- CRITICAL: Position glow OUTSIDE the icon border for better visibility
    -- Extend 6 pixels beyond the badge on all sides
    badge.glow:SetPoint("TOPLEFT", badge, "TOPLEFT", -6, 6)
    badge.glow:SetPoint("BOTTOMRIGHT", badge, "BOTTOMRIGHT", 6, -6)
    badge.glow:SetFrameLevel(badge:GetFrameLevel() + 1)
    
    -- Create glow texture (golden glow effect)
    badge.glow.texture = badge.glow:CreateTexture(nil, "OVERLAY")
    badge.glow.texture:SetAllPoints()
    badge.glow.texture:SetTexture("Interface\\SpellActivationOverlay\\IconAlert")
    badge.glow.texture:SetBlendMode("ADD")
    badge.glow.texture:SetTexCoord(0.00781250, 0.50781250, 0.27734375, 0.52734375)
    
    -- Create animation group for pulsing glow
    badge.glow.animGroup = badge.glow.texture:CreateAnimationGroup()
    badge.glow.animGroup:SetLooping("REPEAT")
    
    -- Scale animation (pulse effect) - more subtle to keep icon visible
    local scale1 = badge.glow.animGroup:CreateAnimation("Scale")
    scale1:SetDuration(0.6)
    scale1:SetScale(1.2, 1.2) -- Reduced from 1.5 to 1.2 for subtler effect
    scale1:SetSmoothing("IN_OUT")
    
    local scale2 = badge.glow.animGroup:CreateAnimation("Scale")
    scale2:SetDuration(0.6)
    scale2:SetScale(0.833, 0.833) -- Back to 1.0 (1.2 * 0.833 = 1.0)
    scale2:SetSmoothing("IN_OUT")
    scale2:SetStartDelay(0.6)
    
    -- Alpha animation (fade in/out) - brighter for better visibility
    local alpha1 = badge.glow.animGroup:CreateAnimation("Alpha")
    alpha1:SetDuration(0.6)
    alpha1:SetFromAlpha(0.7) -- Increased from 0.5 for brighter glow
    alpha1:SetToAlpha(1.0)
    alpha1:SetSmoothing("IN_OUT")
    
    local alpha2 = badge.glow.animGroup:CreateAnimation("Alpha")
    alpha2:SetDuration(0.6)
    alpha2:SetFromAlpha(1.0)
    alpha2:SetToAlpha(0.7) -- Increased from 0.5 for brighter glow
    alpha2:SetSmoothing("IN_OUT")
    alpha2:SetStartDelay(0.6)
    
    badge.glow:Hide()
  end
  
  -- CRITICAL: Set glow color (blue for casts, default golden for instant)
  if color and type(color) == "table" and #color >= 3 then
    badge.glow.texture:SetVertexColor(color[1], color[2], color[3], color[4] or 1.0)
  else
    badge.glow.texture:SetVertexColor(1.0, 0.85, 0.0, 1.0) -- Default golden glow
  end
  
  -- Show and start animation
  badge.glow:Show()
  if badge.glow.animGroup then
    badge.glow.animGroup:Play()
  end
end

function M:HideGlow(badge)
  if badge and badge.glow then
    if badge.glow.animGroup then
      badge.glow.animGroup:Stop()
    end
    badge.glow:Hide()
  end
end

-- Update live arena icons based on actively used spells
function M:UpdateLiveArenaIcons(frame, unit)
  if not frame or not frame.TriBadges then return end
  
  -- CRITICAL FIX: Extract arena index from unit (e.g., "arena1" -> 1)
  local arenaIndex = tonumber(unit:match("arena(%d+)"))
  if not arenaIndex then return end
  
  -- CRITICAL FIX: Check if unit is dead - clear all active spells if dead
  if UnitIsDead(unit) then
    -- Clear all active spells for this unit
    if activeSpells[unit] then
      activeSpells[unit] = {}
    end
    
    -- Hide all badges and glows
    for i=1,3 do
      frame.TriBadges[i]:Hide()
      M:HideGlow(frame.TriBadges[i])
      M:StopTestPulse(frame.TriBadges[i])
    end
    return
  end
  
  -- Get spec ID for this unit
  local specID = GetArenaOpponentSpec(arenaIndex)
  
  if not specID or specID == 0 then
    -- No spec detected yet, hide all badges
    for i=1,3 do
      frame.TriBadges[i]:Hide()
      M:HideGlow(frame.TriBadges[i])
    end
    return
  end
  
  -- Get class from spec
  local _, _, _, _, _, classFile = GetSpecializationInfoByID(specID)
  if not classFile then return end
  
  -- Get class pack data for this class/spec
  local classData = nil
  if AC.DB and AC.DB.profile and AC.DB.profile.classPacks then
    classData = AC.DB.profile.classPacks[classFile]
  end
  
  if not classData and AC.ClassPacks then
    classData = AC.ClassPacks[classFile]
  end
  
  if not classData then
    -- No class pack data, hide all badges
    for i=1,3 do
      frame.TriBadges[i]:Hide()
      M:HideGlow(frame.TriBadges[i])
    end
    return
  end
  
  -- Find spec index (1, 2, 3, or 4 for Druid)
  local specIndex = nil
  for idx, data in pairs(classData) do
    if type(idx) == "number" and type(data) == "table" then
      -- This is a spec entry, check if it matches
      -- For now, use first available spec (we'll improve this with proper spec matching)
      if not specIndex then
        specIndex = idx
      end
    end
  end
  
  if not specIndex then
    for i=1,3 do
      frame.TriBadges[i]:Hide()
      M:HideGlow(frame.TriBadges[i])
    end
    return
  end
  
  local specData = classData[specIndex]
  if not specData then
    for i=1,3 do
      frame.TriBadges[i]:Hide()
      M:HideGlow(frame.TriBadges[i])
    end
    return
  end
  
  -- Initialize active spells table for this unit if needed
  if not activeSpells[unit] then
    activeSpells[unit] = {}
  end
  
  local currentTime = GetTime()
  
  -- CRITICAL FIX: Scan actual buffs on the unit using UnitAura
  -- This verifies that buffs are ACTUALLY active, not just tracked from combat log
  local verifiedBuffs = {}
  
  for i = 1, 40 do -- Scan up to 40 buffs
    local auraData = C_UnitAuras.GetBuffDataByIndex(unit, i)
    if not auraData then break end
    
    local spellID = auraData.spellId
    local expirationTime = auraData.expirationTime
    local duration = auraData.duration
    
    if spellID and expirationTime and expirationTime > 0 then
      verifiedBuffs[spellID] = {
        expirationTime = expirationTime,
        duration = duration
      }
    end
  end
  
  -- For each category (1=Burst, 2=Defensive, 3=Control)
  for category = 1, 3 do
    local badge = frame.TriBadges[category]
    local categoryData = specData[category]
    
    if not categoryData or #categoryData == 0 then
      badge:Hide()
      M:HideGlow(badge)
    else
      -- Find highest priority ACTIVE spell in this category
      local highestPrioritySpell = nil
      local highestPriority = 999
      local expirationTime = 0
      local duration = 0
      
      -- Check all spells in this category
      for _, spellEntry in ipairs(categoryData) do
        local spellID = type(spellEntry) == "table" and spellEntry[1] or spellEntry
        local priority = type(spellEntry) == "table" and spellEntry[2] or 1
        
        -- CRITICAL FIX: Only show if buff is ACTUALLY on the unit (verified by UnitAura)
        if verifiedBuffs[spellID] then
          local buffData = verifiedBuffs[spellID]
          
          -- Only consider if not expired
          if buffData.expirationTime > currentTime then
            -- Higher priority = lower number (1 is highest)
            if priority < highestPriority then
              highestPriority = priority
              highestPrioritySpell = spellID
              expirationTime = buffData.expirationTime
              duration = buffData.duration or (buffData.expirationTime - currentTime)
            end
          end
        end
      end
      
      -- Show the highest priority active spell, or hide if none active
      if highestPrioritySpell then
        local spellInfo = C_Spell.GetSpellInfo(highestPrioritySpell)
        if spellInfo and spellInfo.iconID then
          -- CRITICAL FIX: Only update if spell changed to prevent flickering
          local currentSpell = badge.currentSpellID
          if currentSpell ~= highestPrioritySpell then
            badge.icon:SetTexture(spellInfo.iconID)
            badge.currentSpellID = highestPrioritySpell
          end
          
          -- CRITICAL FIX: Don't show priority numbers - show countdown timers instead
          badge.count:SetText("")
          
          -- CRITICAL FIX: Start countdown timer (like trinkets/racials)
          if expirationTime and expirationTime > currentTime then
            local duration = expirationTime - currentTime
            badge.cooldown:SetCooldown(currentTime, duration)
            -- Start countdown timer update
            M:StartCountdownTimer(badge, expirationTime)
          else
            badge.cooldown:Clear()
            if badge.timer then badge.timer:SetText("") end
          end
          
          -- CRITICAL: Determine if spell is a cast or instant for glow color
          local isCastSpell = false
          local spellCastTime = C_Spell.GetSpellInfo(highestPrioritySpell)
          if spellCastTime and spellCastTime.castTime and spellCastTime.castTime > 0 then
            isCastSpell = true
          end
          
          badge:SetAlpha(1)
          -- CRITICAL FIX: Only call Show() if not already shown to prevent flickering
          if not badge:IsShown() then
            badge:Show()
          end
          
          -- Show glow border: BLUE for cast spells, DEFAULT for instant spells
          if isCastSpell then
            M:ShowGlow(badge, {0.3, 0.5, 1.0, 1.0}) -- Blue glow for cast spells
          else
            M:ShowGlow(badge) -- Default glow for instant spells
          end
          M:ApplyTestPulse(badge) -- Keep the pulse animation
        else
          -- CRITICAL FIX: Only call Hide() if currently shown to prevent flickering
          if badge:IsShown() then
            badge:Hide()
            M:HideGlow(badge)
          end
        end
      else
        -- No active spells in this category
        -- CRITICAL FIX: Only call Hide() if currently shown to prevent flickering
        if badge:IsShown() then
          badge:Hide()
          M:HideGlow(badge)
          M:StopTestPulse(badge)
        end
      end
    end
  end
end

-- Called from COMBAT_LOG when enemy uses a spell
function M:OnSpellCast(unit, spellID, eventType)
  if not unit or not spellID then return end
  if not activeSpells[unit] then
    activeSpells[unit] = {}
  end
  
  -- Get class and spec for this unit
  local arenaIndex = tonumber(unit:match("arena(%d)")) or 0
  if arenaIndex == 0 then return end
  
  local specID = GetArenaOpponentSpec(arenaIndex)
  if not specID or specID == 0 then return end
  
  local _, _, _, _, _, classFile = GetSpecializationInfoByID(specID)
  if not classFile then return end
  
  -- Get class pack data
  local classData = nil
  if AC.DB and AC.DB.profile and AC.DB.profile.classPacks then
    classData = AC.DB.profile.classPacks[classFile]
  end
  
  if not classData and AC.ClassPacks then
    classData = AC.ClassPacks[classFile]
  end
  
  if not classData then return end
  
  -- Find which category this spell belongs to
  for specIndex, specData in pairs(classData) do
    if type(specIndex) == "number" and type(specData) == "table" then
      for category = 1, 3 do
        local categoryData = specData[category]
        if categoryData then
          for _, spellEntry in ipairs(categoryData) do
            local trackedSpellID = type(spellEntry) == "table" and spellEntry[1] or spellEntry
            
            if trackedSpellID == spellID then
              -- Found it! Mark as active
              if not activeSpells[unit][category] then
                activeSpells[unit][category] = {}
              end
              
              -- CRITICAL: Category 3 (Control) spells like Polymorph should NOT have cooldown timers
              -- They only show while casting, handled by cast events
              -- Categories 1 (Burst) and 2 (Defensive) get cooldown timers
              
              local duration = 0
              if category ~= 3 then
                -- Burst and Defensive spells get cooldown tracking
                local spellInfo = C_Spell.GetSpellInfo(spellID)
                duration = 10 -- Default 10 seconds if we can't get duration
              end
              
              -- Set expiration time
              activeSpells[unit][category][spellID] = GetTime() + duration
              
              -- CRITICAL FIX: Only update the specific badge that changed (prevents flickering)
              local frame = framesByUnit[unit]
              if frame and frame.TriBadges and frame.TriBadges[category] then
                local badge = frame.TriBadges[category]
                local spellInfo = C_Spell.GetSpellInfo(spellID)
                
                if spellInfo and spellInfo.iconID then
                  -- Only update if icon actually changed (prevents unnecessary redraws)
                  local currentTexture = badge.icon:GetTexture()
                  if currentTexture ~= spellInfo.iconID then
                    badge.icon:SetTexture(spellInfo.iconID)
                  end
                  
                  badge.count:SetText("")
                  
                  -- Update cooldown (only for Burst/Defensive, not Control)
                  if duration > 0 then
                    badge.cooldown:SetCooldown(GetTime(), duration)
                    M:StartCountdownTimer(badge, GetTime() + duration)
                  else
                    -- Control spells: no cooldown, just show icon
                    badge.cooldown:Clear()
                    if badge.timer then badge.timer:SetText("") end
                  end
                  
                  badge:Show()
                end
              end
              
              return
            end
          end
        end
      end
    end
  end
end


function M:Attach(frame, unit)
  if not frame or not unit then return end
  EnsureWidgets(frame)
  framesByUnit[unit] = frame
  UpdateAll(frame, unit)
end

function M:RefreshAll()
  -- CRITICAL FIX: RefreshAll must work in BOTH test mode and live arena
  -- UpdateAll() handles test mode vs live mode internally (line 249)
  for unit, frame in pairs(framesByUnit) do 
    -- CRITICAL: Call Layout() here when settings change (not on every UNIT_AURA)
    Layout(frame)
    UpdateAll(frame, unit)
    
    -- CRITICAL: Also update live arena icons to handle dead units
    if not AC.testModeEnabled then
      M:UpdateLiveArenaIcons(frame, unit)
    end
  end
end

SLASH_ARENA_CORE_TRI1 = "/actri"
SlashCmdList["ARENA_CORE_TRI"] = function(msg)
  msg=(msg or ""):lower()
  local db=DB()
  if not db then 
    print("|cffFF4444TriBadges:|r No database found!")
    return 
  end
  local a,b=msg:match("(%S+)%s+(.*)")
  a=a or msg
  if a=="auto" then
    db.autoByEnemyClass=(b=="on")
    M:RefreshAll()
  elseif a=="size" then
    local n=tonumber(b)
    if n then db.size=math.max(12,min(28,math.floor(n))); M:RefreshAll() end
  elseif a=="enable" then
    db.enabled=(b=="on")
    print("|cff00FFFFTriBadges:|r Enabled = " .. tostring(db.enabled))
    M:RefreshAll()
  elseif a=="status" then
    print("|cff00FFFFTriBadges:|r Status - Enabled: " .. tostring(db.enabled) .. ", Size: " .. (db.size or "nil"))
  elseif a=="class" then
    local k=(b or ""):upper()
    if k~="" then AC.ApplyClassPack(k,{}); M:RefreshAll() end
  elseif a=="manual" then
    local k=(b or ""):upper()
    if k~="" then AC.ApplyClassPack(k,{setAsManual=true}); M:RefreshAll() end
  else
    print("|cff00FFFFTriBadges:|r Commands: enable on/off, status, size <number>")
  end
end