local GC = select(2, ...)

local Commands = {}
GC.Commands = Commands

local private = {}

--[[
    Register all chat commands
    Called during addon initialization
]]
function Commands.Register()
    GC.Debug.Log("Commands.Register()")

    -- Register /lahistory command
    GC:RegisterChatCommand("lahistory", private.HandleHistoryCommand)
end

--[[
    Handle /lahistory command
    Usage:
        /lahistory - Open history UI
        /lahistory reset - Reset all session history
        /lahistory export - Export all sessions to JSON
        /lahistory save - Save current session to history
]]
function private.HandleHistoryCommand(input)
    GC.Debug.Log("HandleHistoryCommand: " .. tostring(input))

    local args = {}
    for word in (input or ""):gmatch("%S+") do
        table.insert(args, word:lower())
    end

    local subcommand = args[1]

    if not subcommand or subcommand == "" then
        -- Open history UI
        GC.SessionHistoryUI.Show()

    elseif subcommand == "reset" then
        -- Confirm and reset all sessions
        private.ConfirmReset()

    elseif subcommand == "export" then
        -- Export all sessions
        private.ExportAll()

    elseif subcommand == "save" then
        -- Save current session
        if GC.Session.IsRunning() then
            GC.SessionHistory.SaveCurrentSession()
        else
            GC:Print("No active session to save.")
        end

    elseif subcommand == "help" then
        private.ShowHelp()

    else
        GC:Print("Unknown subcommand: " .. subcommand)
        private.ShowHelp()
    end
end

--[[
    Show help for /lahistory command
]]
function private.ShowHelp()
    GC:Print("|cffffd100GallywixCoins - Session History Commands:|r")
    GC:Print("  |cff00ff00/lahistory|r - Open session history window")
    GC:Print("  |cff00ff00/lahistory save|r - Save current session to history")
    GC:Print("  |cff00ff00/lahistory export|r - Export all sessions to JSON")
    GC:Print("  |cff00ff00/lahistory reset|r - Reset all session history")
    GC:Print("  |cff00ff00/lahistory help|r - Show this help")
end

--[[
    Confirm and reset all sessions
]]
function private.ConfirmReset()
    StaticPopupDialogs["LA_RESET_ALL_SESSIONS"] = {
        text = "|cffff0000Warning!|r\n\nThis will permanently delete ALL session history.\n\nAre you sure you want to continue?",
        button1 = "Yes, Reset All",
        button2 = "Cancel",
        OnAccept = function()
            local count = GC.SessionHistory.ResetAllSessions()
            GC:Print("Session history reset. Deleted " .. count ..
                         " session(s).")
        end,
        timeout = 0,
        whileDead = true,
        hideOnEscape = true,
        preferredIndex = 3
    }
    StaticPopup_Show("LA_RESET_ALL_SESSIONS")
end

--[[
    Export all sessions to JSON
]]
function private.ExportAll()
    local json, err = GC.SessionHistory.ExportAllSessionsToJSON()

    if not json then
        GC:Print("Export failed: " .. (err or "No sessions to export"))
        return
    end

    -- Show export dialog
    local AceGUI = LibStub("AceGUI-3.0")

    local exportFrame = AceGUI:Create("Frame")
    exportFrame:SetTitle("Export All Sessions")
    exportFrame:SetWidth(600)
    exportFrame:SetHeight(400)
    exportFrame:SetLayout("Fill")

    local editBox = AceGUI:Create("MultiLineEditBox")
    editBox:SetLabel("JSON Data (select all and copy with Ctrl+C):")
    editBox:SetText(json)
    editBox:SetFullWidth(true)
    editBox:SetFullHeight(true)
    editBox:DisableButton(true)
    exportFrame:AddChild(editBox)

    -- Select all text when focused
    editBox.editBox:SetScript("OnEditFocusGained",
                              function(self) self:HighlightText() end)

    -- Focus and select all
    C_Timer.After(0.1, function()
        editBox:SetFocus()
        editBox.editBox:HighlightText()
    end)

    local sessionCount = GC.SessionHistory.GetSessionCount()
    GC:Print("Exported " .. sessionCount ..
                 " session(s) to JSON. Select all and copy (Ctrl+A, Ctrl+C)")
end

