const { ApplicationCommandOptionType, ActionRowBuilder, PermissionsBitField, StringSelectMenuBuilder, EmbedBuilder } = require("discord.js");
const ertum = require("../../../../../../Global/Settings/Setup.json");

module.exports = {
    name: "toplantÄ±",
    description: "ToplantÄ± baÅŸlatÄ±rsÄ±nÄ±z.",
    category: "ADMIN",
    cooldown: 0,
    command: {
      enabled: true,
      aliases: ["toplanti","toplantÄ±-baÅŸlat","toplantbaÅŸlat"],
      usage: ".toplantÄ±", 
    },

    onLoad: function (client) { },

    onCommand: async function (client, message, args, ertuembed) {

    if (!ertum.OwnerRoles.some(ertucum => message.member.roles.cache.has(ertucum)) && !message.member.permissions.has(PermissionsBitField.Flags.Administrator)) return  message.reply({ content: `Yetkin bulunmamakta dostum.` }).then((e) => setTimeout(() => { e.delete(); }, 5000));
    if (!message.member.voice.channel || message.member.voice.channel.id != ertum.MeetingChannel) return message.reply({content:"Bu komutu baÅŸlatabilmek iÃ§in toplantÄ± kanalÄ±nda olmalÄ±sÄ±n."});

    const row = new ActionRowBuilder()
    .addComponents(
        new StringSelectMenuBuilder()
          .setCustomId('toplanti')
          .setPlaceholder(`ToplantÄ± baÅŸlatmak iÃ§in menÃ¼leri kullanÄ±n!`)
          .addOptions([
            { label: 'ToplantÄ± BaÅŸlat', description: `ToplantÄ±yÄ± BulunduÄŸunuz Ses KanalÄ±nda BaÅŸlatÄ±r Ve Rol DaÄŸÄ±tÄ±r!`, value: 'toplantibaslat', emoji: 'ğŸŸ¢' },
            { label: 'ToplantÄ± Duyuru', description: `Yetkilileri DM Ãœzerinden Ses KanalÄ±na Davet Eder!`, value: 'toplantiduyuru', emoji: 'ğŸ“£' },
          ]),
      );

      let msg = await message.reply({ components: [row], content: `AÅŸaÄŸÄ±daki menÃ¼den toplantÄ± baÅŸlatÄ±p veya yetkililere DM Ã¼zerinden mesaj gÃ¶nderebilirsiniz.`})
      const collector = msg.createMessageComponentCollector({ filter: i => i.user.id === message.member.id, time: 30000, max: 1 });

      collector.on('collect', async (interaction) => {
    if (!interaction.isStringSelectMenu()) return;
    let value = interaction.values[0];
    switch (value) {
        case "toplantibaslat":
            let voiceuser = message.guild.members.cache.filter(member => member.roles.highest.position >= ertum.ConfirmerRoles[0].position && member.voice.channel && !member.user.bot)
            let nvoiceuser = message.guild.members.cache.filter(member => member.roles.highest.position >= ertum.ConfirmerRoles[0].position && !member.voice.channel && !member.user.bot)
            let mazeret = message.guild.roles.cache.get(ertum.MazeretRole).members.size;
            interaction.reply({ content: `${interaction.member}`, embeds: [ertuembed.setDescription(`**KatÄ±ldÄ± RolÃ¼ Verilecek SayÄ±sÄ± ${voiceuser.size}**\n**KatÄ±ldÄ± RolÃ¼ AlÄ±nacak SayÄ±sÄ± ${nvoiceuser.size}**\n> **Mazeretli KiÅŸi SayÄ±sÄ± ${mazeret}**\n\n**ToplantÄ±da Olan ${voiceuser.size} KiÅŸiye KatÄ±ldÄ± RolÃ¼ Veriliyor..**`)] })
            interaction.message.delete();
            voiceuser.array().forEach((ertu, index) => {
                setTimeout(async () => {
                    ertu.roles.add(ertum.JoinedRole)
                }, index * 1000)
            })
    break;
    case "toplantiduyuru":
        let nnvoiceuser = interaction.guild.members.cache.filter(member => member.roles.highest.position >= ertum.ConfirmerRoles[0].position && !member.voice.channel && !member.user.bot)
     if(nnvoiceuser.length == 0)return interaction.reply({ embeds: [ertuembed.setDescription(`Sunucudaki TÃ¼m Yetkililer Seste Bulunuyor!`)] })
     let mesaj = await interaction.reply({ embeds: [ertuembed.setDescription(`Seste Olmayan ${nnvoiceuser.size} KiÅŸiye DM Ãœzerinden Duyuru GeÃ§iliyor! LÃ¼tfen Biraz Bekleyiniz.`)] });
     interaction.message.delete();
     nnvoiceuser.forEach((ertu, index) => {
        setTimeout(() => {
         ertu.send(`Yetkili OlduÄŸun \`${interaction.guild.name}\` Sunucusunda ToplantÄ± BaÅŸlÄ±yor! ToplantÄ±da BulunmadÄ±ÄŸÄ±n Ä°Ã§in Sana Bu MesajÄ± GÃ¶nderiyorum, EÄŸer ToplantÄ±ya KatÄ±lmazsan UyarÄ± AlÄ±caksÄ±n!`).then(ertucanpatlican => mesaj.edit(`> **${ertu} KiÅŸisine DM Ãœzerinden Duyuru YapÄ±ldÄ±!**`).catch((err) => { interaction.channel.send(`${yetkili} Yetkili OlduÄŸun \`${interaction.guild.name}\` Sunucusunda ToplantÄ± BaÅŸlÄ±yor, ToplantÄ±da BulunmadÄ±ÄŸÄ±n Ä°Ã§in Sana Bu MesajÄ± GÃ¶nderiyorum, EÄŸer ToplantÄ±ya KatÄ±lmazsan UyarÄ± AlÄ±caksÄ±n!`).then(x => mesaj.edit({embeds:[ertuembed.setDescription(`${yetkili} KiÅŸisinin DM'i KapalÄ± OlduÄŸundan Kanalda Duyuru YapÄ±ldÄ±!`)]}))}));
        }, index*5000);
        })
break;
}

})
     },

  };