import type { NucleusClient } from "../utils/client.js";
import { AuditLogEvent, Guild, GuildAuditLogsEntry } from "discord.js";
import { callback as statsChannelUpdate } from "../reflex/statsChannelUpdate.js";

export const event = "guildUpdate";

export async function callback(client: NucleusClient, before: Guild, after: Guild) {
    await statsChannelUpdate(client, after.members.me!);
    const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
    if (!await isLogging(after.id, "guildUpdate")) return;
    const auditLog = (await getAuditLog(after, AuditLogEvent.GuildUpdate))
        .filter((entry: GuildAuditLogsEntry) => (entry.target as Guild)!.id === after.id)[0]!;
    if (auditLog.executor!.id === client.user!.id) return;
    const list: Record<string, ReturnType<typeof entry>> = {};

    const verificationLevels = [
        "Unrestricted",
        "Verified email",
        "Registered (5 minutes)",
        "Member (10 minutes)",
        "Verified phone"
    ];

    const explicitContentFilterLevels = [
        "Disabled",
        "Members without roles",
        "All members"
    ];

    const MFALevels = [
        "None",
        "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.id, afterOwner.id],
            `${renderUser(beforeOwner.user)} -> ${renderUser(afterOwner.user)}`
        );
    if (before.verificationLevel !== after.verificationLevel)
        list["verificationLevel"] = entry(
            [verificationLevels[before.verificationLevel.valueOf()]!, verificationLevels[before.verificationLevel.valueOf()]!],
            `${verificationLevels[before.verificationLevel.valueOf()]} -> ${verificationLevels[before.verificationLevel.valueOf()]}`
        );
    if (before.explicitContentFilter !== after.explicitContentFilter)
        list["explicitContentFilter"] = entry(
            [
                explicitContentFilterLevels[before.explicitContentFilter.valueOf()]!,
                explicitContentFilterLevels[after.explicitContentFilter.valueOf()]!
            ],
            `${explicitContentFilterLevels[before.explicitContentFilter]} -> ${
                explicitContentFilterLevels[after.explicitContentFilter]
            }`
        );
    if (before.mfaLevel !== after.mfaLevel)
        list["2 factor authentication"] = entry(
            [MFALevels[before.mfaLevel.valueOf()]!, MFALevels[after.mfaLevel.valueOf()]!],
            `${MFALevels[before.mfaLevel.valueOf()]} -> ${MFALevels[after.mfaLevel.valueOf()]}`
        );

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