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

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

module.exports = {
  data: {
    name: 'warnings',
    description: "List a member's active warnings.",
    default_member_permissions: PermissionFlagsBits.ModerateMembers.toString(),
    options: [
      { name: 'user', description: 'Member to look up', type: ApplicationCommandOptionType.User, required: true },
    ],
  },
  async execute(interaction) {
    const user = interaction.options.getUser('user', true)
    const rows = await warnings.listActive(interaction.guildId, user.id)

    if (rows.length === 0) {
      await interaction.reply({ content: `${user.tag} has no active warnings.`, ephemeral: true })
      return
    }

    const embed = new EmbedBuilder()
      .setColor(0xe0b070)
      .setTitle(`Warnings — ${user.tag}`)
      .setDescription(
        rows
          .map((w, i) => `**${i + 1}.** ${w.reason || '(no reason given)'} — by ${w.staff_tag || 'unknown'} on ${new Date(w.created_at).toLocaleDateString()}`)
          .join('\n'),
      )

    await interaction.reply({ embeds: [embed], ephemeral: true })
  },
}
