// Auto-role on join. Requires the Server Members privileged intent (already
// enabled in the Discord Developer Portal per the Phase 1 setup notes).
const guildConfig = require('../model/guildConfig')
const createLogger = require('../utils/logger')

const log = createLogger('autorole')

async function handleGuildMemberAdd(member) {
  try {
    const roleId = await guildConfig.getAutoRoleId(member.guild.id)
    if (!roleId) return
    await member.roles.add(roleId)
    log.info('auto-role assigned', { userId: member.id, roleId })
  } catch (err) {
    log.warn('auto-role assignment failed', { userId: member.id, message: err.message })
  }
}

module.exports = { handleGuildMemberAdd }
