import { Client, CmdContext, Command } from '../class';

export default class Profile_Bio extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'bio';
    this.description = 'Updates your bio on your profile.';
    this.usage = `${this.client.config.prefix}profile bio <new bio>`;
    this.permissions = 0;
    this.enabled = true;
  }

  public async run(ctx: CmdContext) {
    if (!await this.client.db.mongo.Member.exists({ userID: ctx.message.author.id })) {
      await this.client.db.mongo.Member.create({ userID: ctx.message.author.id });
    }
    const member = await this.client.db.mongo.Member.findOne({ userID: ctx.message.author.id });

    if (!ctx.args[0]) {
      await member.updateOne({
        additional: {
          ...member.additional,
          bio: null,
        },
      });
      return ctx.message.addReaction('modSuccess:578750988907970567');
    }
    const bio = ctx.args.join(' ');
    if (bio.length >= 256) return this.error(ctx.message.channel, 'Bio too long. It must be less than or equal to 256 characters.');
    await member.updateOne({
      additional: {
        ...member.additional,
        bio,
      },
    });
    return ctx.message.addReaction('modSuccess:578750988907970567');
  }
}
