/*
Purge by servers
Acept multiple servers


Purge roleless members




*/ 

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

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


    class purge extends Command{
        constructor(client){
            super(client, {
                name: 'purge',
                group:'admin',
                memberName: 'purge',
                description: 'Kicks everyone that has no roles on the server and is on the server for more than 10 days.',
                needsAdmin:true,
                hidden:false
            })
            this.client = client;
        }
        async run(message, args)
        {
            // Filter the guild members to only include those with 0 roles and a user age of at least 10 days
            // console.log(message.guild.members.cache)
            const now = Date.now();
            await message.guild.members.fetch()
            const members = message.guild.members.cache.filter(
            m => m._roles.length == 0 && now - m.joinedTimestamp >= 10 * 24 * 60 * 60 * 1000
            );
            // Use the for...of loop to iterate over the filtered members
            for (const member of members) 
            {
                // Kick each member with 0 roles and a user age of at least 10 days
                const m = message.guild.members.cache.get(member[0]);
                if (!m) return console.log('Purge: Member not valid')
                m.kick('Optional reason that will be displayed in the audit logs')
                    .then(() => console.log(`Successfully kicked ${m.user.username}`))
                    .catch(err => console.log(`Unable to kick ${member[0]}: ${err}`));
            }
            console.log(`Prune: ${members.size} members with 0 roles and a user age of at least 10 days are going to be kicked.`)
            // Send a confirmation message
            const embed = new EmbedBuilder()
                .setColor(0x0099FF)
                .setTitle('Purging Users with no roles.')
                .setAuthor({name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png",url:'https://rem.wordfights.com/addtodiscord'})
                .setDescription('Kicked Members')
                .addFields(
                    { name: 'Kicked', value:`${members.size} members with 0 roles and a user age of at least 10 days.`  }
                )
                .setTimestamp()
                .setFooter({text:'Rem-chan on ', iconURL:"https://i.imgur.com/g6FSNhL.png"})
                const costumID = require('../../lib').Random();
                const row = new ActionRowBuilder()
                .addComponents(
                    new ButtonBuilder()
                        .setCustomId(costumID)
                        .setLabel('Remove this.')
                        .setStyle(ButtonStyle.Primary),
                );
                const Message = 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();
                });
                collector.on('end', async ()=>
                {
                    Message.edit({ components: [] });
                })
        }
}module.exports = purge;