import { neon } from '@neondatabase/serverless'
import type { VersionResponse } from '../types/api'
import { handleApiError, createSuccessResponse } from '../utils/errors'

export default defineCachedEventHandler(
  async (_event) => {
    try {
      const { databaseUrl } = useRuntimeConfig()
      const db = neon(databaseUrl)
      const result = await db`SELECT version()` as VersionResponse
      return createSuccessResponse<VersionResponse>(result)
    } catch (error) {
      throw handleApiError(error)
    }
  },
  {
    maxAge: 60 * 60 * 24 // cache it for a day
  }
)
