if not select(2, ...).IS_LEGACY_CLIENT then return end -- HET-LEGACY-1207: 12.0.7 only
local addonName, ns = ...
local HET = ns.addon

local AC = LibStub("AceConfig-3.0")
local ACD = LibStub("AceConfigDialog-3.0")

local function isSpatialDisabled()
    return not HET.db.profile.enabled
end

local function isInnDisabled()
    return not HET.db.profile.inn_enabled
end

local LSM = LibStub("LibSharedMedia-3.0", true)

-- Build TTS voice dropdown values from the OS voices
local function GetTTSVoiceValues()
    local values = { [0] = "Default" }
    local voices = C_VoiceChat and C_VoiceChat.GetTtsVoices and C_VoiceChat.GetTtsVoices()
    if voices then
        for _, v in ipairs(voices) do
            values[v.voiceID] = v.name
        end
    end
    return values
end

-- Build sound dropdown values from LibSharedMedia
local function GetSoundValues()
    local values = { ["None"] = "None" }
    if LSM then
        for _, name in ipairs(LSM:List("sound")) do
            values[name] = name
        end
    end
    return values
end

local function isSpatialIconStyle()
    return not isSpatialDisabled() and HET.db.profile.trackerStyle == "icon"
end

local function isSpatialBarOrCircle()
    if isSpatialDisabled() then return false end
    local s = HET.db.profile.trackerStyle
    return s == "bar" or s == "circle"
end

local function isInnIconStyle()
    return not isInnDisabled() and HET.db.profile.inn_trackerStyle == "icon"
end

local function isInnBarOrCircle()
    if isInnDisabled() then return false end
    local s = HET.db.profile.inn_trackerStyle
    return s == "bar" or s == "circle"
end

