import { AuditLogEvent, GuildAuditLogsEntry, ThreadChannel } from "discord.js";
// @ts-expect-error
import humanizeDuration from "humanize-duration";
import type { NucleusClient } from "../utils/client.js";

export const event = "threadUpdate";

export async function callback(client: NucleusClient, oldThread: ThreadChannel, newThread: ThreadChannel) {
    const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger;
    const auditLog = (await getAuditLog(newThread.guild, AuditLogEvent.ThreadUpdate))
        .filter((entry: GuildAuditLogsEntry) => (entry.target as ThreadChannel)!.id === newThread.id)[0]!;
    if (auditLog.executor!.id === client.user!.id) return;
    const list: Record<string, ReturnType<typeof entry>> = {
        threadId: entry(newThread.id, `\`${newThread.id}\``),
        thread: entry(newThread.name, renderChannel(newThread)),
        parentChannel: entry(newThread.parentId, renderChannel(newThread.parent!))
    };
    if (oldThread.name !== newThread.name) {
        list["name"] = entry([oldThread.name, newThread.name], `${oldThread.name} -> ${newThread.name}`);
    }
    if (oldThread.autoArchiveDuration !== newThread.autoArchiveDuration) {
        list["autoArchiveDuration"] = entry(
            [oldThread.autoArchiveDuration!.toString(), newThread.autoArchiveDuration!.toString()],
            `${humanizeDuration((oldThread.autoArchiveDuration ?? 0) * 60 * 1000, {
                round: true
            })} -> ${humanizeDuration((newThread.autoArchiveDuration ?? 0) * 60 * 1000, {
                round: true
            })}`
        );
    }
    if (oldThread.rateLimitPerUser !== newThread.rateLimitPerUser) {
        list["slowmode"] = entry(
            [oldThread.rateLimitPerUser!.toString(), newThread.rateLimitPerUser!.toString()],
            `${humanizeDuration((oldThread.rateLimitPerUser ?? 0) * 1000)} -> ${humanizeDuration(
                (newThread.rateLimitPerUser ?? 0) * 1000
            )}`
        );
    }
    if (!(Object.keys(list).length - 3)) return;
    list["updated"] = entry(new Date().getTime(), renderDelta(new Date().getTime()));
    list["updatedBy"] = entry(auditLog.executor!.id, renderUser(auditLog.executor!));
    const data = {
        meta: {
            type: "channelUpdate",
            displayName: "Thread Edited",
            calculateType: "channelUpdate",
            color: NucleusColors.yellow,
            emoji: "CHANNEL.TEXT.EDIT",
            timestamp: new Date().getTime()
        },
        list: list,
        hidden: {
            guild: newThread.guild.id
        }
    };
    log(data);
}
