-- Emote.lua (with Cooldown Logic, Auto-Prefix, and Model Name)

-- Default Buzzer emotes
Buzzer_DefaultEmotes = {
    "Buzzer crackles with static.",
    "Buzzer flashes a faint green light.",
    "Buzzer lets out a series of beeps.",
    "Buzzer hisses like a snake.",
    "Buzzer buzzes violently.",
    "Buzzer emits a deep hum.",
    "Buzzer gives off a short spark.",
    "Buzzer flashes in rhythmic pulses.",
    "Buzzer beeps in Morse-like patterns.",
    "Buzzer shudders slightly.",
    "Buzzer emits a faint whine.",
    "Buzzer chirps briefly.",
    "Buzzer vibrates against the belt.",
    "Buzzer lights up and quickly dims.",
    "Buzzer clicks rapidly.",
    "Buzzer lets out a distorted voice snippet.",
    "Buzzer produces a low rumbling sound.",
    "Buzzer sputters and pops.",
    "Buzzer spins its indicator dial rapidly.",
    "Buzzer rattles against its casing."
}

-- Merge defaults and customs
function Buzzer_GetAvailableEmotes()
    local emotes = {}
    for _, e in ipairs(Buzzer_DefaultEmotes) do
        table.insert(emotes, e)
    end
    if BuzzerSettings and BuzzerSettings.customEmotes then
        for _, e in ipairs(BuzzerSettings.customEmotes) do
            table.insert(emotes, e)
        end
    end
    return emotes
end

-- Cooldown tracking
local lastEmoteTime = 0
local COOLDOWN_SECONDS = 5

-- Play a random incoming emote with cooldown and auto-prefix
function Buzzer_PlayIncomingEmote()
    local now = GetTime()
    if now - lastEmoteTime < COOLDOWN_SECONDS then
        return
    end

    local emotes = Buzzer_GetAvailableEmotes()
    if #emotes > 0 then
        local emote = emotes[math.random(#emotes)]

        -- Replace "Buzzer" in the emote with the actual model name if available
        local modelName = "Buzzer"
        if BuzzerSettings and BuzzerSettings.radioModel and Buzzer_Models and Buzzer_Models[BuzzerSettings.radioModel] and Buzzer_Models[BuzzerSettings.radioModel].name then
            modelName = Buzzer_Models[BuzzerSettings.radioModel].name
        end

        emote = emote:gsub("Buzzer", modelName)

        SendChatMessage("'s " .. emote, "EMOTE")
        lastEmoteTime = now
    end
end
