-- ============================================================================
-- ARENACORE PROFILE MANAGER - Clean Version
-- ============================================================================

local AC = _G.ArenaCore
if not AC then return end

AC.ProfileManager = {}
local PM = AC.ProfileManager

function PM:Initialize()
    if self.initialized then return end
    
    -- CRITICAL FIX: Ensure the global database exists (for new users)
    if not _G.ArenaCoreDB then 
        _G.ArenaCoreDB = { profile = {} }
        print("|cffFFAA00ProfileManager:|r Created new ArenaCoreDB for first-time user")
    end
    
    -- CRITICAL FIX: If profile is a string (like "Default"), convert it to a table
    if type(_G.ArenaCoreDB.profile) ~= "table" then
        print("|cffFFAA00ProfileManager:|r WARNING - profile was type: " .. type(_G.ArenaCoreDB.profile) .. ", converting to table")
        _G.ArenaCoreDB.profile = {}
    end
    
    -- CRITICAL FIX: Set up AC.DB reference if it doesn't exist or is wrong type
    if not AC.DB or type(AC.DB.profile) ~= "table" then
        AC.DB = { profile = _G.ArenaCoreDB.profile }
        print("|cffFFAA00ProfileManager:|r Created AC.DB reference")
    end
    
    if not _G.ArenaCoreDB.profile.backups then
        _G.ArenaCoreDB.profile.backups = {}
    end
    if not _G.ArenaCoreDB.profile.textures then
        _G.ArenaCoreDB.profile.textures = {
            positioning = { horizontal = 56, vertical = 15, spacing = 2 },
            sizing = { healthWidth = 128, healthHeight = 18, resourceWidth = 136, resourceHeight = 8 },
            healthBarTexture = "Interface\\AddOns\\ArenaCore\\Media\\Frametextures\\texture1.tga",
            powerBarTexture = "Interface\\AddOns\\ArenaCore\\Media\\Frametextures\\texture1.tga",
            castBarTexture = "Interface\\AddOns\\ArenaCore\\Media\\Frametextures\\texture1.tga",
            useDifferentPowerBarTexture = true,
            useDifferentCastBarTexture = true
        }
        print("|cff22AA44ProfileManager:|r Created default textures structure (unified)")
    else
        -- Migrate legacy keys to unified structure if needed
        local t = _G.ArenaCoreDB.profile.textures
        if t.barPosition and not t.positioning then
            t.positioning = {
                horizontal = t.barPosition.horizontal or 56,
                vertical   = t.barPosition.vertical or 15,
                spacing    = t.barPosition.spacing or 2,
            }
        end
        if t.barSizing and not t.sizing then
            t.sizing = {
                healthWidth    = t.barSizing.healthWidth or 128,
                healthHeight   = t.barSizing.healthHeight or 18,
                resourceWidth  = t.barSizing.resourceWidth or 136,
                resourceHeight = t.barSizing.resourceHeight or 8,
            }
        end
    end
    
    -- Initialize textScale defaults if not exist
    if not _G.ArenaCoreDB.profile.textScale then
        _G.ArenaCoreDB.profile.textScale = {
            healthTextScale = 100,
            resourceTextScale = 100,
            spellTextScale = 100
        }
        print("|cff22AA44ProfileManager:|r Created default textScale structure")
    end
    self.initialized = true
    -- DEBUG: Profile Manager initialized
    -- print("|cff22AA44ArenaCore Profile Manager:|r Initialized")
end

