import type { Metadata } from 'next'
import { ScrollReveal } from '@/components/shared/ScrollReveal'
import { FeaturedNewsCarousel } from '@/components/news/FeaturedNewsCarousel'
import { NewsListFilter } from '@/components/news/NewsListFilter'
import { getNewsArticles, getNewsCategories } from '@/lib/server-news'
import { getSiteRecommendations } from '@/lib/server-site'

export const metadata: Metadata = { title: '新闻资讯' }

export default async function NewsPage() {
  const [newsArticles, categories, recommendations] = await Promise.all([getNewsArticles(), getNewsCategories(), getSiteRecommendations(['news.carousel'])])
  const managedFeaturedArticles = (recommendations.placements['news.carousel'] || [])
    .map((slot) => newsArticles.find((article) => article.id === slot.sourceId || article.slug === slot.sourceId))
    .filter((article): article is NonNullable<typeof article> => Boolean(article))
  const featuredArticles = managedFeaturedArticles.length ? managedFeaturedArticles : []

  return (
    <>
      <ScrollReveal />
      {featuredArticles.length > 0 && <FeaturedNewsCarousel articles={featuredArticles} />}
      <NewsListFilter articles={newsArticles} categories={categories} />
    </>
  )
}
