import { MessageActionRow, MessageButton, TextChannel } from "discord.js";
import EmojiEmbed from "../utils/generateEmojiEmbed.js";
import getEmojiByName from "../utils/getEmojiByName.js";
import { PasteClient, Publicity, ExpireDate } from "pastebin-api";
import config from "../config/main.json" assert { type: "json" };
import client from "../utils/client.js";

const pbClient = new PasteClient(config.pastebinApiKey);

export default async function (interaction) {
    const { log, NucleusColors, entry, renderUser, renderDelta } =
        client.logger;

    let messages = [];
    let deleted = 100;

    while (deleted === 100) {
        let fetched;
        await (interaction.channel as TextChannel).messages
            .fetch({ limit: 100 })
            .then(async (ms) => {
                fetched = await (interaction.channel as TextChannel).bulkDelete(
                    ms,
                    true
                );
            });
        deleted = fetched.size;
        if (fetched) {
            messages = messages.concat(fetched.map((m) => m));
        }
    }
    let out = "";
    messages.reverse().forEach((message) => {
        if (!message.author.bot) {
            const sentDate = new Date(message.createdTimestamp);
            out += `${message.author.username}#${
                message.author.discriminator
            } (${message.author.id}) [${sentDate.toUTCString()}]\n`;
            const lines = message.content.split("\n");
            lines.forEach((line) => {
                out += `> ${line}\n`;
            });
            out += "\n\n";
        }
    });
    const member = interaction.channel.guild.members.cache.get(
        interaction.channel.topic.split(" ")[0]
    );
    let m;
    if (out !== "") {
        const url = await pbClient.createPaste({
            code: out,
            expireDate: ExpireDate.Never,
            name: `Ticket Transcript for ${member.user.username}#${
                member.user.discriminator
            } (Created at ${new Date(
                interaction.channel.createdTimestamp
            ).toDateString()})`,
            publicity: Publicity.Unlisted
        });
        const guildConfig = await client.database.guilds.read(
            interaction.guild.id
        );
        m = await interaction.reply({
            embeds: [
                new EmojiEmbed()
                    .setTitle("Transcript")
                    .setDescription(
                        "You can view the transcript using the link below. You can save the link for later" +
                            (guildConfig.logging.logs.channel
                                ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
                                : ".")
                    )
                    .setStatus("Success")
                    .setEmoji("CONTROL.DOWNLOAD")
            ],
            components: [
                new MessageActionRow().addComponents([
                    new MessageButton()
                        .setLabel("View")
                        .setStyle("LINK")
                        .setURL(url),
                    new MessageButton()
                        .setLabel("Delete")
                        .setStyle("DANGER")
                        .setCustomId("close")
                        .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
                ])
            ],
            fetchReply: true
        });
    } else {
        m = await interaction.reply({
            embeds: [
                new EmojiEmbed()
                    .setTitle("Transcript")
                    .setDescription(
                        "The transcript was empty, so no changes were made. To delete this ticket, press the delete button below."
                    )
                    .setStatus("Success")
                    .setEmoji("CONTROL.DOWNLOAD")
            ],
            components: [
                new MessageActionRow().addComponents([
                    new MessageButton()
                        .setLabel("Delete")
                        .setStyle("DANGER")
                        .setCustomId("close")
                        .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
                ])
            ],
            fetchReply: true
        });
    }
    let i;
    try {
        i = await m.awaitMessageComponent({ time: 300000 });
        i.deferUpdate();
    } catch {
        return;
    }
    const data = {
        meta: {
            type: "ticketDeleted",
            displayName: "Ticket Deleted",
            calculateType: "ticketUpdate",
            color: NucleusColors.red,
            emoji: "GUILD.TICKET.CLOSE",
            timestamp: new Date().getTime()
        },
        list: {
            ticketFor: entry(
                interaction.channel.topic.split(" ")[0],
                renderUser(
                    (
                        await interaction.guild.members.fetch(
                            interaction.channel.topic.split(" ")[0]
                        )
                    ).user
                )
            ),
            deletedBy: entry(
                interaction.member.user.id,
                renderUser(interaction.member.user)
            ),
            deleted: entry(
                new Date().getTime(),
                renderDelta(new Date().getTime())
            )
        },
        hidden: {
            guild: interaction.guild.id
        }
    };
    log(data);
    await interaction.channel.delete();
    return;
}
