import { SlashCommandBuilder } from "discord.js";

const command = {
  data: new SlashCommandBuilder()
    .setName("maketeam")
    .setDescription("Crée des equipes au hasard")
    .addIntegerOption((option) =>
      option
        .setName("number")
        .setDescription("Nombre d'équipe")
        .setRequired(true)
    )
    .addStringOption((option) =>
      option
        .setName("players")
        .setDescription("Liste des joueurs, séparés par un espace")
        .setRequired(true)
    ),
  async execute(interaction) {
    const teamNumber = interaction.options.getInteger("number");
    const players = interaction.options.getString("players");
    const playersArray = players.split(" ");

    const teams = Array.from({ length: teamNumber }, () => []);
    const shuffledPlayers = [...playersArray].sort(() => Math.random() - 0.5);

    shuffledPlayers.forEach((player, index) => {
      const teamIndex = index % teamNumber;
      teams[teamIndex].push(player);
    });

    let message = "";
    let teamList = "";

    teams.forEach((team, index) => {
      teamList = `${teamList}\n${index + 1}. ${team.join(", ")}`;
    });

    message = `Yop ! Voici les équipes proposés :\n${teamList}`;
    await interaction.reply(message);
  },
};

export { command };
