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

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

// Escalation (e.g. "3 active warns -> auto-mute for X hours") and warning
// decay/expiry are in the original spec but deferred past this phase — this
// just records the warning and posts it to the mod-log, matching the
// "Suggested Build Order" step 2 scope (core moderation).
module.exports = {
  data: {
    name: 'warn',
    description: 'Log a warning against a member.',
    default_member_permissions: PermissionFlagsBits.ModerateMembers.toString(),
    options: [
      { name: 'user', description: 'Member to warn', type: ApplicationCommandOptionType.User, required: true },
      { name: 'reason', description: 'Reason for the warning', 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 warn yourself.", ephemeral: true })
      return
    }

    await warnings.add({
      guildId: interaction.guildId,
      targetUserId: user.id,
      targetTag: user.tag,
      staffUserId: interaction.user.id,
      staffTag: interaction.user.tag,
      reason,
    })
    await modLog.record({ client: interaction.client, guildId: interaction.guildId, actionType: 'warn', target: user, staffUser: interaction.user, reason })
    await interaction.reply({ content: `Warned ${user.tag}.`, ephemeral: true })
  },
}
