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

export const event = 'guildUpdate'

export async function callback(client, before, after) {
    try { await statsChannelUpdate(client, after.me); } catch(e) { console.log(e)}
    try {
        const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = after.client.logger
        let auditLog = await getAuditLog(after, 'GUILD_UPDATE');
        let audit = auditLog.entries.filter(entry => entry.target.id === after.id).first();
        if (audit.executor.id === client.user.id) return;
        let 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))
        let 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);
    } catch (e) {}
}