function PM:UpdateUICheckboxStates()
    print("|cff00FF00ProfileManager:|r UpdateUICheckboxStates called")
    print("|cff00FF00ProfileManager:|r IsUIVisible = " .. tostring(AC.IsUIVisible))
    print("|cff00FF00ProfileManager:|r currentPage = " .. tostring(AC.currentPage))

    -- Check if UI is visible and on the right page
    local uiVisible = AC.configFrame and AC.configFrame:IsShown()
    local onArenaFramesPage = AC.currentPage == "ArenaFrames" or AC.__currentPage == "ArenaFrames"

    print("|cff00FF00ProfileManager:|r UI visible = " .. tostring(uiVisible))
    print("|cff00FF00ProfileManager:|r On ArenaFrames page = " .. tostring(onArenaFramesPage))

    if uiVisible and onArenaFramesPage then
        print("|cff00FF00ProfileManager:|r Refreshing UI checkboxes")
        -- Force recreation of the page to ensure checkboxes reflect database values
        C_Timer.After(0.2, function()
            if AC.__pageFrames and AC.__pageFrames["ArenaFrames"] then
                AC.__pageFrames["ArenaFrames"]:Hide()
                AC.__pageFrames["ArenaFrames"] = nil
            end
            AC.__currentPage = nil
            if AC.ShowPage then
                local result = AC:ShowPage("ArenaFrames")
                print("|cff00FF00ProfileManager:|r Checkbox refresh result: " .. tostring(result))
            end
        end)
    else
        print("|cff00FF00ProfileManager:|r Conditions not met for checkbox refresh")
    end
end

function PM:ForceCompleteUIRefresh()
    print("|cff00FF00ProfileManager:|r ForceCompleteUIRefresh called")
    
    if not AC.configFrame or not AC.configFrame:IsShown() then
        print("|cffFF0000ProfileManager:|r UI not visible, attempting to show ArenaFrames page")
        
        -- If UI is not visible, try to open it and show the page
        if AC.OpenConfigPanel then
            AC:OpenConfigPanel(false) -- Don't toggle, just open
            C_Timer.After(0.2, function()
                self:ForcePageRecreation("ArenaFrames")
            end)
        end
        return
    end

    -- UI is visible, force complete recreation
    print("|cff00FF00ProfileManager:|r UI is visible, forcing page recreation")
    self:ForcePageRecreation("ArenaFrames")
end

function PM:ForceSynchronousUIRefresh()
    -- Small delay to ensure database changes have taken effect
    C_Timer.After(0.05, function()
        -- Try to find and update existing checkboxes on the current page
        if AC.configFrame and AC.configFrame:IsShown() then
            -- Look for ArenaFrames page checkboxes and sliders and update them
            self:RefreshExistingCheckboxes()
            self:RefreshExistingSliders()
        else
            -- If config frame is not shown, try to force a page refresh
            if AC.ShowPage then
                AC:ShowPage("ArenaFrames")
                -- Try again after a delay
                C_Timer.After(0.1, function()
                    self:RefreshExistingCheckboxes()
                    self:RefreshExistingSliders()
                end)
            end
        end
    end)
end

function PM:RefreshExistingCheckboxes()
    -- Get the scroll child where checkboxes are located
    local scrollChild = AC.scrollChild
    if not scrollChild then
        return
    end
    
    -- Define the settings in the order they appear in the UI
    local settingsOrder = {
        "arenaFrames.general.statusText.enabled",
        "arenaFrames.general.statusText.usePercentage", 
        "arenaFrames.general.useClassColors",
        "arenaFrames.general.showNames",
        "arenaFrames.general.showArenaNumbers"
    }
    
    -- Look for checkbox elements and update them in order
    local checkboxIndex = 1
    local function findAndUpdateCheckboxes(frame)
        if not frame then return end
        
        -- Check if this frame has checkbox functionality
        if frame.checkButton and frame.checkButton.SetChecked and checkboxIndex <= #settingsOrder then
            local settingPath = settingsOrder[checkboxIndex]
            local newValue = self:GetSetting(settingPath)
            if newValue ~= nil then
                -- Delay the visual update to ensure it takes effect
                C_Timer.After(0.01, function()
                    frame:SetChecked(newValue)
                    
                    -- Update visual indicator if it exists
                    if frame.updateIndicator then
                        frame.updateIndicator()
                    end
                end)
                
                checkboxIndex = checkboxIndex + 1
            end
        end
        
        -- Recursively check child frames
        local children = {frame:GetChildren()}
        for _, child in ipairs(children) do
            findAndUpdateCheckboxes(child)
        end
    end
    
    findAndUpdateCheckboxes(scrollChild)
