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

export default defineEventHandler(async (event) => {
  const member = await requireAuth(event)
  const id = getRouterParam(event, 'id')

  const deleted = await BoardPost.findOneAndDelete({ _id: id, author: member._id })

  if (!deleted) {
    const exists = await BoardPost.exists({ _id: id })
    throw createError({
      statusCode: exists ? 403 : 404,
      statusMessage: exists ? 'Not authorized to delete this post' : 'Post not found',
    })
  }

  return { success: true }
})
