-------------------------
-- AiB BuffWatcher V2 - Init
-------------------------
AiB = AiB or {}
AiB.BWV2Settings = AiB.BWV2Settings or {}

-- Eintrittspunkt für aib_settings.lua
function AiB.BWV2Settings.AddToSettingsPanel(parent, startY)
    -- Direct mode: baut Content direkt in den scrollChild ohne inneren ScrollFrame
    AiB.BWV2Settings.Init(parent, true)
    return startY - 2000
end

-- Zone-Events
local zoneFrame = CreateFrame("Frame")
zoneFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
zoneFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
zoneFrame:SetScript("OnEvent", function()
    local _, it = IsInInstance()
    local diff   = select(3, GetInstanceInfo())
    local ns     = AiB.BWV2NS
    if ns and ns.ZoneUtil then
        ns.ZoneUtil._instanceType = it or "none"
        ns.ZoneUtil._inMythicPlus = (it == "party" and diff == 8)
        for _, fn in pairs(ns.ZoneUtil._callbacks) do pcall(fn) end
    end
end)

-- Login-Init
local initFrame = CreateFrame("Frame")
initFrame:RegisterEvent("PLAYER_LOGIN")
initFrame:SetScript("OnEvent", function(self, event)
    if event ~= "PLAYER_LOGIN" then return end
    local ns = AiB.BWV2NS
    C_Timer.After(1, function()
        -- Module sind bereits korrekt in ns eingehängt (durch die Dateien selbst)
        -- Nur InitSavedVars, LocalizeSpecNames und Core:Init aufrufen
        if AiB.BWV2 and AiB.BWV2.InitSavedVars then
            AiB.BWV2:InitSavedVars()
        end
        if ns and ns.BWV2Categories and ns.BWV2Categories.LocalizeSpecNames then
            ns.BWV2Categories:LocalizeSpecNames()
        end
        if ns and ns.BWV2Core and ns.BWV2Core.Init then
            ns.BWV2Core:Init()
        end
    end)
end)
