const { SlashCommandBuilder } = require('discord.js');
const { simpleEmbed } = require('../functions/embeds.js');
const { Minecraft, Users } = require('../functions/models.js');
const { applyUsername } = require('../functions/utils.js');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('removewhitelist')
    .setDescription('Remove yourself from the whitelist'),

  async execute(interaction) {
    try {
      const user = await Users.findOne({ where: { id: interaction.user.id } });

      if (!user.minecraftUuid) return await interaction.reply({ embeds: [simpleEmbed('You are not whitelisted')], ephemeral: true });

      await Minecraft.destroy({ where: { uuid: user.minecraftUuid } });

      await applyUsername(user, interaction.member);

      const role = await interaction.guild.roles.fetch(process.env.MINECRAFT_ROLE_ID);
      await interaction.member.roles.remove(role);

      await interaction.reply({ embeds: [simpleEmbed('Successfully removed you from the whitelist')], ephemeral: true });
    } catch (error) {
      console.error(error);

      await interaction.reply({ embeds: [simpleEmbed('There was an error while removing you from the whitelist')], ephemeral: true });
    }
  },
};
