const { SlashCommandBuilder } = require('discord.js');
const axios = require('axios');
const programMessage = require("./programMessage.json")

const getDownloadCount = async () => {
    try {
        const response = await axios.get('https://api.github.com/repos/LouisMazin/Palworld_Breeding_Tree/releases', {
            headers: {
                'Accept': 'application/vnd.github.v3+json',
                'User-Agent': 'Palworld-Discord-Bot'
            }
        });
        
        let totalDownloads = 0;
        
        for (const asset of response.data[0].assets) {
			totalDownloads += asset.download_count;
		}
        
        return totalDownloads;
    } catch (error) {
        console.log('Erreur lors de la récupération du nombre de téléchargements:', error.message);
        return null;
    }
};

module.exports = {
	data: new SlashCommandBuilder()
		.setName('pbt')
		.setDescription('Afichez les informations sur le programme de Louis !')
		.addUserOption(option =>
			option.setName('utilisateur')
				.setDescription('Utilisateur à mentionner')
				.setRequired(false)),
	async execute(interaction) {
		const user = interaction.options.getUser('utilisateur');
		const downloadCount = await getDownloadCount();
		
		// Clone the message to avoid modifying the original
		const message = JSON.parse(JSON.stringify(programMessage));
		// Add download count to the embed description
		if (downloadCount !== null) {
			const actual = downloadCount.toString();
			let next = (downloadCount + 1).toString();
			const lastNumber = actual[actual.length - 1];
			next += next=='1' ? "ère" : "ème";
			message.embeds[0].description = message.embeds[0].description.replace("X", actual).replace("Y", next);
		}

		message.content = user ? "||<@"+user.id+">||" : "";
		await interaction.reply(message);
	},
};