// @ts-expect-error
import type { HaikuClient } from "jshaiku";
import type { Guild, GuildAuditLogsEntry } from "discord.js";
import { callback as statsChannelUpdate } from "../reflex/statsChannelUpdate.js";

export const event = "guildUpdate";

export async function callback(client: HaikuClient, before: Guild, after: Guild) {
    await statsChannelUpdate(client, after.me!);
    const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
    const auditLog = await getAuditLog(after, "GUILD_UPDATE");
    const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === after.id).first();
    if (audit.executor.id === client.user.id) return;
    const list: Record<string, ReturnType<typeof entry>> = {};

    const verificationLevels = {
        NONE: "Unrestricted",
        LOW: "Verified email",
        MEDIUM: "Registered (5 minutes)",
        HIGH: "Member (10 minutes)",
        VERY_HIGH: "Verified phone"
    };

    const explicitContentFilterLevels = {
        DISABLED: "Disabled",
        MEMBERS_WITHOUT_ROLES: "Members without roles",
        ALL_MEMBERS: "All members"
    };

    const MFALevels = {
        NONE: "None",
        ELEVATED: "Enabled"
    };
    const beforeOwner = await before.fetchOwner();
    const afterOwner = await after.fetchOwner();

    if (before.name !== after.name) list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
    if (before.icon !== after.icon)
        list["icon"] = entry([before.icon, after.icon], `[Before](${before.iconURL()}) -> [After](${after.iconURL()})`);
    if (before.splash !== after.splash)
        list["splash"] = entry(
            [before.splash, after.splash],
            `[Before](${before.splashURL()}) -> [After](${after.splashURL()})`
        );
    if (before.banner !== after.banner)
        list["banner"] = entry(
            [before.banner, after.banner],
            `[Before](${before.bannerURL()}) -> [After](${after.bannerURL()})`
        );
    if (beforeOwner !== afterOwner)
        list["owner"] = entry(
            [beforeOwner, afterOwner],
            `${renderUser(beforeOwner.user)} -> ${renderUser(afterOwner.user)}`
        );
    if (before.verificationLevel !== after.verificationLevel)
        list["verificationLevel"] = entry(
            [verificationLevels[before.verificationLevel], verificationLevels[after.verificationLevel]],
            `${verificationLevels[before.verificationLevel]} -> ${verificationLevels[after.verificationLevel]}`
        );
    if (before.explicitContentFilter !== after.explicitContentFilter)
        list["explicitContentFilter"] = entry(
            [
                explicitContentFilterLevels[before.explicitContentFilter],
                explicitContentFilterLevels[after.explicitContentFilter]
            ],
            `${explicitContentFilterLevels[before.explicitContentFilter]} -> ${
                explicitContentFilterLevels[after.explicitContentFilter]
            }`
        );
    if (before.mfaLevel !== after.mfaLevel)
        list["2 factor authentication"] = entry(
            [MFALevels[before.mfaLevel], MFALevels[after.mfaLevel]],
            `${MFALevels[before.mfaLevel]} -> ${MFALevels[after.mfaLevel]}`
        );

    if (!Object.keys(list).length) return;
    list["updated"] = entry(new Date().getTime(), renderDelta(new Date().getTime()));
    list["updatedBy"] = entry(audit.executor.id, renderUser(audit.executor));
    const data = {
        meta: {
            type: "guildUpdate",
            displayName: "Guild Edited",
            calculateType: "guildUpdate",
            color: NucleusColors.yellow,
            emoji: "GUILD.YELLOW",
            timestamp: new Date().getTime()
        },
        list: list,
        hidden: {
            guild: after.id
        }
    };
    log(data);
}
