--[[
============================================================================
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.
        },
        tooltips = {
            enhance = true,
            showSource = true,
            showCollected = true,
            -- v15.20.0: when true, the in-tooltip ETB block collapses to a
            -- single line (status + wishlist marker) — the side panel still
            -- carries the full detail. Off shows the v15.x inline source +
            -- separate wishlist line layout.
            compact = 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,
        },
        -- v15.36.0: low-memory mode. When ON, passive tooltip hooks do NOT
        -- pull the heavy datasets (Compact source ~62 MB, ReagentData, BiSData,
        -- BossLoot) into memory — those only materialize when you actively open
        -- the ETB window. This is opt-in so public defaults keep every feature
        -- visible; toggle with /etb lite for the smaller open-world footprint.
        performance = {
            lowMemory = false,
            startupQuietSeconds = 25,
            pauseStartupWhileMoving = true,
            -- SourceDB supplementation improves boss-loot coverage but loads and
            -- scans the source-enrichment layer. Leave off for the public default.
            bossLootSourceDBSupplement = false,
            frameWorkBudgetMs = 1.0,
            itemInfoPerFrame = 2,
            modelGridPerFrame = 1,
        },
    },
    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")
