require('dotenv').config()
const mongoose = require('mongoose')

;(async () => {
  await mongoose.connect(process.env.MONGODB_URI)
  const db = mongoose.connection.db

  const email = 'jennie+cleonguyen@machinemagic.co'

  const memberRes = await db.collection('members').deleteOne({ email })
  console.log(`Deleted ${memberRes.deletedCount} member(s)`)

  const preRegRes = await db.collection('preregistrations').updateOne(
    { email },
    {
      $set: { status: 'pending', magicLinkJtiUsed: false },
      $unset: { acceptedAt: '', memberId: '' },
    }
  )
  console.log(`Reset ${preRegRes.modifiedCount} preRegistration(s)`)

  const member = await db.collection('members').findOne({ email })
  console.log('\nMember state after reset:')
  console.log(JSON.stringify(member, null, 2))

  const preReg = await db.collection('preregistrations').findOne(
    { email },
    { projection: { email: 1, status: 1, acceptedAt: 1, memberId: 1, magicLinkJtiUsed: 1 } }
  )
  console.log('\nPreRegistration state after reset:')
  console.log(JSON.stringify(preReg, null, 2))

  await mongoose.disconnect()
})()