local options = {
    type = "group",
    name = "|cff11ace9Buzzard Auras|r",
    childGroups = "tree",
    args = {
        spatialParadox = {
            type = "group",
            name = "Spatial Paradox",
            icon = C_Spell.GetSpellTexture(406732),
            order = 1,
            args = {
                enableToggle = {
                    type = "toggle",
                    name = "Enable",
                    desc = "Enable or disable the Spatial Paradox tracker.",
                    order = 1,
                    width = "full",
                    get = function() return HET.db.profile.enabled end,
                    set = function(_, val)
                        HET.db.profile.enabled = val
                        if not val then
                            HET:HidePreview()
                            local t = ns.trackers["spatial"]
                            if t then t:Stop() end
                        end
                        HET:StartPolling()
                    end,
                },
                dpsEvokerEnabled = {
                    type = "toggle",
                    name = "Enable for DPS Evoker",
                    desc = "Track Spatial Paradox on Devastation and Augmentation Evoker specs. Disable if you only want the tracker active on dedicated healer specs.",
                    order = 1.5,
                    width = "full",
                    hidden = isSpatialDisabled,
                    get = function() return HET.db.profile.dpsEvokerEnabled end,
                    set = function(_, val)
                        HET.db.profile.dpsEvokerEnabled = val
                        -- Re-run spec init so trackedSpellID reflects the new
                        -- setting on the current spec, then restart polling so
                        -- the isApplicable gate is re-evaluated.
                        HET:RefreshSpec()
                    end,
                },
                trackerStyle = {
                    type = "select",
                    name = "Tracker Style",
                    desc = "How the tracker is displayed.",
                    order = 2,
                    values = {
                        icon   = "Icon",
                        bar    = "Bar",
                        circle = "Circle",
                    },
                    sorting = { "icon", "bar", "circle" },
                    hidden = isSpatialDisabled,
                    get = function() return HET.db.profile.trackerStyle end,
                    set = function(_, val)
                        HET.db.profile.trackerStyle = val
                        HET:RefreshPreview()
                    end,
                },
                timerDuration = {
                    type = "range",
                    name = "Timer Duration",
                    desc = "By default, Spatial Paradox has a 10 second duration. For Aug Evokers, this is increased by their mastery. If you know which evoker will be providing Spatial, you can ask them what duration their spell tooltip shows and update this slider for better accuracy.",
                    order = 3,
                    min = 10,
                    max = 15,
                    step = 0.1,
                    hidden = isSpatialDisabled,
                    get = function() return HET.db.profile.timerDuration end,
                    set = function(_, val)
                        HET.db.profile.timerDuration = val
                    end,
                },

                -- Sound Alert options
                alertHeader = {
                    type = "header",
                    name = "Sound Alert",
                    order = 30,
                    hidden = isSpatialDisabled,
                },
                alertMode = {
                    type = "select",
                    name = "Sound Alert",
                    desc = "How to alert when the tracker triggers.",
                    order = 31,
                    values = {
                        none  = "None",
                        sound = "Sound",
                        tts   = "TTS",
                    },
                    sorting = { "none", "sound", "tts" },
                    hidden = isSpatialDisabled,
                    get = function() return HET.db.profile.alertMode end,
                    set = function(_, val)
                        HET.db.profile.alertMode = val
                    end,
                },
                soundName = {
                    type = "select",
                    name = "Sound",
                    desc = "Sound to play when the tracker triggers.",
                    order = 32,
                    width = 1.5,
                    values = GetSoundValues,
                    hidden = function() return isSpatialDisabled() or HET.db.profile.alertMode ~= "sound" end,
                    get = function() return HET.db.profile.soundName end,
                    set = function(_, val)
                        HET.db.profile.soundName = val
                    end,
                },
                soundChannel = {
                    type = "select",
                    name = "Channel",
                    desc = "Audio channel to play the sound on.",
                    order = 33,
                    values = {
                        Master = "Master",
                        SFX    = "Sound Effects",
                    },
                    sorting = { "Master", "SFX" },
                    hidden = function() return isSpatialDisabled() or HET.db.profile.alertMode ~= "sound" end,
                    get = function() return HET.db.profile.soundChannel end,
                    set = function(_, val)
                        HET.db.profile.soundChannel = val
                    end,
                },
                soundTest = {
                    type = "execute",
                    name = "Test",
                    desc = "Preview the selected sound.",
                    order = 34,
                    width = "half",
                    hidden = function() return isSpatialDisabled() or HET.db.profile.alertMode ~= "sound" or HET.db.profile.soundName == "None" end,
                    func = function()
                        local name = HET.db.profile.soundName
                        if name and name ~= "None" and LSM then
                            local path = LSM:Fetch("sound", name)
                            if path then
                                PlaySoundFile(path, HET.db.profile.soundChannel or "Master")
                            end
                        end
                    end,
                },
                ttsVoice = {
                    type = "select",
                    name = "TTS Voice",
                    desc = "Voice to use for text-to-speech.",
                    order = 35,
                    values = GetTTSVoiceValues,
                    hidden = function() return isSpatialDisabled() or HET.db.profile.alertMode ~= "tts" end,
                    get = function() return HET.db.profile.ttsVoice end,
                    set = function(_, val)
                        HET.db.profile.ttsVoice = val
                    end,
                },
                ttsVolume = {
                    type = "range",
                    name = "TTS Volume",
                    desc = "Volume for text-to-speech.",
                    order = 36,
                    min = 0,
                    max = 100,
                    step = 1,
                    hidden = function() return isSpatialDisabled() or HET.db.profile.alertMode ~= "tts" end,
                    get = function() return HET.db.profile.ttsVolume end,
                    set = function(_, val)
                        HET.db.profile.ttsVolume = val
                    end,
                },
                ttsTest = {
                    type = "execute",
                    name = "Test",
                    desc = "Preview TTS.",
                    order = 37,
                    width = "half",
                    hidden = function() return isSpatialDisabled() or HET.db.profile.alertMode ~= "tts" end,
                    func = function()
                        local voiceID = HET.db.profile.ttsVoice or 0
                        local volume = HET.db.profile.ttsVolume or 50
                        local rate = C_TTSSettings and C_TTSSettings.GetSpeechRate() or 0
                        C_VoiceChat.SpeakText(voiceID, "Spatial", rate, volume, true)
                    end,
                },

                -- Icon-specific options
                iconHeader = {
                    type = "header",
                    name = "Icon Settings",
                    order = 10,
                    hidden = function() return not isSpatialIconStyle() end,
                },
                iconSize = {
                    type = "range",
                    name = "Icon Size",
                    desc = "Size of the tracker icon in pixels.",
                    order = 11,
                    min = 20,
                    max = 100,
                    step = 1,
                    hidden = function() return not isSpatialIconStyle() end,
                    get = function() return HET.db.profile.iconSize end,
                    set = function(_, val)
                        HET.db.profile.iconSize = val
                        HET:RefreshPreview()
                    end,
                },
                cooldownSwipe = {
                    type = "toggle",
                    name = "Cooldown Swipe",
                    desc = "Show a cooldown swipe overlay on the icon.",
                    order = 12,
                    hidden = function() return not isSpatialIconStyle() end,
                    get = function() return HET.db.profile.cooldownSwipe end,
                    set = function(_, val)
                        HET.db.profile.cooldownSwipe = val
                    end,
                },

                -- Bar/Circle shared options
                barCircleHeader = {
                    type = "header",
                    name = "Style Settings",
                    order = 20,
                    hidden = function() return not isSpatialBarOrCircle() end,
                },
                barCircleColor = {
                    type = "color",
                    name = "Color",
                    desc = "Color of the bar or circle.",
                    order = 21,
                    hasAlpha = true,
                    hidden = function() return not isSpatialBarOrCircle() end,
                    get = function()
                        local c = HET.db.profile.barCircleColor
                        return c[1], c[2], c[3], c[4]
                    end,
                    set = function(_, r, g, b, a)
                        HET.db.profile.barCircleColor = { r, g, b, a }
                        HET:RefreshPreview()
                    end,
                },

                -- Bar-specific options
                barOrientation = {
                    type = "select",
                    name = "Bar Orientation",
                    desc = "Direction of the bar fill.",
                    order = 22,
                    values = {
                        horizontal = "Horizontal",
                        vertical   = "Vertical",
                    },
                    sorting = { "horizontal", "vertical" },
                    hidden = function() return isSpatialDisabled() or HET.db.profile.trackerStyle ~= "bar" end,
                    get = function() return HET.db.profile.barOrientation end,
                    set = function(_, val)
                        HET.db.profile.barOrientation = val
                        if val == "vertical" then
                            HET.db.profile.barWidth = 20
                            HET.db.profile.barHeight = 200
                        else
                            HET.db.profile.barWidth = 200
                            HET.db.profile.barHeight = 20
                        end
                        HET:RefreshPreview()
                    end,
                },
                barWidth = {
                    type = "range",
                    name = "Bar Width",
                    desc = "Width of the tracker bar in pixels.",
                    order = 23,
                    min = 4,
                    max = 600,
                    step = 1,
                    hidden = function() return isSpatialDisabled() or HET.db.profile.trackerStyle ~= "bar" end,
                    get = function() return HET.db.profile.barWidth end,
                    set = function(_, val)
                        HET.db.profile.barWidth = val
                        HET:RefreshPreview()
                    end,
                },
                barHeight = {
                    type = "range",
                    name = "Bar Height",
                    desc = "Height of the tracker bar in pixels.",
                    order = 24,
                    min = 4,
                    max = 600,
                    step = 1,
                    hidden = function() return isSpatialDisabled() or HET.db.profile.trackerStyle ~= "bar" end,
                    get = function() return HET.db.profile.barHeight end,
                    set = function(_, val)
                        HET.db.profile.barHeight = val
                        HET:RefreshPreview()
                    end,
                },

                -- Circle-specific options
                circleRadius = {
                    type = "range",
                    name = "Circle Size",
                    desc = "Size of the tracker circle in pixels.",
                    order = 25,
                    min = 50,
                    max = 150,
                    step = 1,
                    hidden = function() return isSpatialDisabled() or HET.db.profile.trackerStyle ~= "circle" end,
                    get = function() return HET.db.profile.circleRadius end,
                    set = function(_, val)
                        HET.db.profile.circleRadius = val
                        HET:RefreshPreview()
                    end,
                },
                ringTexture = {
                    type = "select",
                    name = "Circle Thickness",
                    desc = "Thickness of the circle ring.",
                    order = 26,
                    values = {
                        ["Ring_10px"] = "Thin (10px)",
                        ["Ring_20px"] = "Medium (20px)",
                        ["Ring_30px"] = "Thick (30px)",
                        ["Ring_40px"] = "Extra Thick (40px)",
                    },
                    sorting = { "Ring_10px", "Ring_20px", "Ring_30px", "Ring_40px" },
                    hidden = function() return isSpatialDisabled() or HET.db.profile.trackerStyle ~= "circle" end,
                    get = function() return HET.db.profile.ringTexture end,
                    set = function(_, val)
                        HET.db.profile.ringTexture = val
                        HET:RefreshPreview()
                    end,
                },

                -- Preview
                previewHeader = {
                    type = "header",
                    name = "Preview",
                    order = 50,
                    hidden = isSpatialDisabled,
                },
                previewDesc = {
                    type = "description",
                    name = "Toggle the preview to show the tracker. Drag to reposition it.",
                    order = 51,
                    hidden = isSpatialDisabled,
                },
                previewToggle = {
                    type = "execute",
                    name = function()
                        return HET:IsPreviewActive() and "Hide Preview" or "Show Preview"
                    end,
                    desc = "Show or hide the tracker so you can drag it into position.",
                    order = 52,
                    hidden = isSpatialDisabled,
                    func = function()
                        if HET:IsPreviewActive() then
                            HET:HidePreview()
                        else
                            HET:ShowPreview()
                        end
                    end,
                },
                resetPosition = {
                    type = "execute",
                    name = "Reset Position",
                    desc = "Reset the tracker position to the center of the screen.",
                    order = 53,
                    hidden = isSpatialDisabled,
                    func = function()
                        HET.db.profile.position = nil
                        local t = ns.trackers["spatial"]
                        if t then
                            local pos = t.config.defaultPos
                            t.iconFrame:ClearAllPoints()
                            t.iconFrame:SetPoint(pos[1], UIParent, pos[2], pos[3], pos[4])
                            t.barFrame:ClearAllPoints()
                            t.barFrame:SetPoint(pos[1], UIParent, pos[2], pos[3], pos[4])
                            t.circleFrame:ClearAllPoints()
                            t.circleFrame:SetPoint(pos[1], UIParent, pos[2], pos[3], pos[4])
                            t:RefreshPreview()
                        end
                    end,
                },
            },
        },
        innervate = {
            type = "group",
            name = "Innervate",
            icon = C_Spell.GetSpellTexture(29166),
            order = 2,
            args = {
                inn_enableToggle = {
                    type = "toggle",
                    name = "Enable",
                    desc = "Enable or disable the Innervate tracker.",
                    order = 1,
                    width = "full",
                    get = function() return HET.db.profile.inn_enabled end,
                    set = function(_, val)
                        HET.db.profile.inn_enabled = val
                        if not val then
                            HET:HideInnPreview()
                            local t = ns.trackers["innervate"]
                            if t then t:Stop() end
                        end
                        HET:StartPolling()
                    end,
                },
                inn_trackerStyle = {
                    type = "select",
                    name = "Tracker Style",
                    desc = "How the tracker is displayed.",
                    order = 2,
                    values = {
                        icon   = "Icon",
                        bar    = "Bar",
                        circle = "Circle",
                    },
                    sorting = { "icon", "bar", "circle" },
                    hidden = isInnDisabled,
                    get = function() return HET.db.profile.inn_trackerStyle end,
                    set = function(_, val)
                        HET.db.profile.inn_trackerStyle = val
                        HET:RefreshInnPreview()
                    end,
                },

                -- Sound Alert options
                inn_alertHeader = {
                    type = "header",
                    name = "Sound Alert",
                    order = 30,
                    hidden = isInnDisabled,
                },
                inn_alertMode = {
                    type = "select",
                    name = "Sound Alert",
                    desc = "How to alert when the tracker triggers.",
                    order = 31,
                    values = {
                        none  = "None",
                        sound = "Sound",
                        tts   = "TTS",
                    },
                    sorting = { "none", "sound", "tts" },
                    hidden = isInnDisabled,
                    get = function() return HET.db.profile.inn_alertMode end,
                    set = function(_, val)
                        HET.db.profile.inn_alertMode = val
                    end,
                },
                inn_soundName = {
                    type = "select",
                    name = "Sound",
                    desc = "Sound to play when the tracker triggers.",
                    order = 32,
                    width = 1.5,
                    values = GetSoundValues,
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_alertMode ~= "sound" end,
                    get = function() return HET.db.profile.inn_soundName end,
                    set = function(_, val)
                        HET.db.profile.inn_soundName = val
                    end,
                },
                inn_soundChannel = {
                    type = "select",
                    name = "Channel",
                    desc = "Audio channel to play the sound on.",
                    order = 33,
                    values = {
                        Master = "Master",
                        SFX    = "Sound Effects",
                    },
                    sorting = { "Master", "SFX" },
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_alertMode ~= "sound" end,
                    get = function() return HET.db.profile.inn_soundChannel end,
                    set = function(_, val)
                        HET.db.profile.inn_soundChannel = val
                    end,
                },
                inn_soundTest = {
                    type = "execute",
                    name = "Test",
                    desc = "Preview the selected sound.",
                    order = 34,
                    width = "half",
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_alertMode ~= "sound" or HET.db.profile.inn_soundName == "None" end,
                    func = function()
                        local name = HET.db.profile.inn_soundName
                        if name and name ~= "None" and LSM then
                            local path = LSM:Fetch("sound", name)
                            if path then
                                PlaySoundFile(path, HET.db.profile.inn_soundChannel or "Master")
                            end
                        end
                    end,
                },
                inn_ttsVoice = {
                    type = "select",
                    name = "TTS Voice",
                    desc = "Voice to use for text-to-speech.",
                    order = 35,
                    values = GetTTSVoiceValues,
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_alertMode ~= "tts" end,
                    get = function() return HET.db.profile.inn_ttsVoice end,
                    set = function(_, val)
                        HET.db.profile.inn_ttsVoice = val
                    end,
                },
                inn_ttsVolume = {
                    type = "range",
                    name = "TTS Volume",
                    desc = "Volume for text-to-speech.",
                    order = 36,
                    min = 0,
                    max = 100,
                    step = 1,
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_alertMode ~= "tts" end,
                    get = function() return HET.db.profile.inn_ttsVolume end,
                    set = function(_, val)
                        HET.db.profile.inn_ttsVolume = val
                    end,
                },
                inn_ttsTest = {
                    type = "execute",
                    name = "Test",
                    desc = "Preview TTS.",
                    order = 37,
                    width = "half",
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_alertMode ~= "tts" end,
                    func = function()
                        local voiceID = HET.db.profile.inn_ttsVoice or 0
                        local volume = HET.db.profile.inn_ttsVolume or 50
                        local rate = C_TTSSettings and C_TTSSettings.GetSpeechRate() or 0
                        C_VoiceChat.SpeakText(voiceID, "Innervate", rate, volume, true)
                    end,
                },

                -- Icon-specific
                inn_iconHeader = {
                    type = "header",
                    name = "Icon Settings",
                    order = 10,
                    hidden = function() return not isInnIconStyle() end,
                },
                inn_iconSize = {
                    type = "range",
                    name = "Icon Size",
                    desc = "Size of the tracker icon in pixels.",
                    order = 11,
                    min = 20,
                    max = 100,
                    step = 1,
                    hidden = function() return not isInnIconStyle() end,
                    get = function() return HET.db.profile.inn_iconSize end,
                    set = function(_, val)
                        HET.db.profile.inn_iconSize = val
                        HET:RefreshInnPreview()
                    end,
                },
                inn_cooldownSwipe = {
                    type = "toggle",
                    name = "Cooldown Swipe",
                    desc = "Show a cooldown swipe overlay on the icon.",
                    order = 12,
                    hidden = function() return not isInnIconStyle() end,
                    get = function() return HET.db.profile.inn_cooldownSwipe end,
                    set = function(_, val)
                        HET.db.profile.inn_cooldownSwipe = val
                    end,
                },

                -- Bar/Circle shared
                inn_barCircleHeader = {
                    type = "header",
                    name = "Style Settings",
                    order = 20,
                    hidden = function() return not isInnBarOrCircle() end,
                },
                inn_barCircleColor = {
                    type = "color",
                    name = "Color",
                    desc = "Color of the bar or circle.",
                    order = 21,
                    hasAlpha = true,
                    hidden = function() return not isInnBarOrCircle() end,
                    get = function()
                        local c = HET.db.profile.inn_barCircleColor
                        return c[1], c[2], c[3], c[4]
                    end,
                    set = function(_, r, g, b, a)
                        HET.db.profile.inn_barCircleColor = { r, g, b, a }
                        HET:RefreshInnPreview()
                    end,
                },

                -- Bar-specific
                inn_barOrientation = {
                    type = "select",
                    name = "Bar Orientation",
                    desc = "Direction of the bar fill.",
                    order = 22,
                    values = {
                        horizontal = "Horizontal",
                        vertical   = "Vertical",
                    },
                    sorting = { "horizontal", "vertical" },
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_trackerStyle ~= "bar" end,
                    get = function() return HET.db.profile.inn_barOrientation end,
                    set = function(_, val)
                        HET.db.profile.inn_barOrientation = val
                        if val == "vertical" then
                            HET.db.profile.inn_barWidth = 20
                            HET.db.profile.inn_barHeight = 200
                        else
                            HET.db.profile.inn_barWidth = 200
                            HET.db.profile.inn_barHeight = 20
                        end
                        HET:RefreshInnPreview()
                    end,
                },
                inn_barWidth = {
                    type = "range",
                    name = "Bar Width",
                    order = 23,
                    min = 4, max = 600, step = 1,
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_trackerStyle ~= "bar" end,
                    get = function() return HET.db.profile.inn_barWidth end,
                    set = function(_, val)
                        HET.db.profile.inn_barWidth = val
                        HET:RefreshInnPreview()
                    end,
                },
                inn_barHeight = {
                    type = "range",
                    name = "Bar Height",
                    order = 24,
                    min = 4, max = 600, step = 1,
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_trackerStyle ~= "bar" end,
                    get = function() return HET.db.profile.inn_barHeight end,
                    set = function(_, val)
                        HET.db.profile.inn_barHeight = val
                        HET:RefreshInnPreview()
                    end,
                },

                -- Circle-specific
                inn_circleRadius = {
                    type = "range",
                    name = "Circle Size",
                    order = 25,
                    min = 50, max = 150, step = 1,
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_trackerStyle ~= "circle" end,
                    get = function() return HET.db.profile.inn_circleRadius end,
                    set = function(_, val)
                        HET.db.profile.inn_circleRadius = val
                        HET:RefreshInnPreview()
                    end,
                },
                inn_ringTexture = {
                    type = "select",
                    name = "Circle Thickness",
                    desc = "Thickness of the circle ring.",
                    order = 26,
                    values = {
                        ["Ring_10px"] = "Thin (10px)",
                        ["Ring_20px"] = "Medium (20px)",
                        ["Ring_30px"] = "Thick (30px)",
                        ["Ring_40px"] = "Extra Thick (40px)",
                    },
                    sorting = { "Ring_10px", "Ring_20px", "Ring_30px", "Ring_40px" },
                    hidden = function() return isInnDisabled() or HET.db.profile.inn_trackerStyle ~= "circle" end,
                    get = function() return HET.db.profile.inn_ringTexture end,
                    set = function(_, val)
                        HET.db.profile.inn_ringTexture = val
                        HET:RefreshInnPreview()
                    end,
                },

                -- Preview
                inn_previewHeader = {
                    type = "header",
                    name = "Preview",
                    order = 50,
                    hidden = isInnDisabled,
                },
                inn_previewDesc = {
                    type = "description",
                    name = "Toggle the preview to show the tracker. Drag to reposition it.",
                    order = 51,
                    hidden = isInnDisabled,
                },
                inn_previewToggle = {
                    type = "execute",
                    name = function()
                        return HET:IsInnPreviewActive() and "Hide Preview" or "Show Preview"
                    end,
                    desc = "Show or hide the tracker so you can drag it into position.",
                    order = 52,
                    hidden = isInnDisabled,
                    func = function()
                        if HET:IsInnPreviewActive() then
                            HET:HideInnPreview()
                        else
                            HET:ShowInnPreview()
                        end
                    end,
                },
                inn_resetPosition = {
                    type = "execute",
                    name = "Reset Position",
                    desc = "Reset the tracker position to the center of the screen.",
                    order = 53,
                    hidden = isInnDisabled,
                    func = function()
                        HET.db.profile.inn_position = nil
                        local t = ns.trackers["innervate"]
                        if t then
                            local pos = t.config.defaultPos
                            t.iconFrame:ClearAllPoints()
                            t.iconFrame:SetPoint(pos[1], UIParent, pos[2], pos[3], pos[4])
                            t.barFrame:ClearAllPoints()
                            t.barFrame:SetPoint(pos[1], UIParent, pos[2], pos[3], pos[4])
                            t.circleFrame:ClearAllPoints()
                            t.circleFrame:SetPoint(pos[1], UIParent, pos[2], pos[3], pos[4])
                            t:RefreshPreview()
                        end
                    end,
                },
            },
        },
        addonOptions = {
            type = "group",
            name = "General Options",
            order = 3,
            args = {
                showMinimapIcon = {
                    type = "toggle",
                    name = "Show Minimap Icon",
                    desc = "Show or hide the minimap button.",
                    order = 1,
                    width = "full",
                    get = function() return not HET.db.global.minimapIcon.hide end,
                    set = function(_, val)
                        HET.db.global.minimapIcon.hide = not val
                        local LibDBIcon = LibStub("LibDBIcon-1.0", true)
                        if not LibDBIcon then return end
                        if val then
                            LibDBIcon:Show("BuzzardAuras")
                        else
                            LibDBIcon:Hide("BuzzardAuras")
                        end
                    end,
                },
            },
        },
    },
}

