const permissions = require("../../utils/Permissions.json");

module.exports = async (client, member, inviteManager, guildData) => {

  try {

    if (!member.guild.channels.cache.has(inviteManager.channel)) {

      client.logger.log(`Davet kanalÄ± bulunamadÄ±, sunucudaki davet sistemi sÄ±fÄ±rlanÄ±yor... â€¢ ${member.guild.name} (${member.guild.id})`);
      guildData.inviteManager = undefined;
      await guildData.save();

      return member.guild.channels.cache.get(inviteManager.setupChannel)?.send({
        embeds: [
          {
            color: client.settings.embedColors.red,
            title: '**Â»** Davet KanalÄ± BulunamadÄ±ÄŸÄ± Ä°Ã§in Davet Sistemi SÄ±fÄ±rlandÄ±!',
            description: `**â€¢** Tekrar ayarlamak iÃ§in \`/davet-sistemi Ayarla\` komutunu kullanabilirsiniz.`
          }
        ]
      });

    }

    let clientPerms = [];
    ["ViewChannel", "SendMessages", "EmbedLinks"].forEach((perm) => {
      if (!member.guild.channels.cache.get(inviteManager.channel).permissionsFor(member.guild.members.me).has(perm)) {
        clientPerms.push(permissions[perm]);
      }
    });

    if (clientPerms.length > 0) {

      client.logger.log(`Davet kanalÄ±nda bir/birkaÃ§ yetkim bulunmadÄ±ÄŸÄ± iÃ§in davet sistemi sÄ±fÄ±rlanÄ±yor... â€¢ ${member.guild.name} (${member.guild.id})`);
      guildData.inviteManager = undefined;
      await guildData.save();

      return member.guild.channels.cache.get(inviteManager.setupChannel)?.send({
        embeds: [{
          color: client.settings.embedColors.red,
          author: {
            name: `Davet Sistemini Ã‡alÄ±ÅŸtÄ±rabilmem Ä°Ã§in Gereken Ä°zinlere Sahip DeÄŸilim!`,
            icon_url: member.guild.iconURL(),
          },
          description: `**Â»** ${member.guild.channels.cache.get(inviteManager.channel)} kanalÄ±nda yeterli yetkiye sahip olmadÄ±ÄŸÄ±m iÃ§in davet sistemini sÄ±fÄ±rladÄ±m.`,
          fields: [
            {
              name: '**Â»** Ä°htiyacÄ±m Olan Ä°zinler;',
              value: "**â€¢** " + clientPerms.map((p) => `${p}`).join("\n**â€¢** "),
            },
          ]
        }]
      });

    }

    const cachedInvites = client.guildInvites.get(member.guild.id);
    const newInvites = await member.guild.invites.fetch();

    const usedInvite = newInvites.find(inv => cachedInvites?.get(inv.code) < inv.uses);
    //console.log("Cached", [...cachedInvites.keys()])
    //console.log("New", [...newInvites.values()].map(inv => inv.code))
    //console.log("Used", usedInvite)
    //console.log(`The code ${usedInvite.code} was just used by ${member.user.username}.`)

    //DavetÃ§inin davet ettiÄŸi Ã¼yelere bu Ã¼yeyi pushlama 
    if (usedInvite && !inviteManager.invites?.[usedInvite.inviter.id]?.includes(member.id)) {

      guildData.inviteManager.invites ||= {};
      guildData.inviteManager.invites[usedInvite.inviter.id] ||= [];

      guildData.inviteManager.invites[usedInvite.inviter.id].push(member.id);
      guildData.markModified('inviteManager');
      await guildData.save();

    }

    newInvites.each(inv => cachedInvites?.set(inv.code, inv.uses));
    client.guildInvites.set(member.guild.id, cachedInvites);

    let joinEmbed = {
      color: client.settings.embedColors.green,
      //title: `${usedInvite.url}`,
      author: {
        name: `${member.user.tag} KatÄ±ldÄ±!`,
        icon_url: member.user.displayAvatarURL(),
      },
      description: (!usedInvite || member.user.bot)
        ? `ğŸ“© â€¢ Davet eden bulunamadÄ±.`
        : `ğŸ“© â€¢ **${usedInvite.inviter.tag}** tarafÄ±ndan davet edildi. (**${inviteManager.invites?.[usedInvite.inviter.id]?.length || 1}** daveti bulunuyor.)`,
      //.setDescription(`${member.user.tag} is the ${member.guild.memberCount} to join.\nJoined using ${usedInvite.inviter.tag}\nNumber of uses: ${usedInvite.uses}`)
    };

    if (member.user.bot) joinEmbed.author.name += ` (Bot ğŸ¤–)`;

    member.guild.channels.cache.get(inviteManager.channel).send({
      embeds: [joinEmbed]
    });

  } catch (err) { client.logger.error(err); };
};
