import { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction } from 'discord.js';
import { SlashCommand } from '../../utils/types';
import { context } from '../../config/context';

const command: SlashCommand = {
  guildOnly: true,
  data: new SlashCommandBuilder()
    .setName('timeout')
    .setDescription('Timeout in Minuten setzen.')
    .addUserOption((opt) => opt.setName('user').setDescription('Nutzer').setRequired(true))
    .addIntegerOption((opt) => opt.setName('minutes').setDescription('Dauer').setRequired(true))
    .addStringOption((opt) => opt.setName('reason').setDescription('Grund'))
    .setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers),
  async execute(interaction: ChatInputCommandInteraction) {
    if (!interaction.guild) return;
    const user = interaction.options.getUser('user', true);
    const minutes = interaction.options.getInteger('minutes', true);
    const reason = interaction.options.getString('reason') ?? 'Kein Grund angegeben';
    const member = await interaction.guild.members.fetch(user.id).catch(() => null);
    if (!member) {
      await interaction.reply({ content: 'Mitglied nicht gefunden.', ephemeral: true });
      return;
    }
    await member.timeout(minutes * 60 * 1000, reason).catch(() => null);
    await interaction.reply({ content: `${user.tag} wurde für ${minutes} Minuten in Timeout gesetzt.` });
    context.logging.logAction(user, 'Timeout', reason);
  }
};

export default command;
