const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getServerMode, getEmbedColor } = require('../utils/modeHelper');

const RESPONSES = [
  { text: 'It is certain.', positive: true },
  { text: 'It is decidedly so.', positive: true },
  { text: 'Without a doubt.', positive: true },
  { text: 'Yes, definitely.', positive: true },
  { text: 'You may rely on it.', positive: true },
  { text: 'As I see it, yes.', positive: true },
  { text: 'Most likely.', positive: true },
  { text: 'Outlook good.', positive: true },
  { text: 'Yes.', positive: true },
  { text: 'Signs point to yes.', positive: true },
  { text: 'Reply hazy, try again.', positive: null },
  { text: 'Ask again later.', positive: null },
  { text: 'Better not tell you now.', positive: null },
  { text: 'Cannot predict now.', positive: null },
  { text: 'Concentrate and ask again.', positive: null },
  { text: "Don't count on it.", positive: false },
  { text: 'My reply is no.', positive: false },
  { text: 'My sources say no.', positive: false },
  { text: 'Outlook not so good.', positive: false },
  { text: 'Very doubtful.', positive: false },
];

module.exports = {
  data: new SlashCommandBuilder()
    .setName('8ball')
    .setDescription('Ask the magic 8-ball a question')
    .addStringOption(option =>
      option.setName('question')
        .setDescription('Your question for the 8-ball')
        .setRequired(true)
        .setMaxLength(256)
    ),

  async execute(interaction, supabase, client) {
    const question = interaction.options.getString('question');
    const response = RESPONSES[Math.floor(Math.random() * RESPONSES.length)];
    
    const mode = await getServerMode(supabase, interaction.guildId);
    
    let color;
    if (response.positive === true) color = 0x22C55E;
    else if (response.positive === false) color = 0xEF4444;
    else color = 0xF59E0B;

    const embed = new EmbedBuilder()
      .setColor(color)
      .setTitle('🎱 Magic 8-Ball')
      .addFields(
        { name: '❓ Question', value: question },
        { name: '🔮 Answer', value: response.text }
      )
      .setFooter({ text: `Asked by ${interaction.user.username}` })
      .setTimestamp();

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