import Event from '../../../models/event.js'
import { connectDB } from '../../../utils/mongoose.js'
import { requireAdmin } from '../../../utils/auth.js'

export default defineEventHandler(async (event) => {
  try {
    await requireAdmin(event)

    const eventId = getRouterParam(event, 'id')

    await connectDB()

    const deletedEvent = await Event.findByIdAndDelete(eventId)

    if (!deletedEvent) {
      throw createError({
        statusCode: 404,
        statusMessage: 'Event not found'
      })
    }

    return { success: true, message: 'Event deleted successfully' }
  } catch (error) {
    if (error.statusCode) throw error
    console.error('Error deleting event:', error)
    throw createError({
      statusCode: 500,
      statusMessage: error.message || 'Failed to delete event'
    })
  }
})
