const { SlashCommandBuilder } = require('@discordjs/builders');
const help = require('../helpFunctions.js');
const gifAmount = 50;
require('dotenv').config();

module.exports = {
	data: new SlashCommandBuilder()
		.setName('gif')
		.setDescription('Sends gif')
        .addStringOption(option =>
            option.setName('what')
            .setDescription('What should I search for? (If this is empty I will give you something random!)'))
        .addBooleanOption(option => option.setName('r-rated').setDescription('Should the gif be R-rated')),
	async execute(interaction) {
        const embed = await getGifEmbed(interaction.options);
        await interaction.reply({ embeds: [embed] });
    },
};

async function getGifEmbed(options) {
    let rating = 'low';
    try {
        if (options.getBoolean('r-rated')) {
            rating = 'off';
        }
    }
    catch {
        rating = 'low';
    }

    let search;
    try {
        search = options.getString('what').trim();
    }
    catch {
        const gifs = `https://g.tenor.com/v1/random?key=${process.env.TENOR}&limit=${gifAmount}&contentfilter=${rating}`;
        return help.getGifEmbed(gifs, gifAmount);
    }

    const searchSplits = search.split(/[ ]+/);
    const searchKey = searchSplits.join('-');

    const gifs = `https://g.tenor.com/v1/search?q=${searchKey}&key=${process.env.TENOR}&limit=${gifAmount}&contentfilter=${rating}`;
    return help.getGifEmbed(gifs, gifAmount);
}