const { SlashCommandBuilder, Events, MessageFlags, PermissionFlagsBits } = require('discord.js')
const { mClient } = require('../../../index')
require('dotenv').config()

async function channelSet(interaction) {
    const channel = interaction.options.getChannel('channel')
    const purpose = interaction.options.getString('purpose')
    const db = mClient.db(process.env.M_DB)
    const channelColl = db.collection('config_channels')

    await channelColl.findOneAndUpdate({
        $and: [{ guildID: channel.guild.id }, { purpose: purpose }]
    }, {
        $set: {
            guildID: channel.guild.id,
            purpose: purpose,
            channelID: channel.id
        }
    }, {
        upsert: true
    })

    return await interaction.editReply({
        content: `Set <#${channel.id}> as ${purpose} channel!`, flags: MessageFlags.Ephemeral
    })
}

async function channelSimulate(interaction) {
    const purpose = interaction.options.getString('purpose')
    switch (purpose) {
        case 'birthday':
            await interaction.client.emit('Birthday', interaction.member)
            break;
        case 'wge':
            // For Future WGE Use
            break;
        case 'welcome':
            await interaction.client.emit(Events.GuildMemberAdd, interaction.member)
            break;
        case 'logs':
            // For Future Log Use
            break;
        default:
            break;
    }
    return interaction.reply({ content: 'Done.', flags: MessageFlags.Ephemeral })
}

async function roleSet(interaction) {
    const guild = interaction.guild
    const purpose = interaction.options.getString('purpose')
    const role = interaction.options.getRole('role')

    const db = mClient.db(process.env.M_DB)
    const roleColl = db.collection('config_roles')
    
    switch (purpose) {
        case 'birthday':
            await roleColl.findOneAndUpdate({
                $and: [{ guildID: guild.id }, { purpose: purpose }]
            }, {
                $set: {
                    guildID: guild.id,
                    purpose: purpose,
                    roleID: role.id
                }
            }, {
                upsert: true
            })
            break;
        default:
            break;
    }

    return await interaction.editReply({
        content: `Set ${role} as default role for ${purpose} in ${guild} `
    })
}
module.exports = {
    data: new SlashCommandBuilder()
        .setName('config')
        .setDescription('configurations for admins')
        .setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels)
        .addSubcommandGroup(sg =>
            sg
                .setName('channels')
                .setDescription('configure channels for certain events')
                .addSubcommand(s =>
                    s.setName('set')
                        .setDescription('sets channel')
                        .addStringOption(o =>
                            o.setName('purpose').setDescription('set the purpose').setRequired(true).addChoices({
                                name: 'birthday',
                                value: 'birthday'
                            }, {
                                name: 'welcome',
                                value: 'welcome'
                            }, {
                                name: 'logs',
                                value: 'logs'
                            }, {
                                name: 'wge',
                                value: 'wge'
                            }))
                        .addChannelOption(c =>
                            c.setName('channel').setDescription('choose a channel').setRequired(true)
                        )
                )
                .addSubcommand(s =>
                    s
                        .setName('simulate')
                        .setDescription('simulate a specific channel event')
                        .addStringOption(o =>
                            o.setName('purpose').setDescription('set the purpose').setRequired(true).addChoices({
                                name: 'birthday',
                                value: 'birthday'
                            }, {
                                name: 'welcome',
                                value: 'welcome'
                            }, {
                                name: 'logs',
                                value: 'logs'
                            }, {
                                name: 'wge',
                                value: 'wge'
                            }))
                )
        )
        .addSubcommandGroup(sg =>
            sg.setName('roles')
                .setDescription('configure roles for certain events')
                .addSubcommand(s =>
                    s.setName('set')
                        .setDescription('sets roles')
                        .addStringOption(o =>
                            o.setName('purpose').setDescription('set the purpose').setRequired(true).addChoices({
                                name: 'birthday',
                                value: 'birthday'
                            }))
                        .addRoleOption(c =>
                            c.setName('role').setDescription('choose a role').setRequired(true)
                        )
                )
        ),
    async execute(interaction) {
        switch (interaction.options._group) {
            case 'channels':
                switch (interaction.options._subcommand) {
                    case 'set':
                        await interaction.deferReply()
                        channelSet(interaction)
                        break;
                    case 'simulate':
                        channelSimulate(interaction)
                        break;
                    default:
                        break;
                }
            case 'roles':
                switch (interaction.options._subcommand) {
                    case 'set':
                        await interaction.deferReply()
                        roleSet(interaction)
                        break;
                    default:
                        break;
                }
            default:
                break;
        }
    }
}