import type { NucleusClient } from "../utils/client.js";
import Discord, { AuditLogEvent, GuildAuditLogsEntry, Message, User } from "discord.js";

export const event = "messageDelete";

export async function callback(client: NucleusClient, message: Message) {
    try {
        if (message.author.id === client.user!.id) return;
        if (client.noLog.includes(`${message.id}/${message.channel.id}/${message.id}`)) return;
        const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
        const auditLog = (await getAuditLog(message.guild!, AuditLogEvent.MemberBanAdd))
            .filter((entry: GuildAuditLogsEntry) => (entry.target! as User).id === message.author.id)[0];
        if (auditLog) {
            if (auditLog.createdTimestamp - 1000 < new Date().getTime()) return;
        }
        const replyTo = message.reference;
        let content = message.cleanContent;
        content.replace("`", "\\`");
        if (content.length > 256) content = content.substring(0, 253) + "...";
        const attachments =
            message.attachments.size + (
                message.content.match(
                    /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
                ) ?? []
            ).length;
        let attachmentJump = "";
        const config = (await client.database.guilds.read(message.guild!.id)).logging.attachments.saved[
            message.channel.id + message.id
        ];
        if (config) { attachmentJump = ` [[View attachments]](${config})`; }
        const data = {
            meta: {
                type: "messageDelete",
                displayName: "Message Deleted",
                calculateType: "messageDelete",
                color: NucleusColors.red,
                emoji: "MESSAGE.DELETE",
                timestamp: new Date().getTime()
            },
            separate: {
                start: content ? `**Message:**\n\`\`\`${content}\`\`\`` : "**Message:** *Message had no content*"
            },
            list: {
                messageId: entry(message.id, `\`${message.id}\``),
                sentBy: entry(message.author.id, renderUser(message.author)),
                sentIn: entry(message.channel.id, renderChannel(message.channel as Discord.GuildChannel | Discord.ThreadChannel)),
                deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
                mentions: message.mentions.users.size,
                attachments: entry(attachments, attachments + attachmentJump),
                repliedTo: entry(
                    replyTo ? replyTo.messageId! : null,
                    replyTo ? `[[Jump to message]](https://discord.com/channels/${message.guild!.id}/${message.channel.id}/${replyTo.messageId})`
                            : "None"
                )
            },
            hidden: {
                guild: message.guild!.id
            }
        };
        log(data);
    } catch (e) {
        console.log(e);
    }
}
