import { notFound } from 'next/navigation';
import { getResearchArticleById, getResearchArticles } from '@/services/server/research';
import ArticleDetailsContent from '@/components/ArticleDetailsContent';
import { Metadata } from 'next';

export const dynamic = 'force-dynamic';

interface PageProps {
    params: Promise<{ id: string }>;
}

export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
    const { id } = await params;
    const article = await getResearchArticleById(id);

    if (!article) {
        return {
            title: 'Article Not Found',
        };
    }

    const title = article.titleEn || article.titleAr || 'NGU Research';
    const description = article.summaryEn || article.summaryAr || '';

    return {
        title: `${title} | NGU Research`,
        description,
    };
}

export default async function ArticleDetailsPage({ params }: PageProps) {
    const { id } = await params;
    const article = await getResearchArticleById(id);

    if (!article) {
        return notFound();
    }

    const allArticles = await getResearchArticles();
    const relatedArticles = allArticles
        .filter(a => a.id !== id && a.categoryEn === article.categoryEn)
        .slice(0, 3);

    return <ArticleDetailsContent article={article} relatedArticles={relatedArticles} />;
}
