import SiteContent from '../../models/siteContent.js'
import { SITE_CONTENT_KEYS } from '../../utils/schemas.js'

export default defineEventHandler(async (event) => {
  const key = getRouterParam(event, 'key')

  if (!SITE_CONTENT_KEYS.includes(key)) {
    throw createError({ statusCode: 404, statusMessage: 'Unknown content key' })
  }

  const doc = await SiteContent.findOne({ key }).lean()
  return {
    key,
    title: doc?.title || '',
    body: doc?.body || '',
    updatedAt: doc?.updatedAt || null
  }
})