end

function PM:RefreshExistingSliders()
    -- Get the scroll child where sliders are located
    local scrollChild = AC.scrollChild
    if not scrollChild then
        return
    end
    
    -- Define the slider settings in the order they appear in the UI
    local sliderSettings = {
        "arenaFrames.general.playerNameX",
        "arenaFrames.general.playerNameY", 
        "arenaFrames.general.arenaNumberX",
        "arenaFrames.general.arenaNumberY",
        "arenaFrames.general.playerNameScale",
        "arenaFrames.general.arenaNumberScale",
        "arenaFrames.general.healthTextScale",
        "arenaFrames.general.resourceTextScale",
        "arenaFrames.general.spellTextScale",
        "arenaFrames.positioning.horizontal",
        "arenaFrames.positioning.vertical",
        "arenaFrames.positioning.spacing",
        "arenaFrames.sizing.scale",
        "arenaFrames.sizing.width",
        "arenaFrames.sizing.height"
    }
    
    -- Look for slider elements and update them in order
    local sliderIndex = 1
    local function findAndUpdateSliders(frame)
        if not frame then return end
        
        -- Check if this frame has slider functionality (look for slider.slider)
        if frame.slider and frame.slider.slider and frame.slider.slider.SetValue and sliderIndex <= #sliderSettings then
            local settingPath = sliderSettings[sliderIndex]
            local newValue = self:GetSetting(settingPath)
            if newValue ~= nil then
                -- Delay the visual update to ensure it takes effect
                C_Timer.After(0.01, function()
                    frame.slider.slider:SetValue(newValue)
                end)
                
                sliderIndex = sliderIndex + 1
            end
        end
        
        -- Recursively check child frames
        local children = {frame:GetChildren()}
        for _, child in ipairs(children) do
            findAndUpdateSliders(child)
        end
    end
    
    findAndUpdateSliders(scrollChild)
end

function PM:ForcePageRecreation(pageName)
    print("|cff00FF00ProfileManager:|r ForcePageRecreation called for: " .. pageName)
    
    -- Clear any cached page frame
    if AC.__pageFrames and AC.__pageFrames[pageName] then
        print("|cff00FF00ProfileManager:|r Clearing cached page frame: " .. pageName)
        AC.__pageFrames[pageName]:Hide()
        AC.__pageFrames[pageName] = nil
    end

    -- Clear current page to force recreation
    AC.__currentPage = nil
    AC.currentPage = nil

    -- Force recreation after a short delay
    C_Timer.After(0.1, function()
        -- Recreating page
        if AC.ShowPage then
            local result = AC:ShowPage(pageName)
            -- Page recreation completed
        else
            -- ShowPage function not available
        end
    end)
end

function PM:RefreshConfig()
    self:ApplyArenaFramesSettings()
    self:UpdateUICheckboxStates()
end

