import { Message } from 'eris';
import { randomBytes } from 'crypto';
import { Client, CmdContext, Command } from '../class';

export default class AddMerchant extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'addmerchant';
    this.description = 'Creates a new merchant.';
    this.usage = `${this.client.config.prefix}addmerchant <privileged: 1 for yes | 0 for no> <type: 0 for only soft | 1 for soft and hard> <merchant name>`;
    this.aliases = ['am'];
    this.permissions = 6;
    this.guildOnly = true;
    this.enabled = true;
  }

  public async run(ctx: CmdContext) {
    if (!ctx.args[1]) return this.client.commands.get('help').run(new CmdContext(ctx.message, [this.name]));
    if ((Number(ctx.args[0]) !== 0) && (Number(ctx.args[0]) !== 1)) return this.error(ctx.message.channel, 'Invalid permissions.');
    if ((Number(ctx.args[1]) !== 0) && (Number(ctx.args[1]) !== 1)) return this.error(ctx.message.channel, 'Invalid permissions.');
    const key = randomBytes(20).toString('hex');
    const merchant = await (new this.client.db.mongo.Merchant({
      name: ctx.args.slice(2).join(' '),
      privileged: Number(ctx.args[0]),
      type: Number(ctx.args[1]),
      key,
      pulls: [],
    })).save();
    return this.success(ctx.message.channel, `Created merchant (${merchant._id}). \`${ctx.args.slice(2).join(' ')}\`\n\n\`${key}\``);
  }
}
