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

const guildConfig = require('../../model/guildConfig')

module.exports = {
  data: {
    name: 'modlog',
    description: 'View or set the mod-log channel (ban/kick/mute/warn actions post here).',
    // Configuration, not a moderation action — gated to Manage Server rather
    // than the ModerateMembers bit the action commands use.
    default_member_permissions: PermissionFlagsBits.ManageGuild.toString(),
    options: [
      {
        name: 'channel',
        description: 'Channel to post mod-log entries to. Omit to view the current setting.',
        type: ApplicationCommandOptionType.Channel,
        channel_types: [ChannelType.GuildText],
        required: false,
      },
    ],
  },
  async execute(interaction) {
    const channel = interaction.options.getChannel('channel')
    if (!channel) {
      const currentId = await guildConfig.getModLogChannelId(interaction.guildId)
      const content = currentId ? `Mod-log channel is set to <#${currentId}>.` : 'No mod-log channel is set yet.'
      await interaction.reply({ content, ephemeral: true })
      return
    }
    await guildConfig.setModLogChannelId(interaction.guildId, channel.id)
    await interaction.reply({ content: `Mod-log channel set to ${channel}.`, ephemeral: true })
  },
}
