import {
    ActionRowBuilder,
    AttachmentBuilder,
    ButtonBuilder,
    ButtonInteraction,
    ButtonStyle,
    ChannelType,
    CommandInteraction,
    ComponentType,
    Guild,
    GuildTextBasedChannel,
    ModalBuilder,
    ModalSubmitInteraction,
    TextInputBuilder,
    TextInputStyle
} from "discord.js";
import type { SlashCommandSubcommandBuilder } from "discord.js";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
import client from "../../utils/client.js";
import config from "../../config/main.js";

const command = (builder: SlashCommandSubcommandBuilder) =>
    builder.setName("stats").setDescription("Gets the bot's stats");

const confirm = async (interaction: CommandInteraction) => {
    const requiredTexts = [
        "just do it",
        "yes, do as i say!",
        "clicksminuteper/nucleus",
        "i've said it once i'll say it again",
        "no, i've changed my mind",
        "this incident will be reported",
        "coded told me to",
        "mini told me to",
        "pinea told me to",
        "what's a java script",
        "it's a feature not a bug",
        "that never happened during testing"
    ];
    const chosen = requiredTexts[Math.floor(Math.random() * (requiredTexts.length - 1))]!;

    const modal = new ModalBuilder()
        .addComponents(
            new ActionRowBuilder<TextInputBuilder>().addComponents(
                new TextInputBuilder()
                    .setStyle(TextInputStyle.Short)
                    .setLabel(`Type "${chosen}" below`)
                    .setCustomId("confirm")
                    .setPlaceholder("Guild ID")
                    .setMinLength(chosen.length)
                    .setMaxLength(chosen.length)
            )
        )
        .setTitle("Admin Panel")
        .setCustomId("adminPanel");
    await interaction.showModal(modal);
    let out: ModalSubmitInteraction;
    try {
        out = await interaction.awaitModalSubmit({
            filter: (i) => i.customId === "adminPanel" && i.user.id === interaction.user.id,
            time: 300000
        });
    } catch {
        return;
    }
    await out.deferUpdate();
    const typed = out.fields.getTextInputValue("confirm");
    return typed.toLowerCase() === chosen.toLowerCase();
};

