import BoardPost from '../../models/boardPost.js'
import { requireAuth } from '../../utils/auth.js'

export default defineEventHandler(async (event) => {
  const member = await requireAuth(event)

  const query = getQuery(event)
  const dbQuery = {}

  if (query.tag) {
    dbQuery.tags = query.tag
  }

  if (query.author) {
    dbQuery.author = query.author === 'me' ? member._id : query.author
  }

  const posts = await BoardPost.find(dbQuery)
    .sort({ createdAt: -1 })
    .limit(200)
    .populate('author', 'name avatar circle board.slackHandle')
    .lean()

  return { posts }
})
