const { PermissionFlagsBits, ApplicationCommandOptionType } = require('discord.js')

const modLog = require('../modLog')

module.exports = {
  data: {
    name: 'ban',
    description: 'Ban a member from the server.',
    default_member_permissions: PermissionFlagsBits.BanMembers.toString(),
    options: [
      { name: 'user', description: 'Member to ban', type: ApplicationCommandOptionType.User, required: true },
      { name: 'reason', description: 'Reason for the ban', type: ApplicationCommandOptionType.String, required: true },
    ],
  },
  async execute(interaction) {
    const user = interaction.options.getUser('user', true)
    const reason = interaction.options.getString('reason', true)

    if (user.id === interaction.user.id) {
      await interaction.reply({ content: "You can't ban yourself.", ephemeral: true })
      return
    }

    const member = interaction.guild.members.cache.get(user.id)
    if (member && !member.bannable) {
      await interaction.reply({ content: "I don't have permission to ban that member (role hierarchy).", ephemeral: true })
      return
    }

    await interaction.guild.members.ban(user, { reason })
    await modLog.record({ client: interaction.client, guildId: interaction.guildId, actionType: 'ban', target: user, staffUser: interaction.user, reason })
    await interaction.reply({ content: `Banned ${user.tag}.`, ephemeral: true })
  },
}
