import { getPayload } from 'payload'
import configPromise from '@payload-config'
import { News } from '@/payload-types'
import { ContactSectionForm } from '@/globals/ContactForm/Component'
import { NewsSectionBlock } from '@/components/pages/Home/NewsSection'
import { ArticleContent } from '@/components/pages/Blog/ArticleContent'
import { getI18n } from '@/locales/server'
import { notFound } from 'next/navigation'

export default async function ArticlePage({ params }: { params: { id: string } }) {
  const payload = await getPayload({ config: configPromise })
  const { id } = await params

  let currentNews
  try {
    currentNews = await payload.findByID({
      collection: 'news',
      id: id,
      depth: 2,
    })
  } catch {
    return notFound()
  }

  if (!currentNews) {
    return notFound()
  }

  const result = await payload.find({
    collection: 'news',
    limit: 1000,
    pagination: false,
    depth: 2,
  })

  const docs = result.docs as News[]

  const t = await getI18n()

  return (
    <>
      <ArticleContent currentNews={currentNews} allNews={docs} />
      <NewsSectionBlock title={t('blog.relatedPosts')} subtitle="" />
      <ContactSectionForm />
    </>
  )
}