const callback = async (interaction: CommandInteraction): Promise<void> => {
    const description = `**Servers:** ${client.guilds.cache.size}\n` + `**Ping:** \`${client.ws.ping * 2}ms\``;
    const m = await interaction.reply({
        embeds: [
            new EmojiEmbed()
                .setTitle("Stats")
                .setDescription(description)
                .setStatus("Success")
                .setEmoji("SETTINGS.STATS.GREEN")
        ],
        ephemeral: true,
        fetchReply: true
    });
    if (config.owners.includes(interaction.user.id)) {
        await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setTitle("Admin")
                    .setDescription(description)
                    .setStatus("Success")
                    .setEmoji("SETTINGS.STATS.GREEN")
            ],
            components: [
                new ActionRowBuilder<ButtonBuilder>().addComponents(
                    new ButtonBuilder().setCustomId("admin").setLabel("Admin Panel").setStyle(ButtonStyle.Primary),
                    new ButtonBuilder().setCustomId("announce").setLabel("Announce to all Guilds").setStyle(ButtonStyle.Danger)
                )
            ]
        });

        const modal = new ModalBuilder()
            .addComponents(
                new ActionRowBuilder<TextInputBuilder>().addComponents(
                    new TextInputBuilder()
                        .setStyle(TextInputStyle.Short)
                        .setLabel("Guild ID")
                        .setCustomId("guildID")
                        .setPlaceholder("Guild ID")
                        .setMinLength(16)
                        .setMaxLength(25)
                )
            )
            .setTitle("Admin Panel")
            .setCustomId("adminPanel");
        let i1: ButtonInteraction;
        const channel = await client.channels.fetch(interaction.channelId);
        if (
            !channel ||
            [ChannelType.GuildCategory, ChannelType.GroupDM, ChannelType.GuildStageVoice].includes(channel.type)
        )
            return;
        // console.log(interaction)
        if (!("awaitMessageComponent" in channel)) return;
        let GuildID = interaction.guildId;
        try {
            i1 = await m.awaitMessageComponent<ComponentType.Button>({
                filter: (i) => i.user.id === interaction.user.id,
                time: 300000
            });
        } catch (e) {
            console.log(e);
            return;
        }
        switch(i1.customId) {
            case "admin": {
                if (!GuildID) {
                    await i1.showModal(modal);
                    let out: ModalSubmitInteraction;
                    try {
                        out = await i1.awaitModalSubmit({
                            filter: (i) => i.customId === "adminPanel" && i.user.id === interaction.user.id,
                            time: 300000
                        });
                    } catch {
                        return;
                    }
                    await out.deferUpdate();
                    GuildID = out.fields.getTextInputValue("guildID");
                } else if (!client.guilds.cache.has(GuildID)) {
                    await interaction.editReply({
                        embeds: [new EmojiEmbed().setTitle("Admin").setDescription("Not in server").setStatus("Danger")],
                        components: []
                    });
                }

                await interaction.editReply({
                    embeds: [],
                    components: [
                        new ActionRowBuilder<ButtonBuilder>().addComponents(
                            new ButtonBuilder().setCustomId("stats").setLabel("Stats").setStyle(ButtonStyle.Primary),
                            new ButtonBuilder().setCustomId("data").setLabel("Guild data").setStyle(ButtonStyle.Secondary),
                            new ButtonBuilder().setCustomId("cache").setLabel("Reset cache").setStyle(ButtonStyle.Success),
                            new ButtonBuilder().setCustomId("leave").setLabel("Leave").setStyle(ButtonStyle.Danger),
                            new ButtonBuilder().setCustomId("purge").setLabel("Delete data").setStyle(ButtonStyle.Danger)
                        )
                    ]
                });
                let i;
                try {
                    i = await m.awaitMessageComponent<ComponentType.Button>({
                        filter: (i) => i.user.id === interaction.user.id && i.message.id === m.id,
                        time: 300000
                    });
                } catch {
                    return;
                }
                const guild = (await client.guilds.fetch(GuildID)) as Guild | null;
                if (!guild) {
                    await i.deferUpdate();
                    await interaction.editReply({
                        embeds: [new EmojiEmbed().setTitle("Admin").setDescription("Not in server").setStatus("Danger")],
                        components: []
                    });
                    return;
                }
                if (i.customId === "stats") {
                    await i.deferUpdate();
                    await interaction.editReply({
                        embeds: [
                            new EmojiEmbed()
                                .setTitle("Stats")
                                .setDescription(
                                    `**Name:** ${guild.name}\n` +
                                        `**ID:** \`${guild.id}\`\n` +
                                        `**Owner:** ${client.users.cache.get(guild.ownerId)!.tag}\n` +
                                        `**Member Count:** ${guild.memberCount}\n` +
                                        `**Created:** <t:${guild.createdTimestamp}:F>\n` +
                                        `**Added Nucleus:** <t:${guild.members.me!.joinedTimestamp}:R>\n` +
                                        `**Nucleus' Perms:** https://discordapi.com/permissions.html#${guild.members.me!.permissions.valueOf()}\n`
                                )
                                .setStatus("Success")
                                .setEmoji("SETTINGS.STATS.GREEN")
                        ]
                    });
                } else if (i.customId === "leave") {
                    if (!(await confirm(interaction))) {
                        await interaction.editReply({
                            embeds: [new EmojiEmbed().setTitle("No changes were made").setStatus("Danger")],
                            components: []
                        });
                        return;
                    }
                    await guild.leave();
                    await interaction.editReply({
                        embeds: [
                            new EmojiEmbed()
                                .setTitle("Left")
                                .setDescription(`Left ${guild.name}`)
                                .setStatus("Success")
                                .setEmoji("SETTINGS.STATS.GREEN")
                        ],
                        components: []
                    });
                } else if (i.customId === "data") {
                    await i.deferUpdate();
                    // Get all the data and convert to a string
                    const data = await client.database.guilds.read(guild.id);
                    const stringified = JSON.stringify(data, null, 2);
                    const buffer = Buffer.from(stringified);
                    const attachment = new AttachmentBuilder(buffer).setName("data.json");
                    await interaction.editReply({
                        embeds: [
                            new EmojiEmbed().setTitle("Data").setDescription(`Data for ${guild.name}`).setStatus("Success")
                        ],
                        components: [],
                        files: [attachment]
                    });
                } else if (i.customId === "purge") {
                    if (!(await confirm(interaction))) {
                        await interaction.editReply({
                            embeds: [new EmojiEmbed().setTitle("No changes were made").setStatus("Danger")],
                            components: []
                        });
                        return;
                    }
                    await client.database.guilds.delete(GuildID);
                    await client.database.history.delete(GuildID);
                    await client.database.notes.delete(GuildID);
                    await client.database.transcripts.deleteAll(GuildID);
                    await interaction.editReply({
                        embeds: [
                            new EmojiEmbed()
                                .setTitle("Purge")
                                .setDescription(`Deleted data for ${guild.name}`)
                                .setStatus("Success")
                                .setEmoji("SETTINGS.STATS.GREEN")
                        ],
                        components: []
                    });
                } else if (i.customId === "cache") {
                    await i.deferUpdate();
                    await client.memory.forceUpdate(guild.id);
                    await interaction.editReply({
                        embeds: [
                            new EmojiEmbed()
                                .setTitle("Cache")
                                .setDescription(`Reset cache for ${guild.name}`)
                                .setStatus("Success")
                                .setEmoji("SETTINGS.STATS.GREEN")
                        ],
                        components: []
                    });
                }
                break;
            }
            case "announce": {
                const channelsToNotify = await client.database.guilds.staffChannels()
                const modal2 = new ModalBuilder()
                    .addComponents(
                        new ActionRowBuilder<TextInputBuilder>().addComponents(
                            new TextInputBuilder()
                                .setStyle(TextInputStyle.Paragraph)
                                .setLabel("Announcement")
                                .setCustomId("announcement")
                                .setPlaceholder("Announcement...")
                        )
                    )
                    .setTitle("Announcement")
                    .setCustomId("announce");
                await i1.showModal(modal2);
                let out: ModalSubmitInteraction;
                try {
                    out = await i1.awaitModalSubmit({
                        filter: (i) => i.customId === "announce" && i.user.id === interaction.user.id,
                        time: 300000
                    });
                } catch {
                    return;
                }
                await out.deferUpdate();
                const announcement = out.fields.getTextInputValue("announcement");
                await interaction.editReply({
                    embeds: [
                        new EmojiEmbed()
                            .setTitle("Announcement")
                            .setDescription(`Announcement will be sent to ${channelsToNotify.length} channels.\n\n${announcement}`)
                            .setStatus("Success")
                            .setEmoji("SETTINGS.STATS.GREEN")
                    ],
                    components: [
                        new ActionRowBuilder<ButtonBuilder>().addComponents(
                            new ButtonBuilder().setCustomId("confirm").setLabel("Confirm").setStyle(ButtonStyle.Success),
                            new ButtonBuilder().setCustomId("cancel").setLabel("Cancel").setStyle(ButtonStyle.Danger)
                        )
                    ]
                });

                let i;
                try {
                    i = await m.awaitMessageComponent<ComponentType.Button>({
                        filter: (i) => i.user.id === interaction.user.id,
                        time: 300000
                    });
                } catch {
                    return;
                }
                if (i.customId === "confirm") {
                    await i.deferUpdate();
                    await interaction.editReply({
                        embeds: [
                            new EmojiEmbed()
                                .setTitle("Announcement")
                                .setDescription(`Sending to ${channelsToNotify.length} channels. Preview:\n\n${announcement}`)
                                .setStatus("Success")
                                .setEmoji("SETTINGS.STATS.GREEN")
                        ],
                        components: []
                    });
                    const announcementEmbed = new EmojiEmbed()
                        .setTitle("Developer Announcement")
                        .setDescription(announcement)
                        .setStatus("Danger")
                        .setEmoji("NUCLEUS.LOGO")
                        .setFooter({text: `Sent by ${interaction.user.username}`, iconURL: interaction.user.displayAvatarURL()})
                    for (const channel of channelsToNotify) {
                        const ch = await client.channels.fetch(channel) as GuildTextBasedChannel | null;
                        if (!ch) continue;
                        await ch.send({
                            embeds: [announcementEmbed]
                        });
                    }
                    await interaction.editReply({
                        embeds: [
                            new EmojiEmbed()
                                .setTitle("Announcement")
                                .setDescription(`Sent to ${channelsToNotify.length} channels. Preview:\n\n${announcement}`)
                                .setStatus("Success")
                                .setEmoji("SETTINGS.STATS.GREEN")
                        ],
                        components: []
                    })
                } else if (i.customId === "cancel") {
                    await i.deferUpdate();
                    await interaction.editReply({
                        embeds: [new EmojiEmbed().setTitle("Announcement Cancelled").setStatus("Danger")],
                        components: []
                    });
                }
                break;
            }
        }
    }
};

export { command };
export { callback };
