import { GuildMember, PartialGuildMember } from 'discord.js';
import { EventHandler } from '../utils/types';
import { context } from '../config/context';

const event: EventHandler = {
  name: 'guildMemberUpdate',
  execute(oldMember: GuildMember | PartialGuildMember, newMember: GuildMember) {
    if (!newMember.guild) return;
    const before = new Set(oldMember.roles?.cache?.keys?.() || []);
    const after = new Set(newMember.roles.cache.keys());
    const added = Array.from(after).filter((id) => !before.has(id));
    const removed = Array.from(before).filter((id) => !after.has(id));
    if (!added.length && !removed.length) return;
    context.logging.logRoleUpdate(newMember, added, removed);
  }
};

export default event;
