import type { HaikuClient } from "../utils/haiku/index.js";
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);
}
