import { getCommandMentionByName } from "./../utils/getCommandDataByName.js";
import client from "../utils/client.js";
import keyValueList from "../utils/generateKeyValueList.js";
import singleNotify from "../utils/singleNotify.js";
import { saveAttachment } from "../reflex/scanners.js";
import EmojiEmbed from "../utils/generateEmojiEmbed.js";
import addPlural from "../utils/plurals.js";
import type { GuildTextBasedChannel, Message } from "discord.js";

export default async function logAttachment(message: Message): Promise<AttachmentLogSchema> {
    if (message.guild) client.database.premium.hasPremium(message.guild.id).finally(() => {});
    if (!message.guild) throw new Error("Tried to log an attachment in a non-guild message");
    const { renderUser, renderChannel, renderDelta } = client.logger;
    const attachments = [];
    for (const attachment of message.attachments.values()) {
        attachments.push({
            local: (await saveAttachment(attachment.url))[0],
            url: attachment.url,
            height: attachment.height,
            width: attachment.width,
            size: attachment.size
        });
    }
    const links = message.content.match(/https?:\/\/\S+/gi) ?? [];
    for (const link of links) {
        if (link.toLowerCase().match(/\.(jpg|jpeg|png|gif|gifv|webm|webp|mp4|wav|mp3|ogg)$/gi)) {
            attachments.push({
                local: (await saveAttachment(link))[0],
                url: link,
                height: null,
                width: null
            });
        }
    }
    if (attachments.length === 0) return { files: [] };
    if (await client.database.premium.hasPremium(message.guild.id)) {
        const channel = (await client.database.guilds.read(message.guild.id)).logging.attachments.channel;
        if (!channel) {
            singleNotify(
                "noAttachmentLogChannel",
                message.guild.id,
                `No channel set for attachment logging. You can set one with ${getCommandMentionByName(
                    "settings/logs/attachments"
                )}`,
                "Warning"
            );
            return { files: attachments };
        }
        const channelObj = await message.guild.channels.fetch(channel);
        if (!channelObj) {
            singleNotify(
                "attachmentLogChannelDeleted",
                message.guild.id,
                `Your attachment history channel was deleted or is not longer accessible. You can set a new one with ${getCommandMentionByName(
                    "settings/logs/attachments"
                )}`,
                "Warning"
            );
            return { files: attachments };
        }
        const m = await (channelObj as GuildTextBasedChannel).send({
            embeds: [
                new EmojiEmbed()
                    .setTitle(`${addPlural(attachments.length, "Attachment")} Sent`)
                    .setDescription(
                        keyValueList({
                            messageId: `\`${message.id}\``,
                            sentBy: renderUser(message.author),
                            sentIn: renderChannel(message.channel as GuildTextBasedChannel),
                            sent: renderDelta(new Date(message.createdTimestamp).getTime())
                        }) + `\n[[Jump to message]](${message.url})`
                    )
                    .setEmoji("ICONS.ATTACHMENT")
                    .setStatus("Success")
            ],
            files: attachments.map((file) => file.local)
        });
        client.database.guilds.write(message.guild.id, {
            [`logging.attachments.saved.${message.channel.id}${message.id}`]: m.url
        });
        return { files: attachments, jump: m.url };
    } else {
        return { files: attachments };
    }
}

export interface AttachmentLogSchema {
    files: {
        url: string;
        local: string;
        height: number | null;
        width: number | null;
    }[];
    jump?: string;
}
