import {
  ActionRowBuilder,
  ModalBuilder,
  SlashCommandBuilder,
  TextInputBuilder,
  TextInputStyle,
  type APIApplicationCommandOptionChoice,
  type ModalActionRowComponentBuilder,
} from "discord.js";
import type { Command } from "../command";
import { personalities } from "../../client";

const createCommand = (): SlashCommandBuilder => {
  const builder = new SlashCommandBuilder()
    .setName("prompt")
    .setDescription("Get a prompt from the AI.")
    .addStringOption((option) =>
      option
        .setName("prompt")
        .setDescription("The prompt to use.")
        .addChoices(
          ...personalities.map(
            (personality) =>
              ({
                name: personality.name,
                value: personality.name,
              } as APIApplicationCommandOptionChoice<string>)
          )
        )
        .setRequired(false)
    ) as SlashCommandBuilder;

  return builder;
};

const command: Command = {
  data: createCommand(),
  execute: async (interaction) => {
    const prompt = interaction.options.getString("prompt") || "regular";
    const personality = personalities.find(
      (personality) => personality.name === prompt
    );

    if (!personality) {
      await interaction.reply({
        content: "Invalid personality",
        ephemeral: true,
      });
      return;
    }

    const chanceInput = new TextInputBuilder()
      .setCustomId("chance")
      .setLabel("Chance to use this prompt (0-1)")
      .setValue(personality?.chance.toString())
      .setRequired(true)
      .setStyle(TextInputStyle.Short);

    const chanceRow =
      new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(
        chanceInput
      );

    await interaction.showModal(
      new ModalBuilder()
        .setTitle(`Prompt: ${prompt}`)
        .setCustomId("prompt")
        .addComponents(chanceRow)
        
    );
  },
};

export default command;
