const {Command, Random} = require('../../lib.js')

const {EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, PermissionFlagsBits} = require('discord.js');


    class createchannel extends Command{
        constructor(client){
            super(client, {
                name: 'createvoicechannel',
                description: 'Creates a voice based channel on the category the command was used.',
                aliases: ['voicechannel'],
                needsAdmin:true,

            })
        }
        
        async run(message, args) {
            // Check admin privilege
          
            // Store the channel name and the parent channel ID in local variables
            const name = args.join(' ');
            const parentId = message.guild.channels.cache.get(message.channelId).parentId;
          
            // Log the creation of a new channel
            console.log(`Creating a new channel on server: ${message.guild.name} with the name: ${name}`);
          
            // Use Promise.all() to create the channel and send the message in parallel
            await Promise.all([
              message.guild.channels.create({
                name,
                type: ChannelType.GuildVoice,
                parent: parentId
              }),
              sendMessage(name)
            ]);
          
            // Define the sendMessage() function using the async/await syntax
            async function sendMessage(name) {
                const costumID = Random();
              const embed = new EmbedBuilder()
                .setColor(0xd31f1f)
                .setTitle('New voice channel created!')
                .setAuthor({
                  name: "Rem-chan",
                  iconURL: "https://i.imgur.com/g6FSNhL.png"
                })
                .setDescription(`${name}`)
                .setFooter({
                  text: 'Rem-chan em ',
                  iconURL: "https://i.imgur.com/g6FSNhL.png"
                })
                .setTimestamp();
          
              const row = new ActionRowBuilder()
                .addComponents(
                  new ButtonBuilder()
                    .setCustomId(costumID)
                    .setLabel('Remove this.')
                    .setStyle(ButtonStyle.Primary)
                );
          
              await message.channel.send({
                ephemeral: true,
                embeds: [embed],
                components: [row]
              });
          
              const filter = i => i.customId === costumID;
          
              const collector = message.channel.createMessageComponentCollector({
                filter,
                time: 60000
              });
          
              collector.on('collect', async m => {
                message.delete();
                m.message.delete();
              });
            }
          }
          
}module.exports = createchannel;