import { requireUserSession } from '../../../utils/auth'
import { getUnreadNotificationsCount } from '../../../database/queries/notifications'
import { handleApiError, createSuccessResponse } from '../../../utils/errors'

interface UnreadCountResponse {
  count: number
}

export default eventHandler(async (event) => {
  try {
    const session = await requireUserSession(event)
    const count = await getUnreadNotificationsCount(session.user.id)
    return createSuccessResponse<UnreadCountResponse>({ count })
  } catch (error) {
    throw handleApiError(error)
  }
})
