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

export default class Score_Pref extends Command {
  constructor(client: Client) {
    super(client);
    this.name = 'pref';
    this.description = 'Locks or unlocks your Community Report.';
    this.usage = `${this.client.config.prefix}score pref <lock | unlock>`;
    this.permissions = 0;
    this.guildOnly = false;
    this.enabled = true;
  }

  public async run(ctx: CmdContext) {
    if (!ctx.message.author) 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, 'Score not calculated yet.');
    if (!score.locked) await this.client.db.mongo.Score.updateOne({ userID: ctx.message.author.id }, { $set: { locked: false } });
    switch (ctx.args[0]) {
      case 'lock':
        await this.client.db.mongo.Score.updateOne({ userID: ctx.message.author.id }, { $set: { locked: true } });
        return this.success(ctx.message.channel, 'Your report is now locked.');
      case 'unlock':
        await this.client.db.mongo.Score.updateOne({ userID: ctx.message.author.id }, { $set: { locked: false } });
        return this.success(ctx.message.channel, 'Your report is now unlocked.');
      default:
        return this.error(ctx.message.channel, 'Invalid input');
    }
  }
}
