local _, ns = ...

-- Default configuration
ns.defaults = {
    -- General
    debug = false,
    debugChat = false,
    
    -- Player Castbar
    player = {
        enabled = true,
        width = 220,
        height = 19,
        scale = 0.93,
        showLatency = true,
        showSafezone = true,
        classColorCast = false,  -- NEW: Use class color for cast bar
        showTime = false,  -- NEW: Show cast time
        textPosition = "inside",  -- NEW: "inside", "above", "below"
        textAlignCenter = false,   -- NEW: center text when inside
        dynamicWidth = true,  -- NEW: Auto-adjust width based on spell name
        statusbarTexture = "smooth_v2",
        spellNameFontSize = 12,
        spellNameOutline = "OUTLINE",
        spellNameFont = "Friz Quadrata TT",
        icon = {
            show = true,
            position = "LEFT", -- "LEFT" or "RIGHT"
            positionOutside = false,
        },
        castSuccess = true,
        colors = {
            cast = {0.678, 0.082, 0.071},  -- #AD1512 (Original NeavUI color)
            channel = {0.0, 1.0, 0.0},
            nonInterruptible = {0.75, 0.0, 0.0},
            failed = {1.0, 0.0, 0.0},
            success = {0.0, 0.8, 0.0},
            safezone = {1, 0, 1},
        },
        position = {"BOTTOM", "UIParent", "BOTTOM", 0, 170},
    },
    
    -- Target Castbar
    target = {
        enabled = true,
        width = 220,
        height = 19,
        scale = 0.93,
        classColorCast = false,  -- NEW: Use class color for cast bar
        showTime = false,  -- NEW: Show cast time
        textPosition = "inside",  -- NEW: "inside", "above", "below"
        textAlignCenter = false,   -- NEW: center text when inside
        dynamicWidth = true,  -- NEW: Auto-adjust width based on spell name
        statusbarTexture = "smooth_v2",
        spellNameFontSize = 12,
        spellNameOutline = "OUTLINE",
        spellNameFont = "Friz Quadrata TT",
        icon = {
            show = true,
            position = "LEFT",
            positionOutside = false,
        },
        castSuccess = true,
        colors = {
            cast = {0.678, 0.082, 0.071},  -- #AD1512 (Original NeavUI color)
            channel = {0.0, 1.0, 0.0},
            nonInterruptible = {0.75, 0.0, 0.0},
            failed = {1.0, 0.0, 0.0},
            success = {0.0, 0.8, 0.0},
        },
        position = {"BOTTOM", "UIParent", "BOTTOM", 0, 205},
    },
    
    -- Focus Castbar
    focus = {
        enabled = true,
        width = 176,
        height = 19,
        scale = 0.93,
        classColorCast = false,  -- NEW: Use class color for cast bar
        showTime = false,  -- NEW: Show cast time
        textPosition = "inside",  -- NEW: "inside", "above", "below"
        textAlignCenter = false,   -- NEW: center text when inside
        dynamicWidth = true,  -- NEW: Auto-adjust width based on spell name
        statusbarTexture = "smooth_v2",
        spellNameFontSize = 12,
        spellNameOutline = "OUTLINE",
        spellNameFont = "Friz Quadrata TT",
        icon = {
            show = true,
            position = "LEFT",
            positionOutside = false,
        },
        castSuccess = true,
        colors = {
            cast = {0.678, 0.082, 0.071},  -- #AD1512 (Original NeavUI color)
            channel = {0.0, 1.0, 0.0},
            nonInterruptible = {0.75, 0.0, 0.0},
            failed = {1.0, 0.0, 0.0},
            success = {0.0, 0.8, 0.0},
        },
    },
    
    -- Pet Castbar
    pet = {
        enabled = false,
        width = 220,
        height = 19,
        scale = 0.93,
        classColorCast = false,  -- NEW: Use class color for cast bar
        showTime = false,  -- NEW: Show cast time
        textPosition = "inside",  -- NEW: "inside", "above", "below"
        textAlignCenter = false,   -- NEW: center text when inside
        dynamicWidth = true,  -- NEW: Auto-adjust width based on spell name
        statusbarTexture = "smooth_v2",
        spellNameFontSize = 12,
        spellNameOutline = "OUTLINE",
        spellNameFont = "Friz Quadrata TT",
        ignoreSpells = true,
        ignoreList = {
            3110,   -- Firebolt (Imp)
            31707,  -- Waterbolt (Water Elemental)
        },
        icon = {
            show = false,
            position = "LEFT",
            positionOutside = true,
        },
        castSuccess = true,
        colors = {
            cast = {0.678, 0.082, 0.071},  -- #AD1512 (Original NeavUI color)
            channel = {0.0, 1.0, 0.0},
            nonInterruptible = {0.75, 0.0, 0.0},
            failed = {1.0, 0.0, 0.0},
            success = {0.0, 0.8, 0.0},
        },
    },
    
    -- Mirror Timers
    mirrorTimers = {
        enabled = true,
        scale = 1.132,
        width = 220,
        height = 18,
    },

    -- Wie rOrbs: Position beim Ziehen auf 10-Pixel-Raster (Edit-Modus / Anker)
    snapToGrid = false,
}