function PM:ApplyArenaFramesSettings()
    local currentDB = _G.ArenaCoreDB
    if not currentDB or not currentDB.profile.arenaFrames then 
        return 
    end
    
    local settings = currentDB.profile.arenaFrames
    local general = settings.general or {}
    
    -- CONNECT TO MASTER FRAME MANAGER SYSTEM
    if AC.UpdateFramePositions then
        AC:UpdateFramePositions()
    end
    if AC.MasterFrameManager and AC.MasterFrameManager.UpdateFramePositions then
        AC.MasterFrameManager:UpdateFramePositions()
    end
    if AC.UpdateFrameScale then
        AC:UpdateFrameScale()
    end
    if AC.UpdateFrameSize then
        AC:UpdateFrameSize()
    end
    
    if AC.arenaFrames then
        for i = 1, 3 do
            if AC.arenaFrames[i] then
                local frame = AC.arenaFrames[i]
                if general then
                    if frame.healthBar and frame.healthBar.statusText then
                        -- CRITICAL FIX: statusText is a boolean, not a table
                        local enabled = general.statusText
                        if enabled == nil then enabled = true end
                        frame.healthBar.statusText:SetShown(enabled)
                        if enabled then
                            local healthPct = math.floor((frame.healthBar:GetValue() or 70) / (select(2, frame.healthBar:GetMinMaxValues()) or 100) * 100)
                            -- Use general.usePercentage for percentage display (not statusText.usePercentage)
                            local usePct = general.usePercentage
                            if usePct == nil then usePct = true end
                            frame.healthBar.statusText:SetText(usePct and (healthPct .. "%") or frame.healthBar:GetValue() or 70)
                        end
                    end

                    if frame.manaBar and frame.manaBar.statusText then
                        -- CRITICAL FIX: statusText is a boolean, not a table
                        local enabled = general.statusText
                        if enabled == nil then enabled = true end
                        frame.manaBar.statusText:SetShown(enabled)
                        if enabled then
                            local manaPct = math.floor((frame.manaBar:GetValue() or 50) / (select(2, frame.manaBar:GetMinMaxValues()) or 100) * 100)
                            -- Use general.usePercentage for percentage display (not statusText.usePercentage)
                            local usePct = general.usePercentage
                            if usePct == nil then usePct = true end
                            frame.manaBar.statusText:SetText(usePct and (manaPct .. "%") or frame.manaBar:GetValue() or 50)
                        end
                    end

                    if frame.playerName then
                        local show = general.showNames
                        if show == nil then show = true end
                        frame.playerName:SetShown(show)
                        if show then
                            local x = general.playerNameX or 52
                            local y = general.playerNameY or 0
                            frame.playerName:ClearAllPoints()
                            frame.playerName:SetPoint("TOPLEFT", frame, "TOPLEFT", x, y)
                            local scale = (general.playerNameScale or 86) / 100
                            local size = math.max(6, math.floor((11 * scale) + 0.5))
                            -- Use SafeSetFont to prevent crashes
                            -- CRITICAL FIX: Use "OUTLINE" flag to match original font styling
                            if AC.SafeSetFont then
                                AC.SafeSetFont(frame.playerName, AC.FONT_PATH, size, "OUTLINE")
                            else
                                frame.playerName:SetFont("Fonts\\FRIZQT__.TTF", size, "OUTLINE")
                            end
                            frame.playerName:SetTextColor(1, 1, 1, 1)
                        end
                    end

                    if frame.arenaNumber then
                        local show = general.showArenaNumbers
                        if show == nil then show = true end
                        frame.arenaNumber:SetShown(show)
                        if show then
                            local x = general.arenaNumberX or 190
                            local y = general.arenaNumberY or -3
                            frame.arenaNumber:ClearAllPoints()
                            frame.arenaNumber:SetPoint("TOPLEFT", frame, "TOPLEFT", x, y)
                            local scale = (general.arenaNumberScale or 119) / 100
                            local size = math.max(6, math.floor((10 * scale) + 0.5))
                            -- Use SafeSetFont to prevent crashes
                            -- CRITICAL FIX: Use "OUTLINE" flag to match original font styling
                            if AC.SafeSetFont then
                                AC.SafeSetFont(frame.arenaNumber, AC.FONT_PATH, size, "OUTLINE")
                            else
                                frame.arenaNumber:SetFont("Fonts\\FRIZQT__.TTF", size, "OUTLINE")
                            end
                            frame.arenaNumber:SetTextColor(1, 1, 1, 1)
                        end
                    end

                    if frame.playerName and general.playerNameScale then
                        frame.playerName:SetScale(general.playerNameScale / 100)
                    end
                    if frame.arenaNumber and general.arenaNumberScale then
                        frame.arenaNumber:SetScale(general.arenaNumberScale / 100)
                    end
                    if frame.healthBar and frame.healthBar.statusText and general.healthTextScale then
                        frame.healthBar.statusText:SetScale(general.healthTextScale / 100)
                    end
                    if frame.manaBar and frame.manaBar.statusText and general.resourceTextScale then
                        frame.manaBar.statusText:SetScale(general.resourceTextScale / 100)
                    end
                    if frame.castBar and frame.castBar.Text and general.spellTextScale then
                        frame.castBar.Text:SetScale(general.spellTextScale / 100)
                    end
                end
            end
        end
    end
