local addonName, ns = ...
local L = ns.L

-- Combat logging for raids/M+
-- Auto mode:  enables silently on instance enter (default).
-- Popup mode: shows a dialog asking the user first.
-- Toggle module on/off with: /bqcombatlog

local isLogging = false

-- StaticPopup for popup mode
if not StaticPopupDialogs["BRIGHTQOL_COMBATLOG_POPUP"] then
    StaticPopupDialogs["BRIGHTQOL_COMBATLOG_POPUP"] = {
        text = "%s\n|cffaaaaaa(%s)|r",
        button1 = L["COMBATLOGGER_ENABLE_BTN"] or "Enable Logging",
        button2 = L["COMBATLOGGER_SKIP_BTN"]   or "Skip",
        OnAccept = function()
            LoggingCombat(true)
            isLogging = true
            ns:Log(L["COMBATLOGGER_ENABLED_MSG"], "03E826")
        end,
        OnCancel = function() end,
        timeout = 0, whileDead = false, hideOnEscape = true,
    }
end

local function OnZoneChanged(zoneData)
    local db = BrightQoL.combatLogger
    if not db or not db.enabled then
        if isLogging then
            LoggingCombat(false)
            isLogging = false
        end
        return
    end

    local shouldTrack = (zoneData.instanceType == "raid" or zoneData.instanceType == "party")

    if not shouldTrack then
        -- Left instance — stop logging silently, dismiss popup if open
        StaticPopup_Hide("BRIGHTQOL_COMBATLOG_POPUP")
        if isLogging then
            LoggingCombat(false)
            isLogging = false
        end
        return
    end

    if db.popupMode then
        -- Ask the user via popup
        StaticPopup_Show("BRIGHTQOL_COMBATLOG_POPUP", zoneData.zoneName, zoneData.difficultyName)
    else
        -- Auto-enable and notify via chat
        LoggingCombat(true)
        isLogging = true
        ns:Log(L["COMBATLOGGER_ENABLED_MSG"], "03E826")
    end
end

-- Slash command: /bqcombatlog — toggle module on/off
SLASH_BRIGHTQOL_COMBATLOG1 = "/bqcombatlog"
SlashCmdList["BRIGHTQOL_COMBATLOG"] = function()
    local db = BrightQoL.combatLogger
    if not db then return end

    db.enabled = not db.enabled

    if db.enabled then
        ns:Log(L["COMBATLOGGER_SLASH_ENABLED"], "03E826")
    else
        StaticPopup_Hide("BRIGHTQOL_COMBATLOG_POPUP")
        if isLogging then
            LoggingCombat(false)
            isLogging = false
        end
        ns:Log(L["COMBATLOGGER_SLASH_DISABLED"], "FF4444")
    end
end

local loader = CreateFrame("Frame", "BrightQoL_CombatLogger")
loader:RegisterEvent("PLAYER_LOGIN")

loader:SetScript("OnEvent", function(self, event)
    if event == "PLAYER_LOGIN" then
        if not BrightQoL.combatLogger then
            BrightQoL.combatLogger = { enabled = true, popupMode = false }
        end
        local db = BrightQoL.combatLogger
        if db.enabled == nil then db.enabled = true end
        if db.popupMode == nil then db.popupMode = false end

        -- Note: we do NOT sync isLogging = LoggingCombat() here intentionally.
        -- We only manage logging sessions that BrightQoL itself started,
        -- so we never stop logging begun by other addons (BWL/WCL Helper/MRT).

        if ns.ZoneUtil and ns.ZoneUtil.RegisterCallback then
            ns.ZoneUtil.RegisterCallback("CombatLogger", OnZoneChanged)
            C_Timer.After(0.5, function()
                OnZoneChanged(ns.ZoneUtil.GetCurrentZone())
            end)
        end

        self:UnregisterEvent("PLAYER_LOGIN")
    end
end)

ns.CombatLogger = loader
