import { callback as statsChannelUpdate } from "../reflex/statsChannelUpdate.js";

export const event = "guildUpdate";

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

    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"
    };

    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 (before.owner !== after.owner)
        list.owner = entry(
            [before.owner, after.owner],
            `${renderUser(before.owner.user)} -> ${renderUser(
                after.owner.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);
}