-- Initialize config with defaults
function ns.InitConfig()
    -- Deep copy defaults if DB is empty
    if not nCastbarDB.profile then
        nCastbarDB.profile = {}
        ns.CopyDefaults(ns.defaults, nCastbarDB.profile)
    else
        -- Merge new defaults with existing config
        ns.MergeDefaults(ns.defaults, nCastbarDB.profile)
    end
    
end

-- Deep copy function
function ns.CopyDefaults(src, dst)
    for k, v in pairs(src) do
        if type(v) == "table" then
            dst[k] = {}
            ns.CopyDefaults(v, dst[k])
        else
            dst[k] = v
        end
    end
end

-- Merge defaults (preserves existing values, adds missing ones)
function ns.MergeDefaults(src, dst)
    for k, v in pairs(src) do
        if dst[k] == nil then
            if type(v) == "table" then
                dst[k] = {}
                ns.CopyDefaults(v, dst[k])
            else
                dst[k] = v
            end
        elseif type(v) == "table" and type(dst[k]) == "table" then
            ns.MergeDefaults(v, dst[k])
        end
    end
end

-- Reset config to defaults
function ns.ResetConfig()
    nCastbarDB.profile = {}
    ns.CopyDefaults(ns.defaults, nCastbarDB.profile)
    ns.Print(ns.L["SETTINGS_RESET"])
    ReloadUI()
end

-- Get config value
function ns.GetConfig(...)
    local t = nCastbarDB.profile
    for i = 1, select("#", ...) do
        local key = select(i, ...)
        if t[key] == nil then
            return nil
        end
        t = t[key]
    end
    return t
end

-- Set config value
function ns.SetConfig(value, ...)
    local t = nCastbarDB.profile
    local lastKey = select(select("#", ...), ...)
    
    for i = 1, select("#", ...) - 1 do
        local key = select(i, ...)
        if t[key] == nil then
            t[key] = {}
        end
        t = t[key]
    end
    
    t[lastKey] = value
end

-- Create LibDataBroker object for minimap icon
local LDB = LibStub("LibDataBroker-1.1")
local icon = LDB:NewDataObject("nCastbar", {
    type = "launcher",
    text = "nCastbar",
    icon = "Interface\\AddOns\\nCastbar\\Media\\logo_small",
    OnClick = function(self, button)
        if button == "LeftButton" then
            if ns.ModernSettings and ns.ModernSettings.configFrame then
                local frame = ns.ModernSettings.configFrame
                if frame:IsShown() then
                    frame:FadeOut()
                else
                    frame:FadeIn()
                end
            end
        end
    end,
    OnTooltipShow = function(tooltip)
        if not tooltip or not tooltip.AddLine then return end
        tooltip:AddLine("|cffffd700nCastbar|r")
        tooltip:AddLine((ns.L["MINIMAP_LEFT_CLICK"] or "|cffffffffLeft-click:|r Open settings"), 0.8, 0.8, 0.8)
        tooltip:AddLine((ns.L["MINIMAP_RIGHT_CLICK"] or "|cffffffffRight-click:|r (reserved)"), 0.8, 0.8, 0.8)
    end,
})

-- Register with LibDBIcon
local LibDBIcon = LibStub("LibDBIcon-1.0")
LibDBIcon:Register("nCastbar", icon, nCastbarDB.minimap or {hide = false})

ns.Debug("Config.lua loaded")