end

function PM:CreateBackup(name)
    local currentDB = _G.ArenaCoreDB
    if not currentDB then return false end
    name = name or ("Backup_" .. date("%Y%m%d_%H%M%S"))
    
    self:CaptureFramePositions()
    
    local function DeepCopy(t)
        if type(t) ~= "table" then return t end
        local c = {}
        for k, v in pairs(t) do c[k] = DeepCopy(v) end
        -- Copy metatable if it exists
        local mt = getmetatable(t)
        if mt then setmetatable(c, mt) end
        return c
    end
    
    -- Also initialize textScale in backup creation
    currentDB.profile.backups[name] = {
        arenaFrames = DeepCopy(currentDB.profile.arenaFrames),
        castBars = DeepCopy(currentDB.profile.castBars or {}),
        textures = DeepCopy(currentDB.profile.textures or {}),
        textScale = DeepCopy(currentDB.profile.textScale or {}),
        timestamp = time(),
        dateString = date("%Y-%m-%d %H:%M:%S")
    }
    
    -- Debug player name positioning
    if currentDB.profile.backups[name].arenaFrames.general then
        print("|cff00FF00CreateBackup:|r Player Name X/Y saved: " .. 
              tostring(currentDB.profile.backups[name].arenaFrames.general.playerNameX) .. " / " ..
              tostring(currentDB.profile.backups[name].arenaFrames.general.playerNameY))
    end
    
    print("|cff22AA44ArenaCore:|r Backup created: '" .. name .. "'")
    return true
end

function PM:RestoreBackup(name)
    local currentDB = _G.ArenaCoreDB
    if not currentDB or not currentDB.profile.backups then return false end
    
    local backup = currentDB.profile.backups[name]
    if not backup then
        print("|cffFF0000ArenaCore:|r Backup not found: '" .. name .. "'")
        return false
    end
    
    if backup.arenaFrames then
        -- Use DeepCopy to create a proper copy, not share references
        local function DeepCopy(t)
            if type(t) ~= "table" then return t end
            local c = {}
            for k, v in pairs(t) do c[k] = DeepCopy(v) end
            -- Copy metatable if it exists
            local mt = getmetatable(t)
            if mt then setmetatable(c, mt) end
            return c
        end
        
        _G.ArenaCoreDB = _G.ArenaCoreDB or {}
        _G.ArenaCoreDB.profile = _G.ArenaCoreDB.profile or {}
        _G.ArenaCoreDB.profile.arenaFrames = DeepCopy(backup.arenaFrames)
        _G.ArenaCoreDB.profile.castBars = DeepCopy(backup.castBars or {})
        _G.ArenaCoreDB.profile.textures = DeepCopy(backup.textures or {})
        _G.ArenaCoreDB.profile.textScale = DeepCopy(backup.textScale or {})
        
        -- Debug player name positioning restore
        if _G.ArenaCoreDB.profile.arenaFrames.general then
            print("|cff00FF00RestoreBackup:|r Player Name X/Y restored to: " .. 
                  tostring(_G.ArenaCoreDB.profile.arenaFrames.general.playerNameX) .. " / " ..
                  tostring(_G.ArenaCoreDB.profile.arenaFrames.general.playerNameY))
        end
        
        print("|cff22AA44ArenaCore:|r Restored from '" .. name .. "'")
        
        -- Apply settings and force UI refresh
        self:ApplyArenaFramesSettings()
        self:ForceSynchronousUIRefresh()
        
        -- Also refresh cast bars since they were restored
        if AC.RefreshCastBarsLayout then
            AC:RefreshCastBarsLayout()
            print("|cff22AA44ArenaCore:|r Cast bars restored and refreshed")
        end
        
        -- Use original UpdateBarPositions function for bar positions
        if AC.UpdateBarPositions then
            AC:UpdateBarPositions()
            print("|cff22AA44ArenaCore:|r Bar positions restored and refreshed")
        end
        
        -- Also refresh text scaling - DISABLED TO TEST
        -- if UpdateAllFrames then
        --     UpdateAllFrames()
        --     print("|cff22AA44ArenaCore:|r Text scaling restored and refreshed")
        -- end
        
        return true
    end
    return false
