const db = require('../db')

async function add({ guildId, channelId, messageId, mapping, createdBy }) {
  await db.query(
    `INSERT INTO role_menus (guild_id, channel_id, message_id, mapping, created_by)
     VALUES (?, ?, ?, ?, ?)`,
    [guildId, channelId, messageId, JSON.stringify(mapping), createdBy || null],
  )
}

async function getByMessageId(messageId) {
  const rows = await db.query('SELECT * FROM role_menus WHERE message_id = ? LIMIT 1', [messageId])
  if (!rows[0]) return null
  return { ...rows[0], mapping: JSON.parse(rows[0].mapping) }
}

module.exports = { add, getByMessageId }
