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 eventData = await Event.findById(eventId)

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

    return { data: eventData }
  } catch (error) {
    if (error.statusCode) throw error
    console.error('Error fetching event:', error)
    throw createError({
      statusCode: 500,
      statusMessage: error.message || 'Failed to fetch event'
    })
  }
})
