import { loadPublicEvent } from '../../utils/loadEvent.js'

export default defineEventHandler(async (event) => {
  try {
    const identifier = getRouterParam(event, 'id')
    const eventData = await loadPublicEvent(event, identifier, {
      lean: true,
      select: '-registrations.email'
    })

    return {
      ...eventData,
      id: eventData._id.toString(),
      registeredCount: eventData.registrations?.length || 0,
      isFull: eventData.maxAttendees
        ? (eventData.registrations?.length || 0) >= eventData.maxAttendees
        : false
    }
  } catch (error) {
    if (error.statusCode) {
      throw error
    }
    console.error('Error fetching event:', error)
    throw createError({
      statusCode: 500,
      statusMessage: 'Failed to fetch event'
    })
  }
})
