const Command = require('../../structures/Command')
const GUILD = require('../../database/schemas/Guild')

module.exports = class extends Command {
  constructor(...args) {
    super(...args, {
      name: "starboard",
      userPermission: ["MANAGE_GUILD"],
      subCmds: ["channelset", "minimumstars"],
      usage: ["<enable | disable>"],
      examples: ["enable", "channelset #starboard", "minimumstars 3"],
      argsLength: 1,
    })
  }
  async run(message, args, client) {
    const Guild = await GUILD.findOne({ guildId: message.guild.id })
    if (!Guild) return message.channel.send(`${client.emoji.fail} | This guild doesn't exist in our database please try again.`)
    const language = require(`../../data/language/${Guild.language}.json`)

    

    if (args[0].toLowerCase() === "enable") {
      if (Guild.starboard.toggle === "true") return message.channel.send(`${client.emoji.fail} | Starboard is already enabled for this guild.`)
      Guild.starboard.toggle = "true"
      Guild.save()
        .then(() => message.channel.send(`${client.emoji.check} | Successfully enabled starboard`))
        .catch(() => message.channel.send(`${client.emoji.fail} | I couldn't enable starboard please try again.`))
    } else if (args[0].toLowerCase() === "disable") {
      if (Guild.starboard.toggle === "false") return message.channel.send(`${client.emoji.fail} | Starboard is already disabled for this guild.`)
      Guild.starboard.toggle = "false"
      Guild.save()
        .then(() => message.channel.send(`${client.emoji.check} | Successfully disabled starboard`))
        .catch(() => message.channel.send(`${client.emoji.fail} | I couldn't disable please try again.`))
    } else if (args[0].toLowerCase() === "channelset"){
      if(!args[1]) return message.channel.send(`${client.emoji.fail} | ${language.mentionAChannel}.`)
      const channel = message.guild.channels.cache.find(chan => (chan.toString() === args[1] || chan.id === args[1]))
      if(!channel) return message.channel.send(`${client.emoji.fail} | Please mention a valid channel.`)
      Guild.starboard.channel = channel.id
      Guild.save()
      .then(() => message.channel.send(`${client.emoji.check} | Successfully set the starboard channel to ${channel.toString()}.`))
      .catch(() => message.channel.send(`${client.emoji.fail} | I couldn't set the starboard channel please try again.`))
    } else if (args[0].toLowerCase() === "minimumstars"){
      if(!args[1] || !Number(args[1]) || Number(args[1]) <= 0) return message.channel.send(`${client.emoji.fail} | Please enter a valid amount.`)
      Guild.starboard.minStars = Math.floor(Number(args[1]))
      Guild.save()
      .then(() => message.channel.send(`${client.emoji.check} | Successfully set minimum stars to ${Math.floor(Number(args[1]))}.`))
      .catch(() => message.channel.send(`${client.emoji.fail} | I couldn't set the minimum stars please try again.`))
    }
  }
}