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

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

  constructor(client: Client) {
    super(client);
    this.name = 'ca';
    this.description = 'Creates a new IAM Account. Most likely, one was already created for you.';
    this.usage = `${this.client.config.prefix}staff-self-serv ca`;
    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.');


      if (message.member.roles.includes('662163685439045632')) { // Board of Directors
        await this.client.util.authClient.createUser({
          email: staff.emailAddress,
          name: message.author.username,
          user_id: message.author.id,
          connection: 'Staff-Database',
          email_verified: true,
          app_metadata: {
            boardOfDirectors: true,
          },
          picture: message.author.avatarURL,
          password: this.client.config.defaultAccountPassword,
        });
      } else if (message.member.roles.includes('701454855952138300')) { // Supervisor
        await this.client.util.authClient.createUser({
          email: staff.emailAddress,
          name: message.author.username,
          user_id: message.author.id,
          connection: 'Staff-Database',
          email_verified: true,
          app_metadata: {
            supervisor: true,
          },
          picture: message.author.avatarURL,
          password: this.client.config.defaultAccountPassword,
        });
      } else if (message.member.roles.includes('701454780828221450')) { // Technician
        await this.client.util.authClient.createUser({
          email: staff.emailAddress,
          name: message.author.username,
          user_id: message.author.id,
          connection: 'Staff-Database',
          email_verified: true,
          app_metadata: {
            technician: true,
          },
          picture: message.author.avatarURL,
          password: this.client.config.defaultAccountPassword,
        });
      } else if (message.member.roles.includes('455972169449734144')) { // Moderator
        await this.client.util.authClient.createUser({
          email: staff.emailAddress,
          name: message.author.username,
          user_id: message.author.id,
          connection: 'Staff-Database',
          email_verified: true,
          app_metadata: {
            moderator: true,
          },
          picture: message.author.avatarURL,
          password: this.client.config.defaultAccountPassword,
        });
      } else if (message.member.roles.includes('453689940140883988')) { // Core Team
        await this.client.util.authClient.createUser({
          email: staff.emailAddress,
          name: message.author.username,
          user_id: message.author.id,
          connection: 'Staff-Database',
          email_verified: true,
          app_metadata: {
            coreTeam: true,
          },
          picture: message.author.avatarURL,
          password: this.client.config.defaultAccountPassword,
        });
      } else if (message.member.roles.includes('701481967149121627')) { // Associates
        await this.client.util.authClient.createUser({
          email: staff.emailAddress,
          name: message.author.username,
          user_id: message.author.id,
          connection: 'Staff-Database',
          email_verified: true,
          app_metadata: {
            associate: true,
          },
          picture: message.author.avatarURL,
          password: this.client.config.defaultAccountPassword,
        });
      }

      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);
    }
  }
}
