EbonholdHub = EbonholdHub or {}

local eventFrame = CreateFrame("Frame")
local initialized = false

local function EnsureSavedVariables()
    EbonholdHubDB = EbonholdHubDB or {
        builds        = {},
        minimapAngle  = 220,
        globalSettings = {
            evalDelay     = 2,
            toastDuration = 3,
            toastPinned   = false,
            toastChargesOpacity = 0.92,
        },
    }
    -- TEMP: offer drop-rate logger. Remove after analysis is done.
    EbonholdHubDB.offerLog = EbonholdHubDB.offerLog or {}
    EbonholdHubDB.minimapAngle = EbonholdHubDB.minimapAngle or 220
    EbonholdHubDB.globalSettings = EbonholdHubDB.globalSettings or {}
    EbonholdHubDB.globalSettings.evalDelay     = EbonholdHubDB.globalSettings.evalDelay     or 2
    EbonholdHubDB.globalSettings.toastDuration = EbonholdHubDB.globalSettings.toastDuration or 3
    EbonholdHubDB.globalSettings.toastPinned   = EbonholdHubDB.globalSettings.toastPinned == true
    EbonholdHubDB.globalSettings.lootAutoNeed  = EbonholdHubDB.globalSettings.lootAutoNeed == true
    local opacity = EbonholdHubDB.globalSettings.toastChargesOpacity
    if type(opacity) ~= "number" then
        EbonholdHubDB.globalSettings.toastChargesOpacity = 0.92
    else
        EbonholdHubDB.globalSettings.toastChargesOpacity = math.max(0.15, math.min(1, opacity))
    end

    EbonholdHubCharDB = EbonholdHubCharDB or {
        activeBuildId = nil,
    }

    EbonholdHubPickLog = EbonholdHubPickLog or { entries = {} }

    if EbonholdHub.Build and EbonholdHub.Build.EnsureDatabase then
        EbonholdHub.Build.EnsureDatabase()
    end
    if EbonholdHub.Catalog and EbonholdHub.Catalog.EnsureStorage then
        EbonholdHub.Catalog.EnsureStorage()
    end
end

local function RefreshUi()
    if EbonholdHub.AffixData and EbonholdHub.AffixData.RefreshFromServer then
        EbonholdHub.AffixData.RefreshFromServer()
    end
    if EbonholdHub.BuildList and EbonholdHub.BuildList.Refresh then
        EbonholdHub.BuildList.Refresh()
    end
    if EbonholdHub.MainWindow and EbonholdHub.MainWindow._ShowInitialView then
        local frame = EbonholdHub.MainWindow._frame
        if frame and frame:IsShown() then
            EbonholdHub.MainWindow._ShowInitialView()
        end
    end
    if EbonholdHub.PublicBuildsView and EbonholdHub.PublicBuildsView.RefreshIfMounted then
        EbonholdHub.PublicBuildsView.RefreshIfMounted()
    end
end

local function TryInitialize()
    if initialized then
        EnsureSavedVariables()
        RefreshUi()
        return
    end

    if not ProjectEbonhold then
        return
    end

    EnsureSavedVariables()

    if EbonholdHub.AffixData and EbonholdHub.AffixData.Init then
        EbonholdHub.AffixData.Init()
    end

    EbonholdHub.Session.Init()
    EbonholdHub.SessionHistory.Init()
    if EbonholdHub.TierData then EbonholdHub.TierData.Init() end
    if EbonholdHub.AffixOwnership and EbonholdHub.AffixOwnership.Init then
        EbonholdHub.AffixOwnership.Init()
    end
    if EbonholdHub.EchoOwnership and EbonholdHub.EchoOwnership.Init then
        EbonholdHub.EchoOwnership.Init()
    end
    EbonholdHub.Toast.Init()
    if EbonholdHub.ActivityFeed and EbonholdHub.ActivityFeed.Init then
        EbonholdHub.ActivityFeed.Init()
    end
    EbonholdHub.WelcomeView.Init()
    EbonholdHub.TalentsView.Init()
    EbonholdHub.GearView.Init()
    if EbonholdHub.TierView then EbonholdHub.TierView.Init() end
    if EbonholdHub.ItemCatalog and EbonholdHub.ItemCatalog.Init then
        EbonholdHub.ItemCatalog.Init()
    end
    EbonholdHub.Catalog.Init()
    EbonholdHub.MinimapButton.Init()
    EbonholdHub.MainWindow.Init()
    if EbonholdHub.AutoPickDebug and EbonholdHub.AutoPickDebug.Init then
        EbonholdHub.AutoPickDebug.Init()
    end
    EbonholdHub.Automation.Init()
    if EbonholdHub.LootTracker then EbonholdHub.LootTracker.Init() end
    if EbonholdHub.LootAlert then EbonholdHub.LootAlert.Init() end
    if EbonholdHub.JournalLoadoutHook and EbonholdHub.JournalLoadoutHook.Init then
        EbonholdHub.JournalLoadoutHook.Init()
    end

    initialized = true
end

local function OnAddonLoaded(addonName)
    if addonName == "EbonholdHub" then
        TryInitialize()
    end
end

eventFrame:RegisterEvent("ADDON_LOADED")
eventFrame:RegisterEvent("PLAYER_LOGIN")
eventFrame:SetScript("OnEvent", function(self, event, ...)
    if event == "ADDON_LOADED" then
        OnAddonLoaded(...)
    elseif event == "PLAYER_LOGIN" then
        TryInitialize()
        -- Defer non-critical UI refresh so login/zoning stays responsive.
        if C_Timer and C_Timer.After then
            C_Timer.After(3, RefreshUi)
        else
            RefreshUi()
        end
    end
end)
