const { EmbedBuilder, MessageFlags } = require("discord.js")
const { client, mClient } = require("../../../index")

module.exports = {
    name: 'quotesadd',
    description: 'adds a quote to the database by replying to the message',
    category: 'Quotes',
    aliases: ['qadd', 'qa'],
    async execute(message, args) {
        const refMessage = message.reference // message to be quoted has been replied to
        const refMessageId = refMessage.messageId
        const refChannelId = refMessage.channelId
        const refGuildId = refMessage.guildId
        var msgData = {}
        var refData = {}
        const quoteGuild = await client.guilds.fetch(refGuildId)
        const quoteChannel = await quoteGuild.channels.fetch(refChannelId)
        const quoteMessage = await quoteChannel.messages.fetch(refMessageId)

        msgData = {
            content: quoteMessage.content,
            author: await quoteMessage.author,
            reference: await quoteMessage.reference,
            embed: await quoteMessage.embeds ? 'embed' : null
        } // debug
        if (msgData.reference) {
            const quoteRefGuild = await client.guilds.fetch(msgData.reference.guildId)
            const quoteRefChannel = await quoteRefGuild.channels.fetch(msgData.reference.channelId)
            const quoteRefMessage = await quoteRefChannel.messages.fetch(msgData.reference.messageId)


            refData = {
                content: quoteRefMessage.content,
                author: await quoteRefMessage.author,
                embed: await quoteRefMessage.embeds.length > 0 ? 'embed' : null
            }
        }

        const db = mClient.db(process.env.M_DB)
        const quotesColl = db.collection('items_quotes')
        const found = await quotesColl.findOne({ messageID: quoteMessage.id })
        if (found) { return await message.reply({ content: 'Quote Already in Database', flags: MessageFlags.Ephemeral }) }
        await quotesColl.findOneAndUpdate({ messageID: quoteMessage.id }, {
            $set: {
                messageID: quoteMessage.id,
                channelID: quoteChannel.id,
                guildID: quoteGuild.id,
                userID: msgData.author.id,
                count: 0
            }
        }, { upsert: true })

        var description = ''
        if (refData) {
            description += `> ${refData.author}`
            if (refData.embed) {
                description += '*an embed / a command* '
            } else {
                description += refData.content
            }
            description += `\r\n↳`
        }
        const messageLink = `https://discord.com/channels/${quoteGuild.id}/${quoteChannel.id}/${quoteMessage.id}/`
        description += `${msgData.author} ${msgData.content}\r\n\r\n${messageLink}`
        let timestamp = new Date(message.createdTimestamp)
        let footer = timestamp.toDateString().toString()

        const embed = new EmbedBuilder()
            .setThumbnail(msgData.author.displayAvatarURL())
            .setURL(messageLink)
            .setTitle(`- Quote Added! -`)
            .setDescription(description)
            .setFooter({ text: (footer + ' #' + quoteMessage.id) })
        await message.reply({ embeds: [embed] })
    }
}
