import WikiArticle from '../../models/wikiArticle.js'
import { connectDB } from '../../utils/mongoose.js'

export default defineEventHandler(async (event) => {
  await connectDB()

  const query = getQuery(event)
  const limit = Math.min(Math.max(parseInt(query.limit) || 4, 1), 10)

  const articles = await WikiArticle.find({
    publishedAt: { $ne: null },
    hidden: { $ne: true }
  })
    .sort({ publishedAt: -1 })
    .limit(limit)
    .select('title url publishedAt')
    .lean()

  return articles
})
