-- ============================================================================
-- pom/pvp/pvp_db.lua
-- PvP DB wrapper (no Ace)
-- Owns PlateOMaticDB.pvp (defaults + getters/setters)
-- ============================================================================

local ADDON_NAME, ns = ...

ns = _G[ADDON_NAME] or ns
_G[ADDON_NAME] = ns

ns.pvp_db = ns.pvp_db or {}
local DB = ns.pvp_db

-- ------------------------------------------------------------
-- Defaults (NO SKINS)
-- ------------------------------------------------------------
DB.DEFAULTS = DB.DEFAULTS or {
    -- IMPORTANT: default OFF so it never bleeds into PvE
    enabled = false,

    useArena = true,
    useBG    = true,
    useWorld = false,

    -- Visuals
    classColorBars   = true,  -- class colors on enemy players
    greyHPBackground = true,  -- grey background INSIDE the HP bar

    -- Borders
    showTargetBorder  = true,
    showFocusBorder   = true,
    targetBorderColor = { r=1.0, g=0.82, b=0.0, a=1.0 },
    focusBorderColor  = { r=0.0, g=0.8,  b=1.0, a=1.0 },

    -- Arena numbers
    arenaNumbersEnabled = true,
    arenaNumbersOnly123 = true,

    -- NEW: Arena numbers layout (red 1-5 above nameplate)
    arenaNumbersSize = 18,
    arenaNumbersX    = 0,
    arenaNumbersY    = 8,

    -- NEW: Healer marker (green square above the red number)
    healerMarkEnabled = true,
    healerMarkSize    = 12,
    healerMarkX       = 0,
    healerMarkY       = 16,  -- extra Y ABOVE numbers

    -- Target CC (best-effort icon)
    showTargetCC = true,

    -- Behavior
    disableThreatInPvP = true,
}

-- ------------------------------------------------------------
-- Deep merge (copy missing keys only)
-- No dependency on global CopyTable
-- ------------------------------------------------------------
local function ShallowCopyTable(t)
local o = {}
if type(t) == "table" then
    for k, v in pairs(t) do
        if type(v) == "table" then
            local c = {}
            for kk, vv in pairs(v) do c[kk] = vv end
                o[k] = c
                else
                    o[k] = v
                    end
                    end
                    end
                    return o
                    end

                    local function MergeMissing(dst, src)
                    if type(dst) ~= "table" or type(src) ~= "table" then return end
                        for k, v in pairs(src) do
                            if dst[k] == nil then
                                dst[k] = (type(v) == "table") and ShallowCopyTable(v) or v
                                elseif type(dst[k]) == "table" and type(v) == "table" then
                                    MergeMissing(dst[k], v)
                                    end
                                    end
                                    end

                                    -- ------------------------------------------------------------
                                    -- Ensure DB exists + migrated
                                    -- ------------------------------------------------------------
                                    function DB:Ensure()
                                    _G.PlateOMaticDB = _G.PlateOMaticDB or PlateOMaticDB or {}
                                    PlateOMaticDB = _G.PlateOMaticDB

                                    PlateOMaticDB.pvp = PlateOMaticDB.pvp or {}

                                    -- Merge in new defaults without deleting old keys
                                    MergeMissing(PlateOMaticDB.pvp, self.DEFAULTS)

                                    -- Ensure nested color tables exist (old installs / partial saves)
                                    PlateOMaticDB.pvp.targetBorderColor = PlateOMaticDB.pvp.targetBorderColor or ShallowCopyTable(self.DEFAULTS.targetBorderColor)
                                    PlateOMaticDB.pvp.focusBorderColor  = PlateOMaticDB.pvp.focusBorderColor  or ShallowCopyTable(self.DEFAULTS.focusBorderColor)

                                    -- Hard-sanitize numeric keys (prevents nil/strings from older saves)
                                    local p = PlateOMaticDB.pvp
                                    p.arenaNumbersSize = tonumber(p.arenaNumbersSize or self.DEFAULTS.arenaNumbersSize) or self.DEFAULTS.arenaNumbersSize
                                    p.arenaNumbersX    = tonumber(p.arenaNumbersX    or self.DEFAULTS.arenaNumbersX)    or self.DEFAULTS.arenaNumbersX
                                    p.arenaNumbersY    = tonumber(p.arenaNumbersY    or self.DEFAULTS.arenaNumbersY)    or self.DEFAULTS.arenaNumbersY

                                    p.healerMarkSize = tonumber(p.healerMarkSize or self.DEFAULTS.healerMarkSize) or self.DEFAULTS.healerMarkSize
                                    p.healerMarkX    = tonumber(p.healerMarkX    or self.DEFAULTS.healerMarkX)    or self.DEFAULTS.healerMarkX
                                    p.healerMarkY    = tonumber(p.healerMarkY    or self.DEFAULTS.healerMarkY)    or self.DEFAULTS.healerMarkY
                                    end

                                    function DB:Get()
                                    self:Ensure()
                                    return PlateOMaticDB.pvp
                                    end

                                    function DB:SetBool(key, v)
                                    self:Ensure()
                                    PlateOMaticDB.pvp[key] = (v and true or false)
                                    end

                                    function DB:SetNumber(key, n)
                                    self:Ensure()
                                    PlateOMaticDB.pvp[key] = tonumber(n) or 0
                                    end

                                    -- Legacy no-ops (in case other files still call them)
                                    -- We keep them so nothing errors, but they do nothing now that skins are dropped.
                                    function DB:SetRoleSkin(roleToken, id) end
                                    function DB:SetClassSkin(classToken, id) end


