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

export default class Score_Notify extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'notify';
    this.description = 'Edits your notification preferences for Hard Inquiries.';
    this.usage = `${this.client.config.prefix}score notify <on | off>`;
    this.permissions = 0;
    this.guildOnly = false;
    this.enabled = true;
  }

  public async run(ctx: CmdContext) {
    const user = ctx.message.author;
    if (!user) return this.error(ctx.message.channel, 'Member not found.');
    const score = await this.client.db.mongo.Score.findOne({ userID: ctx.message.author.id });
    if (!score) return this.error(ctx.message.channel, 'Your Community Report has not been generated yet. Check back a little later.');
    if (!score.notify) await this.client.db.mongo.Score.updateOne({ userID: ctx.message.author.id }, { $set: { notify: false } });
    switch (ctx.args[0]) {
      case 'on':
        await this.client.db.mongo.Score.updateOne({ userID: ctx.message.author.id }, { $set: { notify: true } });
        return this.success(ctx.message.channel, 'You will now be sent notifications whenever your score is hard-pulled.');
      case 'off':
        await this.client.db.mongo.Score.updateOne({ userID: ctx.message.author.id }, { $set: { notify: false } });
        return this.success(ctx.message.channel, 'You will no longer be sent notifications when your score is hard-pulled.');
      default:
        return this.error(ctx.message.channel, 'Invalid option. Valid options are `on` and `off`.');
    }
  }
}
