// file: app/research/[slug]/page.tsx

import { getPostBySlug, getPostSlugs } from '@/lib/mdx';
import { notFound } from 'next/navigation';
import { getPublicationsData } from '@/lib/publications';
import { Article } from '@/components/page-article';
import { DATA } from '@/app/resume';
import { Props } from '@/components/types';

export async function generateStaticParams() {
  const slugs = getPostSlugs('research');
  return slugs.map((slug) => ({ slug }));
}

export async function generateMetadata({ params }: Props ) {
  const { slug } = await params;

  const post = await getPostBySlug('research', slug);
  if (!post) { return {}; }
  return {
    title: post.frontmatter.title,
    description: post.frontmatter.teaser || DATA.description,
  };
}

export default async function ResearchPage({ params }: Props ) {
  const { slug } = await params;

  const post = await getPostBySlug('research', slug);
  const publications = getPublicationsData();

  if (!post) {
    notFound();
  }

  // --- Navigation Logic ---
  const allSlugs = getPostSlugs('research');
  const currentIndex = allSlugs.findIndex((s) => s === slug);
  const prevSlug = currentIndex > 0 ? allSlugs[currentIndex - 1] : null;
  const nextSlug = currentIndex < allSlugs.length - 1 ? allSlugs[currentIndex + 1] : null;

  const prevPost = prevSlug ? await getPostBySlug('research', prevSlug) : null;
  const nextPost = nextSlug ? await getPostBySlug('research', nextSlug) : null;

  const navigation = {
    prev: prevPost ? { slug: prevSlug!, title: prevPost.frontmatter.title } : null,
    next: nextPost ? { slug: nextSlug!, title: nextPost.frontmatter.title } : null,
  };

  return <Article post={post} publications={publications} navigation={navigation} basePath="research" />;
}