import { Message } from 'eris';
import { apply as Apply } from '.';
import { Client, Command } from '../class';

export default class SSS_Password_Reset extends Command {
  public applyCommand: Apply;

  constructor(client: Client) {
    super(client);
    this.name = 'pw';
    this.description = 'Sends a password reset link to your email address.';
    this.usage = `${this.client.config.prefix}staff-self-serv pw`;
    this.permissions = 1;
    this.guildOnly = false;
    this.enabled = true;
  }

  public async run(message: Message) {
    try {
      const staff = await this.client.db.Staff.findOne({ userID: message.author.id }).lean().exec();
      if (!staff) return this.error(message.channel, 'Staff information not located.');

      const passwordTicket = await this.client.util.authClient.createPasswordChangeTicket({
        email: staff.emailAddress,
        connection_id: 'con_T3ELEx2reigKMSlP',
      });
      const channel = await this.client.getDMChannel(message.author.id);
      channel.createMessage(`__**Library of Code sp-us | Identity & Account Management**__\n\nPlease click the link below to reset your password.\n\n${passwordTicket.ticket}`).catch(() => this.error(message.channel, 'Unable to send you a DM.'));
      return message.addReaction('modSuccess:578750988907970567');
    } catch (err) {
      return this.client.util.handleError(err, message, this);
    }
  }
}
