import { NextResponse } from 'next/server'

import configPromise from '@/payload.config'
import { getPayload } from 'payload'

export async function GET(req: Request) {
  const { searchParams } = new URL(req.url)
  const limit = Number(searchParams.get('limit') || 6)
  const payload = await getPayload({ config: configPromise })
  const result = await payload.find({
    collection: 'reviews',
    limit,
    pagination: false,
    depth: 2,
  })
  return NextResponse.json({ docs: result.docs, totalCount: result.totalDocs })
}
