--[[
============================================================================
ETB v10.0 - Settings.lua
============================================================================
User settings and preferences management.
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETB = NS.ETB

ETB.Settings = {
    defaults = {
        ui = {
            scale = 1.0,
            showQualityBorders = true,
            showSourceInfo = true,
            showCollectionStatus = true,
            compactMode = false,
        },
        preview = {
            autoPreview = true,
            previewDelay = 0.3,
            showOnHover = true,
        },
        tooltips = {
            enhance = true,
            showSource = true,
            showCollected = true,
            showItemIDs = false,
        },
        minimap = {
            show = true,
            position = 195,
        },
        filters = {
            rememberLastCategory = true,
            defaultCollected = "all",
        },
    },
    current = {},
}

local Settings = ETB.Settings

-- =============================================================================
-- LOAD / SAVE
-- =============================================================================

function Settings:Load()
    ETB_Settings = ETB_Settings or {}
    
    -- Deep copy defaults
    self.current = self:DeepCopy(self.defaults)
    
    -- Merge saved settings
    self:MergeSettings(self.current, ETB_Settings)
    
    ETB:Debug("Settings", "Settings loaded")
end

function Settings:Save()
    ETB_Settings = self:DeepCopy(self.current)
    ETB:Debug("Settings", "Settings saved")
end

function Settings:Reset()
    self.current = self:DeepCopy(self.defaults)
    self:Save()
    ETB:Print("Settings reset to defaults")
    ETB.EventBus:Fire("ETB_SETTINGS_RESET")
end

-- =============================================================================
-- GETTERS / SETTERS
-- =============================================================================

function Settings:Get(path)
    local parts = { strsplit(".", path) }
    local value = self.current
    
    for _, part in ipairs(parts) do
        if type(value) ~= "table" then return nil end
        value = value[part]
    end
    
    return value
end

function Settings:Set(path, value)
    local parts = { strsplit(".", path) }
    local target = self.current
    
    for i = 1, #parts - 1 do
        if type(target[parts[i]]) ~= "table" then
            target[parts[i]] = {}
        end
        target = target[parts[i]]
    end
    
    target[parts[#parts]] = value
    self:Save()
    
    ETB.EventBus:Fire("ETB_SETTING_CHANGED", path, value)
end

-- =============================================================================
-- UTILITIES
-- =============================================================================

function Settings:DeepCopy(orig)
    local copy
    if type(orig) == "table" then
        copy = {}
        for key, value in pairs(orig) do
            copy[self:DeepCopy(key)] = self:DeepCopy(value)
        end
    else
        copy = orig
    end
    return copy
end

function Settings:MergeSettings(target, source)
    if type(source) ~= "table" then return end
    
    for key, value in pairs(source) do
        if type(value) == "table" and type(target[key]) == "table" then
            self:MergeSettings(target[key], value)
        else
            target[key] = value
        end
    end
end

ETB:Debug("Settings", "Settings.lua loaded")