end

function PM:RestoreLatest()
    -- DEBUG: Add stack trace to see what's calling this
    -- RestoreLatest called
    
    local currentDB = _G.ArenaCoreDB
    if not currentDB or not currentDB.profile.backups then return false end
    
    print("|cff00FF00RestoreLatest:|r Available backups:")
    for name, backup in pairs(currentDB.profile.backups) do
        print("|cff00FF00RestoreLatest:|r   " .. name .. " (timestamp: " .. tostring(backup.timestamp) .. ")")
        if backup.arenaFrames and backup.arenaFrames.general then
            print("|cff00FF00RestoreLatest:|r     statusText.enabled = " .. tostring(backup.arenaFrames.general.statusText.enabled))
        end
    end
    
    local latest, latestTime = nil, 0
    for name, backup in pairs(currentDB.profile.backups) do
        if backup.timestamp and backup.timestamp > latestTime then
            latestTime = backup.timestamp
            latest = name
        end
    end
    
    if latest then
        print("|cff00FF00RestoreLatest:|r Selected latest backup: " .. latest .. " (time: " .. latestTime .. ")")
        print("|cff22AA44ArenaCore:|r Restoring latest: " .. latest)
        return self:RestoreBackup(latest)
    else
        print("|cffFF0000RestoreLatest:|r No backups found")
        return false
    end
end

function PM:CaptureFramePositions()
    local currentDB = _G.ArenaCoreDB
    if not currentDB or not _G.ArenaFramesAnchor then return end
    
    -- Capture main frame position
    local _, _, _, x, y = _G.ArenaFramesAnchor:GetPoint()
    if x and y then
        currentDB.profile.arenaFrames.positioning.horizontal = math.floor(x + 0.5)
        currentDB.profile.arenaFrames.positioning.vertical = math.floor(y + 0.5)
    end
    
    -- CRITICAL FIX: Capture CURRENT health/resource bar positions
    if _G.ArenaCore and _G.ArenaCore.arenaFrames and _G.ArenaCore.arenaFrames[1] then
        local frame = _G.ArenaCore.arenaFrames[1]
        
        -- Capture health bar position if it exists
        if frame.healthBar then
            local _, _, _, hx, hy = frame.healthBar:GetPoint()
            if hx and hy then
                -- Ensure textures.barPosition structure exists
                currentDB.profile.textures = currentDB.profile.textures or {}
                currentDB.profile.textures.barPosition = currentDB.profile.textures.barPosition or {}
                
                currentDB.profile.textures.barPosition.horizontal = math.floor(hx + 0.5)
                currentDB.profile.textures.barPosition.vertical = math.floor(hy + 0.5)
                
                -- Also capture spacing between health and resource bars
                if frame.manaBar then
                    local _, _, _, mx, my = frame.manaBar:GetPoint()
                    if mx and my then
                        local spacing = math.abs(hy - my)
                        currentDB.profile.textures.barPosition.spacing = math.floor(spacing + 0.5)
                    end
                end
                
                print("|cff00FF00ProfileManager:|r Captured health/resource bar positions - H:" .. hx .. ", V:" .. hy)
            end
        end
    end
end

