-- settings.lua (Namespaced + Cleaned)

local ADDON_NAME, WDIGI = ...
WDIGI.Settings = WDIGI.Settings or {}

------------------------------------------------------------
-- DEFAULTS
------------------------------------------------------------
local DEFAULTS = {
    WDIGI_ShowItemID_Toggle = false,
    WDIGI_ALTKey_Toggle = false,
    WDIGI_EnableNavHUD_Experimental = false,
    WDIGI_DebugNavHUD = false, -- NEW: Debug toggle for REL output
    WDIGI_Limit_DroppedBy = 5,
    WDIGI_Limit_SoldBy = 5,
    WDIGI_Limit_ContainedIn = 5,
    WDIGI_Limit_Disenchanted = 5,
    WDIGI_Limit_GatheredFrom = 5,
    WDIGI_Limit_Other = 5,
}

------------------------------------------------------------
-- INITIALIZATION
------------------------------------------------------------
local function OnEvent(self, event, addon)
    if addon ~= ADDON_NAME then return end

    WDIGI_APP_SETTINGS = WDIGI_APP_SETTINGS or {}

    -- Apply defaults
    for key, val in pairs(DEFAULTS) do
        if WDIGI_APP_SETTINGS[key] == nil then
            WDIGI_APP_SETTINGS[key] = val
        end
    end
end

local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("OnEvent", OnEvent)

------------------------------------------------------------
-- ACCESSORS
------------------------------------------------------------
function WDIGI.Settings.Get(key)
    return WDIGI_APP_SETTINGS[key]
end

function WDIGI.Settings.Set(key, value)
    WDIGI_APP_SETTINGS[key] = value
end

-- NEW: Debug accessor
function WDIGI.Settings.DebugNav()
    return WDIGI.Settings.Get("WDIGI_DebugNavHUD")
end

------------------------------------------------------------
-- SETTINGS PANEL
------------------------------------------------------------
local category = Settings.RegisterVerticalLayoutCategory("Where Do I Get It?")

local function RegisterCheckbox(name, variable, tooltip)
    local setting = Settings.RegisterProxySetting(
        category,
        variable,
        type(false),
        name,
        DEFAULTS[variable],
        function() return WDIGI.Settings.Get(variable) end,
        function(value) WDIGI.Settings.Set(variable, value) end
    )

    Settings.CreateCheckbox(category, setting, tooltip)
end

local function RegisterSlider(name, variable, tooltip, min, max)
    local setting = Settings.RegisterProxySetting(
        category,
        variable,
        type(0),
        name,
        DEFAULTS[variable],
        function() return WDIGI.Settings.Get(variable) end,
        function(value) WDIGI.Settings.Set(variable, value) end
    )

    local options = Settings.CreateSliderOptions(min, max, 1)
    options:SetLabelFormatter(MinimalSliderWithSteppersMixin.Label.Right)

    Settings.CreateSlider(category, setting, options, tooltip)
end

------------------------------------------------------------
-- REGISTER ALL SETTINGS
------------------------------------------------------------
RegisterCheckbox("Show Item ID", "WDIGI_ShowItemID_Toggle", "Show Item ID in Tooltip")
RegisterCheckbox("Enable ALT Hotkey", "WDIGI_ALTKey_Toggle", "Only show locations when ALT is pressed")

RegisterSlider("Dropped By Limit", "WDIGI_Limit_DroppedBy", "Max 'dropped by' locations", 1, 30)
RegisterSlider("Sold By Limit", "WDIGI_Limit_SoldBy", "Max 'sold by' locations", 1, 30)
RegisterSlider("Contained In Limit", "WDIGI_Limit_ContainedIn", "Max 'contained in' locations", 1, 30)
RegisterSlider("Disenchanted Limit", "WDIGI_Limit_Disenchanted", "Max 'disenchanted from' locations", 1, 30)
RegisterSlider("Gathered From Limit", "WDIGI_Limit_GatheredFrom", "Max 'gathered from' locations", 1, 30)

RegisterCheckbox("Enable Navigation HUD (Experimental)", "WDIGI_EnableNavHUD_Experimental", "Enable the directional arrow HUD. Warning: Currently contains coordinate scaling bugs.")

-- NEW: Debug toggle
RegisterCheckbox("Debug Navigation HUD", "WDIGI_DebugNavHUD", "Show REL angle and diagnostic info for arrow calibration.")

Settings.RegisterAddOnCategory(category)
