import { Message, TextChannel } from 'eris';
import { Client, Command, RichEmbed } from '../class';

export default class Judgement_Delete extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'delete';
    this.description = 'Deletes a judgement';
    this.usage = `${this.client.config.prefix}judgement delete <judgementID>`;
    this.aliases = ['rm'];
    this.permissions = 5;
    this.guildOnly = true;
    this.enabled = true;
  }

  public async run(message: Message, args: string[]) {
    try {
      if (!args[0]) return this.client.commands.get('help').run(message, ['judgement', 'delete']);

      const member = this.client.util.resolveMember(args[0], this.mainGuild);
      if (!member) return this.error(message.channel, 'Unable to locate member.');
      const staff = await this.client.db.Staff.findOne({ userID: message.author.id }).lean().exec();
      if (!staff) return this.error(message.channel, 'Unable to locate Staff information.');
      const judgement = await this.client.db.Judgement.findOne({ jid: args[0] });
      if (!judgement) return this.error(message.channel, 'Unable to locate judgement.');

      await judgement.delete();

      const log = <TextChannel> this.client.guilds.get(this.client.config.guildID).channels.get('611584771356622849');
      const embed = new RichEmbed();
      embed.setTitle('Judgement - Rescind');
      embed.addField('Member', `${member.username}#${member.discriminator} (<@${member.id}>)`, true);
      embed.addField('Rescinded by', `${message.author.username}, ${staff.pn.slice(0, 2).join(', ')}`, true);
      embed.setFooter(this.client.user.username, this.client.user.avatarURL);
      embed.setTimestamp();
      log.createMessage({ embed });
      return this.success(message.channel, `Judgement \`${args[0]}\` successfully rescinded.`);
    } catch (err) {
      return this.client.util.handleError(err, message, this);
    }
  }
}
