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

/**
 * Fetch pope prayer intentions
 *
 * @param year
 * @param month - in the form of '01' for january, '02' for february ...
 */
export const fetchPopePrayerIntentions = async (
  year: number,
  month: string,
): Promise<PopePrayerIntention | undefined> => {
  const payload = await getPayload({ config })
  const result = await payload.find({
    collection: 'popePrayerIntentions',
    where: {
      and: [
        {
          month: {
            equals: month,
          },
          year: {
            equals: year,
          },
        },
      ],
    },
    limit: 1,
  })
  return result.docs[0]
}
