// @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);
}
