import { stringify } from 'qs-esm'
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 query: any = {
    and: [
      {
        month: {
          equals: month,
        },
        year: {
          equals: year
        }
      }
    ],
  }

  const stringifiedQuery = stringify(
    {
      where: query,
      limit: 1
    },
    { addQueryPrefix: true },
  )

  const response = await fetch(`http://localhost:3000/api/popePrayerIntentions${stringifiedQuery}`)
  if (!response.ok) return undefined
  let data = await response.json()
  return data.docs[0]
}