const express = require('express');
const index = require('../index');
const { PermissionsBitField, ChannelType } = require('discord.js');

const router = express.Router();

router.post('/createchannels', async (req, res) => {
  const { name, discordId } = req.body;

  if (!name || !discordId) return res.status(400).send({ error: 'Name en discordId zijn vereist' });

  try {
    const guild = await index.client.guilds.fetch(process.env.GUILD_ID);

    const category = await guild.channels.fetch(process.env.TEAM_CATEGORY_ID);

    const member = await guild.members.fetch(discordId);

    const textChannel = await guild.channels.create({
      name: name,
      type: ChannelType.GuildText,
      parent: category,
      permissionOverwrites: [
        {
          id: guild.id,
          deny: [PermissionsBitField.Flags.ViewChannel],
        },
        {
          id: member.id,
          allow: [PermissionsBitField.Flags.ViewChannel],
        },
      ],
    });

    const voiceChannel = await guild.channels.create({
      name: name,
      type: ChannelType.GuildVoice,
      parent: category,
      permissionOverwrites: [
        {
          id: guild.id,
          deny: [PermissionsBitField.Flags.ViewChannel],
        },
        {
          id: member.id,
          allow: [PermissionsBitField.Flags.ViewChannel],
        },
      ],
    });

    res.send({ textChannel, voiceChannel });
  } catch (e) {
    console.log(e);
    return res.status(500).send({ error: 'Error tijdens het maken van discord channels' });
  }
});


router.post('/deletechannels', async (req, res) => {
  const { textChannelId, voiceChannelId } = req.body;

  if (!textChannelId, !voiceChannelId) return res.status(400).send({ error: 'textChannelId en voiceChannelId zijn vereist' });

  try {
    const guild = await index.client.guilds.fetch(process.env.GUILD_ID);

    const textChannel = await guild.channels.fetch(textChannelId);
    const voiceChannel = await guild.channels.fetch(voiceChannelId);

    await textChannel.delete();
    await voiceChannel.delete();

    res.send({ status: 'success' });
  } catch (e) {
    console.log(e);
    return res.status(500).send({ error: 'Error tijdens het verwijderen van discord channels' });
  }
});

router.post('/removeteammember', async (req, res) => {
  const { textChannelId, voiceChannelId, discordId } = req.body;

  if (!textChannelId, !voiceChannelId, !discordId) return res.status(400).send({ error: 'textChannelId, voiceChannelId en discordId zijn vereist' });

  try {
    const guild = await index.client.guilds.fetch(process.env.GUILD_ID);
    const member = await guild.members.fetch(discordId);

    const textChannel = await guild.channels.fetch(textChannelId);
    const voiceChannel = await guild.channels.fetch(voiceChannelId);

    await textChannel.permissionOverwrites.delete(member);
    await voiceChannel.permissionOverwrites.delete(member);

    res.send({ status: 'success' });
  } catch (e) {
    console.log(e);
    return res.status(500).send({ error: 'Error tijdens het verwijderen van een team member' });
  }
});

router.post('/addteammember', async (req, res) => {
  const { textChannelId, voiceChannelId, discordId } = req.body;

  if (!textChannelId, !voiceChannelId, !discordId) return res.status(400).send({ error: 'textChannelId, voiceChannelId en discordId zijn vereist' });

  try {
    const guild = await index.client.guilds.fetch(process.env.GUILD_ID);
    const member = await guild.members.fetch(discordId);

    const textChannel = await guild.channels.fetch(textChannelId);
    const voiceChannel = await guild.channels.fetch(voiceChannelId);

    await textChannel.permissionOverwrites.edit(member, { ViewChannel: true });
    await voiceChannel.permissionOverwrites.edit(member, { ViewChannel: true });

    res.send({ status: 'success' });
  } catch (e) {
    console.log(e);
    return res.status(500).send({ error: 'Error tijdens het toevoegen van team member' });
  }
});

router.post('/edit', async (req, res) => {
  const { textChannelId, voiceChannelId, name } = req.body;

  if (!textChannelId, !voiceChannelId, !name) return res.status(400).send({ error: 'textChannelId, voiceChannelId en name zijn vereist' });

  try {
    const guild = await index.client.guilds.fetch(process.env.GUILD_ID);

    const textChannel = await guild.channels.fetch(textChannelId);
    const voiceChannel = await guild.channels.fetch(voiceChannelId);

    await textChannel.edit({ name: name });
    await voiceChannel.edit({ name: name });

    res.send({ status: 'success' });
  } catch (e) {
    console.log(e);
    return res.status(500).send({ error: 'Error tijds het veranderen van Discord channel naam' });
  }
});

module.exports = router;
