import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, ChannelType } from 'discord.js';
import { audit } from '../core/db.js';

export default {
  data: new SlashCommandBuilder()
    .setName('announcement')
    .setDescription('Kirim pengumuman @everyone ke channel')
    .setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
    .addChannelOption(o => o
      .setName('channel')
      .setDescription('Channel tujuan')
      .addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement)
      .setRequired(true))
    .addStringOption(o => o
      .setName('message')
      .setDescription('Isi pengumuman')
      .setRequired(true))
    .addStringOption(o => o
      .setName('title')
      .setDescription('Judul embed (opsional)'))
    .addStringOption(o => o
      .setName('color')
      .setDescription('Warna embed: red, green, blue, yellow, purple (default: blue)'))
    .addStringOption(o => o
      .setName('image_url')
      .setDescription('URL gambar/banner (opsional)'))
    .addBooleanOption(o => o
      .setName('mention_everyone')
      .setDescription('Mention @everyone? (default: ya)')),

  async execute(interaction) {
    const channel = interaction.options.getChannel('channel');
    const message = interaction.options.getString('message');
    const title = interaction.options.getString('title');
    const colorName = interaction.options.getString('color') || 'blue';
    const imageUrl = interaction.options.getString('image_url');
    const mentionEveryone = interaction.options.getBoolean('mention_everyone') ?? true;

    // Validation
    if (message.length > 4000) {
      return interaction.reply({ content: '❌ Pesan terlalu panjang (max 4000 karakter)', ephemeral: true });
    }

    // Color mapping
    const colors = {
      red: 0xef4444,
      green: 0x10b981,
      blue: 0x5865f2,
      yellow: 0xfbbf24,
      purple: 0xa855f7,
      orange: 0xf97316,
    };
    const color = colors[colorName] || colors.blue;

    await interaction.deferReply({ ephemeral: true });

    try {
      const embed = new EmbedBuilder()
        .setColor(color)
        .setDescription(message)
        .setTimestamp()
        .setFooter({ text: `Dikirim oleh ${interaction.user.tag}` });

      if (title) embed.setTitle(title);
      if (imageUrl) embed.setImage(imageUrl);

      const content = mentionEveryone ? '@everyone' : '';
      const msg = await channel.send({ content, embeds: [embed] });

      audit(interaction.guildId, interaction.user.id, 'announcement:send', {
        channel_id: channel.id,
        message_id: msg.id,
        mention_everyone: mentionEveryone,
      });

      await interaction.editReply({
        embeds: [new EmbedBuilder()
          .setTitle('✅ Pengumuman Terkirim')
          .setColor(0x10b981)
          .addFields(
            { name: 'Channel', value: `${channel}`, inline: true },
            { name: 'Message ID', value: `\`${msg.id}\``, inline: true },
            { name: '@everyone', value: mentionEveryone ? 'Ya' : 'Tidak', inline: true },
          )
          .setDescription(`[Lihat pesan](${msg.url})`)],
      });
    } catch (e) {
      await interaction.editReply({ content: `❌ Gagal kirim: ${e.message}` });
    }
  },
};
