import { getPayload } from 'payload'
import config from '@/payload.config'
import { Search } from '@/payload-types'

export async function fetchSearchResults(query: string): Promise<Search[]> {
  const trimmed = query.trim()
  if (!trimmed) return []

  const payload = await getPayload({ config })
  const result = await payload.find({
    collection: 'search',
    where: {
      title: {
        contains: trimmed,
      },
    },
    depth: 1,
    limit: 50,
    sort: 'priority',
  })

  return result.docs
}
