// Member leave handling (Phase 6b): record a leave event for the dashboard's
// members feed. Fires on both voluntary leaves and kicks/bans — Discord doesn't
// distinguish them on this event, and the mod-action (if any) is logged
// separately via mod_actions, so a leave row here is purely the lifecycle fact.
const memberEvents = require('../model/memberEvents')
const createLogger = require('../utils/logger')

const log = createLogger('members')

async function handleGuildMemberRemove(member) {
  try {
    await memberEvents.record({
      guildId: member.guild.id,
      eventType: 'leave',
      discordUserId: member.id,
      username: member.user?.tag,
    })
  } catch (err) {
    log.warn('member leave record failed', { userId: member.id, message: err.message })
  }
}

module.exports = { handleGuildMemberRemove }
