const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageAttachment } = require("discord.js");
const fs = require('node:fs');
const gameDict = {
    bhop: "files/bhop_submissions.csv",
    surf: "files/surf_submissions.csv",
    deathrun: "files/deathrun_submissions.csv",
};
var commandChoices = [];
for (const [game, file] of Object.entries(gameDict)) {
    commandChoices.push({name: game, value: game})
}

async function execute(interaction) {
    const game = interaction.options.getString("game");

    let fname = gameDict[game];
    if (fname === undefined) {
        await interaction.reply({content: "Invalid game specified!", ephemeral: true});
        return;
    }

    if (!fs.existsSync(fname)) {
        await interaction.reply(`No submissions exist yet for ${game}.`);
        return;
    }

    const csv = fs.readFileSync(fname);
    const file = new MessageAttachment(csv, fname);
    await interaction.reply({files: [file]});
    
}

module.exports = {
	data: new SlashCommandBuilder()
		.setName('submissions')
		.setDescription('View the submissions in .csv format')
        .addStringOption(option =>
            option.setName("game")
            .setDescription("Select the maptest game")
            .setRequired(true)
            .addChoices(commandChoices))
        ,
	execute
};