const express = require('express');
const { getConnection, getLastMessages, getMentions } = require('../libs/mysql');

const router = express.Router();

router.get('/', async (req, res) => {
    const connection = await getConnection();
    const messages = await getLastMessages(connection);

    for (const message of messages) {
        if (message.content.includes('@')) {
            const mentions = await getMentions(connection, message.id);
            message.mentions = mentions;
        } else {
            message.mentions = [];
        }
    }

    connection.end();
    res.send(messages);
});

module.exports = router;