function HET:RegisterOptions()
    AC:RegisterOptionsTable(addonName, options)

    local g = self.db.global
    local w = g.optionsPanelW or 650
    local h = g.optionsPanelH or 450
    ACD:SetDefaultSize(addonName, w, h)
end

-- Hook into AceConfigDialog to customize the panel frame
local origOpen = ACD.Open
ACD.Open = function(self, appName, ...)
    origOpen(self, appName, ...)
    if appName ~= addonName then return end

    local aceFrame = self.OpenFrames and self.OpenFrames[addonName]
    if not aceFrame then return end

    local frame = aceFrame.frame
    if not frame or frame._hetHooked then return end
    frame._hetHooked = true

    -- Save panel size on resize
    frame:HookScript("OnSizeChanged", function(self, width, height)
        if HET.db then
            HET.db.global.optionsPanelW = width
            HET.db.global.optionsPanelH = height
        end
    end)

    -- Find and hide the native AceConfigDialog "Close" button
    for i = 1, select("#", frame:GetChildren()) do
        local child = select(i, frame:GetChildren())
        if child and child.GetObjectType and child:GetObjectType() == "Button" then
            local text = child.GetText and child:GetText()
            if text == CLOSE or text == "Close" then
                child:Hide()
                frame._hetNativeCloseBtn = child
                break
            end
        end
    end

    -- Hide previews when panel closes
    frame:HookScript("OnHide", function()
        HET:HidePreview()
        HET:HideInnPreview()
    end)

    -- Add X button in top-right corner
    local xBtn = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
    xBtn:SetSize(26, 26)
    xBtn:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -8, -8)
    xBtn:SetScript("OnClick", function()
        PlaySound(799)
        aceFrame:Hide()
    end)
end
