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

module.exports = {
  data: new SlashCommandBuilder()
    .setName('timeout')
    .setDescription('Timeout a user')
    .setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers)
    .addUserOption(option =>
      option.setName('user')
        .setDescription('User to timeout')
        .setRequired(true)
    )
    .addIntegerOption(option =>
      option.setName('duration')
        .setDescription('Duration in minutes')
        .setRequired(true)
        .setMinValue(1)
        .setMaxValue(40320)
    )
    .addStringOption(option =>
      option.setName('reason')
        .setDescription('Reason for timeout')
        .setRequired(false)
        .setMaxLength(500)
    ),

  async execute(interaction, supabase, client) {
    const target = interaction.options.getUser('user');
    const duration = interaction.options.getInteger('duration');
    const reason = interaction.options.getString('reason') || 'No reason provided';
    const moderator = interaction.user;

    const member = await interaction.guild.members.fetch(target.id).catch(() => null);

    if (!member) {
      return interaction.reply({ content: 'User not found in this server.', ephemeral: true });
    }

    if (!member.moderatable) {
      return interaction.reply({ content: 'I cannot timeout this user. They may have higher permissions.', ephemeral: true });
    }

    if (target.id === interaction.user.id) {
      return interaction.reply({ content: 'You cannot timeout yourself.', ephemeral: true });
    }

    const durationMs = duration * 60 * 1000;
    const endsAt = new Date(Date.now() + durationMs);

    try {
      await member.timeout(durationMs, `${reason} | By ${moderator.tag}`);

      if (supabase) {
        try {
          await supabase.from('mod_actions').insert({
            guild_id: interaction.guildId,
            action: 'timeout',
            user_id: target.id,
            user_tag: target.tag,
            moderator_id: moderator.id,
            moderator_tag: moderator.tag,
            reason: reason,
            duration_minutes: duration,
          });
        } catch (e) {
          console.warn('Failed to log timeout:', e.message);
        }
      }

      const embed = new EmbedBuilder()
        .setColor(0xfbbf24)
        .setTitle('User Timed Out')
        .setThumbnail(target.displayAvatarURL())
        .addFields(
          { name: 'User', value: `${target.tag} (${target.id})`, inline: true },
          { name: 'Moderator', value: moderator.tag, inline: true },
          { name: 'Duration', value: `${duration} minutes`, inline: true },
          { name: 'Ends', value: `<t:${Math.floor(endsAt.getTime() / 1000)}:R>`, inline: true },
          { name: 'Reason', value: reason }
        )
        .setTimestamp();

      await interaction.reply({ embeds: [embed] });

      try {
        await target.send({
          embeds: [
            new EmbedBuilder()
              .setColor(0xfbbf24)
              .setTitle(`Timed out in ${interaction.guild.name}`)
              .addFields(
                { name: 'Duration', value: `${duration} minutes` },
                { name: 'Reason', value: reason }
              )
              .setTimestamp()
          ]
        });
      } catch (e) {}

    } catch (error) {
      console.error('Timeout error:', error);
      await interaction.reply({ content: 'Failed to timeout user.', ephemeral: true });
    }
  },
};