function PM:GetSetting(path)
    -- CRITICAL FIX: Auto-initialize if not done yet (for early calls)
    if not self.initialized then
        self:Initialize()
    end
    
    -- Always use the current global database to avoid stale references
    local currentDB = _G.ArenaCoreDB
    if not currentDB then return nil end
    
    -- CRITICAL FIX: Ensure profile is a table, not a string
    if type(currentDB.profile) ~= "table" then
        print("|cffFF0000ProfileManager:|r ERROR in GetSetting - profile is type: " .. type(currentDB.profile) .. ", returning nil")
        return nil
    end
    
    local keys = {strsplit(".", path)}
    local value = currentDB.profile
    for _, key in ipairs(keys) do
        if not (value and value[key] ~= nil) then
            return nil
        end
        value = value[key]
    end
    return value
end

function PM:SetSetting(path, value, skipApply)
    -- CRITICAL FIX: Auto-initialize if not done yet (for early calls)
    if not self.initialized then
        self:Initialize()
    end
    
    -- Always use the current global database to avoid stale references
    local currentDB = _G.ArenaCoreDB
    if not currentDB then return end
    
    -- CRITICAL FIX: Ensure profile is a table, not a string
    if type(currentDB.profile) ~= "table" then
        print("|cffFF0000ProfileManager:|r ERROR - profile is type: " .. type(currentDB.profile) .. ", recreating as table")
        currentDB.profile = {}
        if AC.DB then
            AC.DB.profile = currentDB.profile
        end
    end
    
    -- DEBUG: Track all SetSetting calls for arenaFrames (DISABLED - uncomment for debugging)
    -- if path and path:match("^arenaFrames%.") then
    --     print("|cffFF00FF[PM DEBUG]|r SetSetting: path=" .. path .. ", skipApply=" .. tostring(skipApply))
    -- end
    
    local keys = {strsplit(".", path)}
    local current = currentDB.profile
    for i = 1, #keys - 1 do
        local key = keys[i]
        -- Ensure we have a table at each level
        if type(current[key]) ~= "table" then
            current[key] = {}
        end
        current = current[key]
    end
    
    current[keys[#keys]] = value
    
    -- Only apply when explicitly requested (skipApply = false or nil)
    local rootKey = keys[1]
    if rootKey == "arenaFrames" and not skipApply then
        -- DEBUG: Track when ApplyArenaFramesSettings is triggered (DISABLED - uncomment for debugging)
        -- print("|cffFFAA00[PM DEBUG]|r ApplyArenaFramesSettings TRIGGERED by path: " .. path)
        self:ApplyArenaFramesSettings()
    elseif rootKey == "textures" then
        if AC.RefreshTexturesLayout then AC:RefreshTexturesLayout() end
    elseif rootKey == "classPacks" or rootKey == "trinkets" or rootKey == "specIcons" or 
           rootKey == "racials" or rootKey == "classIcons" or rootKey == "castBars" or 
           rootKey == "diminishingReturns" or rootKey == "moreGoodies" then
        -- CRITICAL FIX: These settings have their own refresh systems in Helpers.lua
        -- Don't call ApplyArenaFramesSettings here - it causes main frames to resize/reposition
        -- Each system has its own RefreshXXXLayout function called from Helpers.lua callback
        -- Do nothing here to prevent double-refresh and frame jumping
    else
        -- Unknown setting - apply arena frames settings as fallback
        self:ApplyArenaFramesSettings()
    end
end

-- DEBUG: Profile Manager loaded
-- print("|cff8B45FFArenaCore Profile Manager:|r Loaded")

-- Debug test function
function PM:TestPlayerNameDebug()
    -- Test current database values
    local currentDB = _G.ArenaCoreDB
    if currentDB and currentDB.profile and currentDB.profile.arenaFrames and currentDB.profile.arenaFrames.general then
        print("|cff00FF00Player Name Debug:|r Current DB X/Y: " .. 
              tostring(currentDB.profile.arenaFrames.general.playerNameX) .. " / " ..
              tostring(currentDB.profile.arenaFrames.general.playerNameY))
    else
        print("|cffFF0000Player Name Debug:|r No current DB values found!")
    end
end

-- Make it globally accessible for testing
_G.TestPlayerNameDebug = function() AC.ProfileManager:TestPlayerNameDebug() end
