import Member from '../../../models/member.js'
import { connectDB } from '../../../utils/mongoose.js'

export default defineEventHandler(async (event) => {
  await requireAdmin(event)
  const { members } = await validateBody(event, bulkMemberImportSchema)
  await connectDB()

  // Check for duplicate emails within the batch
  const batchEmails = members.map(m => m.email)
  const uniqueEmails = new Set(batchEmails)
  if (uniqueEmails.size !== batchEmails.length) {
    throw createError({
      statusCode: 400,
      statusMessage: 'Duplicate emails found in import batch'
    })
  }

  // Check which emails already exist in the DB
  const existingMembers = await Member.find(
    { email: { $in: batchEmails } },
    { email: 1 }
  )
  const existingEmails = new Set(existingMembers.map(m => m.email))

  const results = []

  for (const row of members) {
    if (existingEmails.has(row.email)) {
      results.push({ email: row.email, success: false, error: 'Email already exists' })
      continue
    }

    try {
      const member = new Member({
        name: row.name,
        email: row.email,
        circle: row.circle,
        contributionAmount: row.contributionAmount,
        slackInvited: false
      })
      const saved = await member.save()
      results.push({ email: row.email, success: true, memberId: saved._id })
    } catch (err) {
      results.push({ email: row.email, success: false, error: err.message })
    }
  }

  const created = results.filter(r => r.success).length
  const failed = results.filter(r => !r.success).length

  return { created, failed, results }
})
