local lastMessage = {}

RegisterServerEvent('gbcore-phone:sendMessage')
AddEventHandler('gbcore-phone:sendMessage', function(message)
    local src = source
    table.insert(lastMessages, { from = src, text = message, time = os.time() })
    for _, id in ipairs(GetPlayers()) do
        TriggerClientEvent('gbcore-phone:receiveMessage', id, src, message)
    end
end)

RegisterCommand('phonehistory', function(source, args, raw)
    if source == 0 then
        for i, m in ipairs(lastMessages) do
            print(i, m.from, m.text, os.date("%Y-%m-%d %H:%M:%S", m.time))
        end
    else
        TriggerClientEvent('chat:addMessage', source, { args = { '[PHONE]', 'History printed to server console (admin)' } })
    end
end, true)