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

module.exports = {
  data: new SlashCommandBuilder()
    .setName('setusername')
    .setDescription('Choose between Discord or Minecraft username')
    .addStringOption(option => option
      .setName('type')
      .setDescription('Discord or Minecraft')
      .setRequired(true)
      .addChoices(
        { name: 'Discord', value: 'discord' },
        { name: 'Minecraft', value: 'minecraft' },
      )),

  async execute(interaction) {
    const usernameType = interaction.options.getString('type');

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

      user.useMinecraftUsername = usernameType === 'minecraft' ? true : false;

      await user.save();

      await applyUsername(user, interaction.member);

      await interaction.reply({ embeds: [simpleEmbed('Successfully changed your username type')], ephemeral: true });
    } catch (error) {
      console.error(error);

      await interaction.reply({ embeds: [simpleEmbed('There was an error while changing your username type')], ephemeral: true });
    }
  },
};
