-- ============================================================================
-- Vamoose's Companion - Mood Engine
-- Tracks player mood via game events. Session-only (not persisted).
-- Adapted from archive/VamooseAssistant 2/Intelligence/VA_MoodEngine.lua
-- ============================================================================

VC = VC or {}
VC.Mood = {
    intensity = 0,       -- float -1 to 1
    state = "NEUTRAL",   -- current mood label
    deathCount = 0,      -- deaths in current streak window
    lastDeathTime = 0,   -- for streak detection
    lastSignalTime = 0,  -- for grace period before decay
    inCombat = false,
    initialized = false,
}

-- ============================================================================
-- INITIALIZATION
-- ============================================================================

function VC.Mood:Initialize()
    if self.initialized then return end

    -- Decay ticker: every 10 seconds
    C_Timer.NewTicker(10, function() self:DecayMood() end)

    -- Combat tracking
    local cf = CreateFrame("Frame")
    cf:RegisterEvent("PLAYER_REGEN_DISABLED")
    cf:RegisterEvent("PLAYER_REGEN_ENABLED")
    cf:SetScript("OnEvent", function(_, event)
        self.inCombat = (event == "PLAYER_REGEN_DISABLED")
    end)

    self.initialized = true
end

-- ============================================================================
-- SIGNAL PROCESSING
-- ============================================================================

function VC.Mood:ApplySignal(signalType, magnitude)
    local MOOD = VC.Constants.MOOD
    magnitude = magnitude or MOOD.SIGNALS[signalType] or 0
    if magnitude == 0 then return end

    self.intensity = math.max(-1, math.min(1, self.intensity + magnitude))
    self.lastSignalTime = GetTime()
    self:UpdateState()

    if VC.Store:GetState().config.debug then
        print(format("|cFFA0522D[VC Mood]|r %s (%.2f) -> %s (%.2f)",
            signalType, magnitude, self.state, self.intensity))
    end

    VC.EventBus:Trigger("VC_MOOD_CHANGED", { mood = self.state, intensity = self.intensity })
end

-- ============================================================================
-- DEATH TRACKING
-- ============================================================================

function VC.Mood:RecordDeath()
    local now = GetTime()
    local MOOD = VC.Constants.MOOD

    -- Reset streak if outside window
    if (now - self.lastDeathTime) > MOOD.DEATH_STREAK_WINDOW then
        self.deathCount = 0
    end

    self.deathCount = self.deathCount + 1
    self.lastDeathTime = now

    if self.deathCount >= MOOD.DEATH_STREAK_COUNT then
        self:ApplySignal("DEATH_STREAK")
    else
        self:ApplySignal("DEATH")
    end
end

-- ============================================================================
-- DECAY
-- ============================================================================

function VC.Mood:DecayMood()
    if self.inCombat then return end
    if self.intensity == 0 then return end

    -- Grace period after last signal
    local elapsed = GetTime() - self.lastSignalTime
    if elapsed < VC.Constants.MOOD.GRACE_PERIOD then return end

    -- Decay toward 0 by DECAY_RATE per second (ticker is 10s)
    local decayAmount = VC.Constants.MOOD.DECAY_RATE * 10
    if self.intensity > 0 then
        self.intensity = math.max(0, self.intensity - decayAmount)
    else
        self.intensity = math.min(0, self.intensity + decayAmount)
    end

    local oldState = self.state
    self:UpdateState()

    if oldState ~= self.state then
        VC.EventBus:Trigger("VC_MOOD_CHANGED", { mood = self.state, intensity = self.intensity })
    end
end

-- ============================================================================
-- STATE CALCULATION
-- ============================================================================

function VC.Mood:UpdateState()
    local T = VC.Constants.MOOD.THRESHOLDS
    if self.intensity <= T.FRUSTRATED then
        self.state = "FRUSTRATED"
    elseif self.intensity <= T.STRESSED then
        self.state = "STRESSED"
    elseif self.intensity >= T.TRIUMPHANT then
        self.state = "TRIUMPHANT"
    elseif self.intensity >= T.EXCITED then
        self.state = "EXCITED"
    elseif self.intensity >= T.FOCUSED then
        self.state = "FOCUSED"
    else
        self.state = "NEUTRAL"
    end
end

-- ============================================================================
-- GETTERS
-- ============================================================================

function VC.Mood:GetMood()
    return self.state
end

function VC.Mood:GetIntensity()
    return self.intensity
end
