import { SlashCommandBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, PermissionFlagsBits } from 'discord.js';
import db from '../../models/index.js';

export default {
	data: new SlashCommandBuilder()
		.setName('quotation')
		.setDescription('Enable or disable quotations')
		.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages),
	category: 'admin',
	async execute(interaction, args, client) {
		const quotationstat = await db.quotationStat.findOne({ where: { serverID: interaction.guild.id } });

		if (!quotationstat) {
			const body = { serverID: interaction.guild.id, stat: 'enable' };
			await db.quotationStat.create(body);
			return await interaction.reply({ content: 'Quotation has been enabled.', ephemeral: true });
		}


		const row = new ActionRowBuilder()
			.addComponents(
				new ButtonBuilder()
					.setCustomId(`yes${interaction.user.id}`)
					.setLabel('Yes')
					.setStyle(ButtonStyle.Primary),
			)
			.addComponents(
				new ButtonBuilder()
					.setCustomId(`no${interaction.user.id}`)
					.setLabel('No')
					.setStyle(ButtonStyle.Danger),
			);

		if (quotationstat.stat === 'enable') {
			await interaction.reply({ content: 'Quotation is already enabled, do you wish to disable it?', components: [row], ephemeral: true });
		}
		else {
			const body = { serverID: interaction.guild.id, stat: 'enable' };
			await db.autoresponseStat.update(body, { where: { serverID: interaction.guild.id } });
			return interaction.editReply({ content: 'Quotation has been enabled.', ephemeral: true });
		}

		client.on('interactionCreate', async (interactionMenu) => {
			if (interaction.user !== interactionMenu.user) return;
			if (!interactionMenu.isButton) return;
			interactionMenu.update({ components: [] });
			if (interactionMenu.customId === `yes${interaction.user.id}`) {
				const body = { serverID: interaction.guild.id, stat: 'disable' };
				await db.quotationStat.update(body, { where: { serverID: interaction.guild.id } });
				return interaction.editReply({ content: 'Quotation has been disabled.', ephemeral: true });
			}
			else {
				return interaction.editReply({ content: 'Nothing has been changed.', ephemeral: true });
			}
		});
	},
};
