import moment from 'moment'
import { fetchUpcomingOccurrences } from '@/fetch/eventOccurrences'
import { fetchWorship } from '@/fetch/worship'
import { fetchBlogPosts } from '@/fetch/blog'
import { fetchHighlights } from '@/fetch/highlights'
import { fetchLastAnnouncements } from '@/fetch/announcement'
import { fetchLastCalendars } from '@/fetch/calendar'
import { perParish } from '@/utils/dto/perParish'
import { PublicationAndNewsletter } from '@/compositions/PublicationAndNewsletter/PublicationAndNewsletter'
import { HomeView } from './HomeView'

export const Home = async () => {
  const fromDate = moment().isoWeekday(1).hours(0).minutes(0)
  const tillDate = moment().isoWeekday(7).hours(23).minutes(59)

  const events = await fetchUpcomingOccurrences()
  const worship = await fetchWorship({
    fromDate: fromDate.toDate(),
    tillDate: tillDate.toDate(),
  })
  const blog = await fetchBlogPosts(true)
  const highlights = await fetchHighlights()
  const announcements = await fetchLastAnnouncements()
  const announcementsLinks = announcements ? perParish(announcements) : []
  const calendars = await fetchLastCalendars()
  const calendarsLinks = calendars ? perParish(calendars) : []

  return (
    <HomeView
      events={events?.docs || []}
      worship={worship?.docs || []}
      blog={blog?.docs || []}
      highlights={highlights?.docs || []}
      announcements={announcementsLinks}
      calendars={calendarsLinks}
      publicationAndNewsletter={<PublicationAndNewsletter />}
    />
  )
}
