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

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]
}

export const fetchMagazines = async (page = 1, limit = 12): Promise<PaginatedDocs<Magazine>> => {
  const payload = await getPayload({ config })
  return payload.find({
    collection: 'magazine',
    sort: '-date',
    limit,
    page,
  })
}
