import { getPayload } from 'payload'
import config from '@/payload.config'

export const fetchHighlights = async () => {
  const date = new Date()
  date.setHours(0, 0, 0, 0)

  const payload = await getPayload({ config })
  return payload.find({
    collection: 'highlight',
    sort: 'date',
    where: {
      and: [
        {
          from: {
            less_than_equal: date.toISOString(),
          },
          until: {
            greater_than_equal: date.toISOString(),
          },
        },
      ],
    },
    limit: 3,
  })
}

export const fetchHighlightsBetweenDates = async (
  from: Date,
  until: Date,
) => {
  const payload = await getPayload({ config })
  return payload.find({
    collection: 'highlight',
    sort: 'date',
    where: {
      and: [
        {
          date: {
            greater_than_equal: from.toISOString(),
          },
        },
        {
          date: {
            less_than: until.toISOString(),
          },
        },
      ],
    },
    limit: 5,
  })
}
