-- ============================================================================
-- AIOS CopyBox - Main Initialization File
-- ============================================================================
local AddonName = "AIOS_CopyBox"

-- Create main addon table
AIOS_CopyBox = {
    name = AddonName,
    version = "1.0.1",
    author = "Poorkingz",
    initialized = false
}

-- Store reference locally
local Addon = AIOS_CopyBox

-- Initialize addon
local function Initialize()
    if Addon.initialized then return end

    -- Init saved variables
    if not AIOS_CopyBoxDB then
        AIOS_CopyBoxDB = {}
    end

    -- Initialize modules in dependency order
    if AIOS_CopyBox_Logger then
        AIOS_CopyBox_Logger:Initialize()
    end

    if AIOS_CopyBox_ThemeManager then
        AIOS_CopyBox_ThemeManager:Initialize()
        -- Restore saved theme
        if AIOS_CopyBoxDB.themeIndex then
            AIOS_CopyBox_ThemeManager:SetTheme(AIOS_CopyBoxDB.themeIndex)
        end
    end

    if AIOS_CopyBox_ChatManager then
        AIOS_CopyBox_ChatManager:Initialize()
        -- Restore saved channel
        if AIOS_CopyBoxDB.lastChannel then
            AIOS_CopyBox_ChatManager.lastViewedChannel = AIOS_CopyBoxDB.lastChannel
        end
    end

    if AIOS_CopyBox_MainFrame then
        AIOS_CopyBox_MainFrame:Initialize()
        -- Restore saved position
        if AIOS_CopyBoxDB.framePos and AIOS_CopyBox_MainFrame.frame then
            AIOS_CopyBox_MainFrame:RestorePosition()
        end
    end

    if AIOS_CopyBox_ChatButtons then
        AIOS_CopyBox_ChatButtons:Initialize()
    end

    -- Register slash commands
    SLASH_AIOSCOPYBOX1 = "/copybox"
    SlashCmdList["AIOSCOPYBOX"] = function()
        if AIOS_CopyBox_ChatManager then
            AIOS_CopyBox_ChatManager:OpenCopyBox()
        end
    end

    SLASH_AIOSCOPYBOX2 = "/cb"
    SlashCmdList["AIOSCOPYBOX2"] = function()
        if AIOS_CopyBox_ChatManager then
            AIOS_CopyBox_ChatManager:OpenCopyBox()
        end
    end

    -- Debug commands
    SLASH_AIOSCOPYBOXDEBUG1 = "/copyboxdebug"
    SlashCmdList["AIOSCOPYBOXDEBUG"] = function()
        if AIOS_CopyBox_ChatManager then
            DEFAULT_CHAT_FRAME:AddMessage("Available channels: " .. #AIOS_CopyBox_ChatManager.availableChannels)
            for i, channel in ipairs(AIOS_CopyBox_ChatManager.availableChannels) do
                DEFAULT_CHAT_FRAME:AddMessage("Channel " .. i .. ": " .. channel.id .. " - " .. channel.name)
            end
        end

        if AIOS_CopyBox_ThemeManager then
            local theme = AIOS_CopyBox_ThemeManager:GetCurrentTheme()
            DEFAULT_CHAT_FRAME:AddMessage("Current theme: " .. (theme and theme.name or "None"))
        end
    end

    -- Theme test commands
    SLASH_AIOSTHEMETEST1 = "/themestest"
    SlashCmdList["AIOSTHEMETEST"] = function()
        if AIOS_CopyBox_ThemeManager then
            local currentTheme = AIOS_CopyBox_ThemeManager:GetCurrentTheme()
            DEFAULT_CHAT_FRAME:AddMessage("Current theme: " .. (currentTheme and currentTheme.name or "None"))
            DEFAULT_CHAT_FRAME:AddMessage("Theme index: " .. AIOS_CopyBox_ThemeManager.currentThemeIndex)

            local themes = AIOS_CopyBox_ThemeManager.availableThemes or {}
            for i, theme in ipairs(themes) do
                DEFAULT_CHAT_FRAME:AddMessage("Theme " .. i .. ": " .. theme.name)
            end
        end
    end

    SLASH_AIOSSETTHEME1 = "/settheme"
    SlashCmdList["AIOSSETTHEME"] = function(msg)
        local index = tonumber(msg)
        if index and AIOS_CopyBox_ThemeManager then
            if AIOS_CopyBox_ThemeManager:SetTheme(index) then
                DEFAULT_CHAT_FRAME:AddMessage("Set theme to index: " .. index)
            else
                DEFAULT_CHAT_FRAME:AddMessage("Invalid theme index: " .. index)
            end
        else
            DEFAULT_CHAT_FRAME:AddMessage("Usage: /settheme <number>")
        end
    end

    -- Version and theme debug commands
    SLASH_AIOSVERSION1 = "/copyboxversion"
    SlashCmdList["AIOSVERSION"] = function()
        local version = AIOS_CopyBox_Config.version
        DEFAULT_CHAT_FRAME:AddMessage("=== AIOS CopyBox Version Info ===")
        DEFAULT_CHAT_FRAME:AddMessage("Build: " .. version.currentBuild)
        DEFAULT_CHAT_FRAME:AddMessage("Retail: " .. tostring(version.isRetail))
        DEFAULT_CHAT_FRAME:AddMessage("Classic: " .. tostring(version.isClassic))
        DEFAULT_CHAT_FRAME:AddMessage("Modern Retail: " .. tostring(version:isWarWithinOrHigher()))
        DEFAULT_CHAT_FRAME:AddMessage("Advanced Themes: " .. tostring(AIOS_CopyBox_Config:HasAdvancedThemes()))

        if AIOS_CopyBox_ThemeManager then
            DEFAULT_CHAT_FRAME:AddMessage("Available Themes: " .. AIOS_CopyBox_ThemeManager:GetThemeCount())
            for i, theme in ipairs(AIOS_CopyBox_ThemeManager.availableThemes) do
                DEFAULT_CHAT_FRAME:AddMessage("  " .. i .. ": " .. theme.name .. " (" .. (theme.type or "basic") .. ")")
            end
        end
    end

    -- Force advanced themes for testing
    SLASH_AIOSFORCEADV1 = "/forceadvanced"
    SlashCmdList["AIOSFORCEADV"] = function()
        AIOS_CopyBox_Config.behavior.forceAdvancedThemes = not AIOS_CopyBox_Config.behavior.forceAdvancedThemes
        DEFAULT_CHAT_FRAME:AddMessage("Force Advanced Themes: " .. tostring(AIOS_CopyBox_Config.behavior.forceAdvancedThemes))
        DEFAULT_CHAT_FRAME:AddMessage("Reload UI to apply changes!")
    end

    -- Log success (debug only)
    if AIOS_CopyBox_Logger then
        AIOS_CopyBox_Logger:Debug("AIOS CopyBox v" .. Addon.version .. " loaded")
    end

    Addon.initialized = true
end

-- Event handling
local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event, addonName)
    if event == "ADDON_LOADED" and addonName == AddonName then
        Initialize()
        self:UnregisterEvent("ADDON_LOADED")
    end
end)
