--[[
============================================================================
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,
            -- Feature 4.4: hide cosmetic slots that visually clutter the model
            autoHideTabard = false,
            autoHideShirt = false,
            autoHideWeapons = false,
            -- Feature 5.3: which background preset to use behind the model.
            -- v15.8.2: 'parchment_large' is a confirmed-existing MoP 5.4.8
            -- texture (Interface\AchievementFrame\UI-Achievement-Parchment-Horizontal).
            -- The v15.8.1 'dressing_room' path was a guess that didn't resolve
            -- in 5.4.8, leaving the model with no background at all.
            backgroundID = "parchment_large",
        },
        tooltips = {
            enhance = true,
            showSource = true,
            showCollected = true,
            -- v15.5.0: when true, show "Cannot learn — wrong armor/weapon type"
            -- on tooltips for items the player isn't class-eligible for.
            -- Off-by-default for new installs would hide a useful feature; on-by-default
            -- mirrors showCollected.
            showCantLearn = true,
            showItemIDs = false,
        },
        -- v15.6.0: B2 bag overlay markers
        bagOverlay = {
            enabled = true,          -- master toggle: paint markers in bags
            showUnlearnable = false, -- show dim gray markers on items the
                                     -- player can't equip (off because it's
                                     -- visual noise for most players)
        },
        minimap = {
            show = true,
            position = 195,
        },
        filters = {
            rememberLastCategory = true,
            defaultCollected = "all",
        },
        -- v14.7.0 M3 fix: declare notifications namespace explicitly so the
        -- CollectionToast IsEnabled() check has a real default to read from.
        -- Was previously falling back to "true if nil" — works but means a
        -- future Settings refactor that requires explicit defaults would
        -- silently disable toasts.
        notifications = {
            collectionToast = true,
            -- v15.8.16: toast when a transmog set (Blizzard or community)
            -- transitions to fully collected. Default ON for celebration.
            setCompletionToast = true,
        },
        -- v15.8.17: dev-tool warning at login. Default ON. Turn off via
        -- /etb devwarn off once the user has acknowledged.
        dev = {
            warnOnDevTools = true,
        },
    },
    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")
