const { SlashCommandBuilder } = require('@discordjs/builders');
const { parse } = require("csv-parse/sync");
const fs = require('node:fs');
const noblox = require("noblox.js");
const axios = require("axios").default;
const { submissions, commands, cookies } = require("../config/config.js");
const { getAssetInfo, getCurrentUser, SubmissionColumnsString, createSubmissionLine, getSubmissionLine, validateMapAsset, getValidationMessage } = require("../common.js");

async function robloxUserFromDiscord(id) {
    if (isNaN(id)) return undefined;
    try  {
        const res =  await axios.get(`https://api.fiveman1.net/v1/users/${id}`);
        return res.data.result.robloxId;
    } catch {
        return undefined;
    }
}

async function robloxUsernameFromId(id) {
    if (isNaN(id)) return undefined;
    try  {
        const res =  await axios.get(`https://users.roblox.com/v1/users/${id}`);
        return res.data.name;
    } catch {
        return undefined;
    }
}

/**
 * @param {import('discord.js').ChatInputCommandInteraction} interaction
 */
async function execute(interaction) {
    const userId = await robloxUserFromDiscord(interaction.user.id);
    if (!userId) {
        await interaction.editReply("🚫  You don't have a Roblox account linked with your Discord account. Use !link with the rbhop dog bot to link your account.");
        return;
    }

    const game = interaction.options.getString("game", true);
    const fname = submissions[game];
    if (fname === undefined) {
        await interaction.editReply("🚫  Invalid game specified!");
        return;
    }

    if (!fs.existsSync(fname)) {
        fs.writeFileSync(fname, SubmissionColumnsString);
    }

    const id = interaction.options.getInteger("asset_id", true);
    noblox.setCookie(cookies[game], false);

    try {
        // Check that the bot owns this model
        if (!(await noblox.getOwnership(await getCurrentUser(), id, "Asset"))) {
            const msg = `🚫  The ${game} maptest bot's inventory does not contain this asset (id: \`${id}\`). You must use the /take command first.`;
            await interaction.editReply(msg);
            return;
        }
    } catch (error) {
        if (error.message !== "400 The specified Asset does not exist!") {
            throw error;
        }
        await interaction.editReply(`🚫  This asset does not exist (id: \`${id}\`).`);
        return;
    }

    const assetInfo = await getAssetInfo(id);
    if (!assetInfo.isModel) {
        await interaction.editReply(`🚫  This asset (id: \`${id}\`) is not a model. Your map must be a model.`);
        return;
    }

    if (assetInfo.creatorId !== userId) {
        const assetUsernamePromise = robloxUsernameFromId(assetInfo.creatorId);
        const interactionUsernamePromise = robloxUsernameFromId(userId);
        const assetUsername = await assetUsernamePromise;
        const interactionUsername = await interactionUsernamePromise;
        const msg = `🚫  The account linked to your Discord (${interactionUsername}) is not the owner of this model (${assetUsername}), so you cannot submit it.`;
        await interaction.editReply(msg);
        return;
    }

    const csvFile = fs.readFileSync(fname);
    const lines = parse(csvFile, {delimiter: ',', fromLine: 2});

    for (let lineStr of lines) {
        const line = getSubmissionLine(lineStr);
        if (id === line.modelId) {
            await interaction.editReply(`🚫  This map (id: \`${id}\`) was already submitted on <t:${line.timestamp}:d>.`);
            return;
        }
    }

    // Validate and send the validation result
    const validation = await validateMapAsset(id, game);
    const msg = getValidationMessage(validation, game, true);
    await interaction.editReply(msg);

    if (!validation.valid) {
        await interaction.followUp("Due to having problems, your map was **NOT submitted**.");
        return;
    }

    let csvString = SubmissionColumnsString;
    for (let lineStr of lines) {
        const line = getSubmissionLine(lineStr);
        csvString += createSubmissionLine(line.modelId, line.timestamp, line.userId, line.username, line.displayName, line.creator);
    }

    const unixTimestamp = Math.round(+new Date()/1000);
    csvString += createSubmissionLine(id, unixTimestamp, userId, await robloxUsernameFromId(userId), validation.displayName, validation.creator);
    fs.writeFileSync(fname, csvString);

    await interaction.followUp(`Map (id: \`${id}\`) successfully submitted.`);
}

module.exports = {
	data: new SlashCommandBuilder()
		.setName('submit')
		.setDescription('Submit your map')
        .addStringOption(option =>
            option.setName("game")
            .setDescription("Select the maptest game")
            .setRequired(true)
            .addChoices(...commands))
        .addIntegerOption(option =>
            option.setName("asset_id")
            .setDescription("The asset ID of the model")
            .setRequired(true))
        ,
	execute
};