// @ts-expect-error
import humanizeDuration from "humanize-duration";
import type { CommandInteraction, GuildMember, TextChannel } from "discord.js";
import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
import keyValueList from "../../utils/generateKeyValueList.js";
import confirmationMessage from "../../utils/confirmationMessage.js";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";

const command = (builder: SlashCommandSubcommandBuilder) =>
    builder
        .setName("slowmode")
        .setDescription("Manages slowmode in a channel")
        .addStringOption((option) =>
            option
                .setName("time")
                .setDescription("The delay between messages")
                .setRequired(false)
                .addChoices(
                    {name: "Off", value: "0"},
                    {name: "5 seconds", value: "5"},
                    {name: "10 seconds", value: "10"},
                    {name: "15 seconds", value: "15"},
                    {name: "30 seconds", value: "30"},
                    {name: "1 minute", value: "60"},
                    {name: "2 minutes", value: "120"},
                    {name: "5 minutes", value: "300"},
                    {name: "10 minutes", value: "600"},
                    {name: "15 minutes", value: "900"},
                    {name: "30 minutes", value: "1800"},
                    {name: "1 hour", value: "3600"},
                    {name: "2 hours", value: "7200"},
                    {name: "6 hours", value: "21600"}
                )
        );

const callback = async (interaction: CommandInteraction): Promise<void> => {
    let time = parseInt(interaction.options.get("time")?.value as string || "0");
    if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) {
        time = 10;
    }
    const confirmation = await new confirmationMessage(interaction)
        .setEmoji("CHANNEL.SLOWMODE.OFF")
        .setTitle("Slowmode")
        .setDescription(
            keyValueList({
                time: time ? humanizeDuration(time * 1000, { round: true }) : "No delay"
            }) + "Are you sure you want to set the slowmode in this channel?"
        )
        .setColor("Danger")
        .setFailedMessage("No changes were made", "Danger", "CHANNEL.SLOWMODE.ON")
        .send();
    if (confirmation.cancelled || !confirmation.success) return;
    try {
        (interaction.channel as TextChannel).setRateLimitPerUser(time);
    } catch (e) {
        await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setEmoji("CHANNEL.SLOWMODE.OFF")
                    .setTitle("Slowmode")
                    .setDescription("Something went wrong while setting the slowmode")
                    .setStatus("Danger")
            ],
            components: []
        });
    }
    await interaction.editReply({
        embeds: [
            new EmojiEmbed()
                .setEmoji("CHANNEL.SLOWMODE.ON")
                .setTitle("Slowmode")
                .setDescription("The channel slowmode was set successfully")
                .setStatus("Success")
        ],
        components: []
    });
};

const check = (interaction: CommandInteraction) => {
    const member = interaction.member as GuildMember;
    // Check if Nucleus can set the slowmode
    if (!interaction.guild!.members.me!.permissions.has("ManageChannels")) throw new Error("I do not have the *Manage Channels* permission");
    // Check if the user has manage_channel permission
    if (!member.permissions.has("ManageChannels")) throw new Error("You do not have the *Manage Channels* permission");
    // Allow slowmode
    return true;
};

export { command, callback, check };
