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

/**
 * Asynchronously fetches the last magazine entry.
 *
 * @returns {Promise<Magazine | undefined>} The last magazine entry if successful, or undefined.
 */
export const fetchLastMagazine = async (): Promise<
  Magazine | undefined
> => {
  const payload = await getPayload({ config })
  const result = await payload.find({
    collection: 'magazine',
    sort: '-date',
    limit: 1,
  })
  return result.docs[0]
}
