--[[--------------------------------------------------------------------
  PaladinHolyPower - logic/settings.lua
  SavedVariables & Default Handling
  - Canonical DB: PaladinHolyPowerDB
----------------------------------------------------------------------]]

local PHP = _G.PaladinHolyPower
if not PHP then return end

-- Defaults
local function GetDefaultSettings()
    return {
        ------------------------------------------------------------------
        -- Holy Power Log tracker
        ------------------------------------------------------------------
        HPL = {
            enabled    = true,          -- Wasted Holy Power tracker on/off
            x          = -500,
            y          = 0,
            iconSize   = 50,
            textSize   = 20,
            zoom       = 0,
            alpha      = 1,
            valueColor = { 0.949, 0.902, 0.600 },
            font       = "Fonts\\FRIZQT__.TTF",
            locked     = true,
        },

        ------------------------------------------------------------------
        -- Holy Power Bar (multi-style)
        ------------------------------------------------------------------
        HP = {
            enabled          = true,    -- Holy Power bar on/off
            x                = 0,
            y                = -215,
            locked           = true,
            showOnlyInCombat = false,
            font             = "Fonts\\FRIZQT__.TTF",

            -- Which style is currently active
            style            = "default",

            -- Style-specific sub-tables
            styles = {
                default = {
                    iconSize    = 30,
                    iconSpacing = 0,
                    verticalWave  = 0,
                    zoom        = 0,
                    alpha       = 1,
                    fillColor   = { 1, 1, 1 },
                    emptyColor  = { 0.3, 0.3, 0.3 },
                    pulseAnim   = true,
                },
                custom = {
                    iconSize         = 30,
                    iconSpacing      = 0,
                    verticalWave  = 0,
                    zoom             = 0,
                    alpha            = 1,
                    fillColor        = { 1, 1, 1 },
                    emptyColor       = { 0.3, 0.3, 0.3 },
                    pulseAnim        = true,
                    customIcon       = 1445240,
                    customIconLabel  = "- Default Shine",
                },
                custom_bar = {
                    width        = 35,
                    height       = 15,
                    iconSpacing  = 0,
                    verticalWave  = 0,
                    alpha        = 1,
                    fillColor    = { 1, 1, 1 },
                    emptyColor   = { 0.3, 0.3, 0.3 },
                    pulseAnim    = true,
                    barTexture   = "Solid Color",
                },
            },
        },
    }
end

-- Shallow-merge defaults into the provided db
local function ApplyDefaults(db)
    local defaults = GetDefaultSettings()

    -- HPL
    db.HPL = db.HPL or {}
    for k, v in pairs(defaults.HPL) do
        if db.HPL[k] == nil then db.HPL[k] = v end
    end

    -- HP
    db.HP = db.HP or {}
    for k, v in pairs(defaults.HP) do
        if db.HP[k] == nil then db.HP[k] = v end
    end

    -- Ensure style tables exist
    db.HP.styles = db.HP.styles or {}
    for styleName, styleDefaults in pairs(defaults.HP.styles) do
        db.HP.styles[styleName] = db.HP.styles[styleName] or {}
        for k, v in pairs(styleDefaults) do
            if db.HP.styles[styleName][k] == nil then
                db.HP.styles[styleName][k] = v
            end
        end
    end

    return db
end

-- Public: copy/upgrade defaults into the active DB
-- Accepts optional db param; if nil, ensures global PaladinHolyPowerDB exists.
function PHP.CopyDefaults(db)
    if type(db) ~= "table" then
        _G.PaladinHolyPowerDB = _G.PaladinHolyPowerDB or {}
        db = _G.PaladinHolyPowerDB
    else
        _G.PaladinHolyPowerDB = db
    end
    return ApplyDefaults(db)
end

PHP.GetDefaultSettings = GetDefaultSettings