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 ACD = LibStub("AceConfigDialog-3.0")

-------------------------------------------------------------------------------
-- Slash Command
-------------------------------------------------------------------------------
function HET:SetupSlashCommand()
    self:RegisterChatCommand("buzzardauras", "SlashCommand")
    self:RegisterChatCommand("ba", "SlashCommand")
    self:RegisterChatCommand("het", "SlashCommand")
end

function HET:SlashCommand(input)
    self:OpenOptions()
end

function HET:OpenOptions()
    ACD:Open(addonName)
end

-------------------------------------------------------------------------------
-- Minimap Button (LibDataBroker + LibDBIcon)
-------------------------------------------------------------------------------
function HET:SetupMinimapButton()
    local LDB = LibStub("LibDataBroker-1.1", true)
    local LibDBIcon = LibStub("LibDBIcon-1.0", true)
    if not LDB or not LibDBIcon then return end

    local launcher = LDB:NewDataObject("BuzzardAuras", {
        type  = "launcher",
        label = "Buzzard Auras",
        icon  = "Interface\\AddOns\\BuzzardAuras\\Media\\BuzzardAuras",
        OnClick = function(_, button)
            if button == "LeftButton" then
                local openFrame = ACD.OpenFrames and ACD.OpenFrames[addonName]
                if openFrame and openFrame.frame and openFrame.frame:IsShown() then
                    ACD:Close(addonName)
                else
                    HET:OpenOptions()
                end
            end
        end,
        OnTooltipShow = function(tooltip)
            tooltip:AddLine("|cff11ace9Buzzard Auras|r")
            tooltip:AddLine("Left click to open settings", 0.8, 0.8, 0.8)
        end,
    })

    ns.launcher = launcher
    LibDBIcon:Register("BuzzardAuras", launcher, self.db.global.minimapIcon)
end

-------------------------------------------------------------------------------
-- Addon Compartment (global functions referenced in TOC)
-------------------------------------------------------------------------------
function BuzzardAuras_AddonCompartmentFunc(addonName, menuButton)
    local HET = LibStub("AceAddon-3.0"):GetAddon("BuzzardAuras", true)
    if HET and HET.OpenOptions then HET:OpenOptions() end
end

function BuzzardAuras_AddonCompartmentFuncOnEnter(addonName, menuButton)
    GameTooltip:SetOwner(menuButton, "ANCHOR_LEFT")
    GameTooltip:AddLine("|cff11ace9Buzzard Auras|r", 1, 1, 1, true)
    GameTooltip:AddLine("Click to open the options panel.", 0.8, 0.8, 0.8, true)
    GameTooltip:Show()
end

function BuzzardAuras_AddonCompartmentFuncOnLeave(addonName, menuButton)
    GameTooltip:Hide()
end

-------------------------------------------------------------------------------
-- Blizzard Settings Panel
-------------------------------------------------------------------------------
function HET:SetupBlizzardPanel()
    if not (Settings and Settings.RegisterCanvasLayoutCategory) then return end

    local panel = CreateFrame("Frame")
    panel.name = addonName

    local initialized = false
    panel:SetScript("OnShow", function(self)
        if initialized then return end
        initialized = true

        local title = self:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
        title:SetPoint("TOPLEFT", 16, -16)
        title:SetText("|cff11ace9Buzzard Auras|r")

        local desc = self:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
        desc:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
        desc:SetText("Spatial Paradox and Innervate tracker for Healers in Midnight. /het")

        local btn = CreateFrame("Button", nil, self, "UIPanelButtonTemplate")
        btn:SetSize(160, 24)
        btn:SetPoint("TOPLEFT", desc, "BOTTOMLEFT", 0, -16)
        btn:SetText("Open Options")
        btn:SetScript("OnClick", function()
            -- Close Blizzard Settings panel and game menu so the AceConfig
            -- dialog isn't obscured. HideUIPanel is safe; SettingsPanel:Close()
            -- routes through ToggleGameMenu -> SpellStopCasting (protected)
            -- and would trigger ADDON_ACTION_FORBIDDEN.
            if not InCombatLockdown() then
                if SettingsPanel and SettingsPanel:IsShown() then
                    HideUIPanel(SettingsPanel)
                end
                if GameMenuFrame and GameMenuFrame:IsShown() then
                    HideUIPanel(GameMenuFrame)
                end
            end
            HET:OpenOptions()
        end)
    end)

    local category = Settings.RegisterCanvasLayoutCategory(panel, "Buzzard Auras")
    Settings.RegisterAddOnCategory(category)
end

-------------------------------------------------------------------------------
-- Init (called from Core.lua OnInitialize hook)
-------------------------------------------------------------------------------
function HET:SetupUI()
    self:SetupSlashCommand()
    self:SetupMinimapButton()
    self:SetupBlizzardPanel()
end
