import { getPayload } from 'payload'
import configPromise from '@payload-config'
import { NextResponse } from 'next/server'

export async function GET(request: Request) {
  const payload = await getPayload({ config: configPromise })
  const { searchParams } = new URL(request.url)
  const page = Number(searchParams.get('page') || '1')
  const limit = 16

  const result = await payload.find({
    collection: 'cars',
    limit,
    page,
    depth: 2,
  })

  return NextResponse.json({
    cars: result.docs,
    hasNextPage: result.hasNextPage,
  })
}
