import type { Metadata } from 'next' import Link from 'next/link' import { notFound } from 'next/navigation' import { headers } from 'next/headers' import { getPayload } from 'payload' import configPromise from '@payload-config' import { TOPICS, topicsFor, isTopic } from '@/lib/topics' import { guidesLabel, minutesLabel, formatNumber, contentTypeLabel } from '@/lib/format' import { freeEarlyAccess } from '@/lib/launch' import Topnav from '@/components/topnav' import Footer from '@/components/footer' type Args = { params: Promise<{ topic: string }> } type HubPost = { id: number title: string slug: string dek: string access: 'free' | 'vip' category: string level: string readingMinutes: number views: number cover?: { url?: string | null } | number | null contentType?: string | null } const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL ?? 'https://anwarpi.ai' export async function generateMetadata({ params }: Args): Promise { const { topic } = await params if (!isTopic(topic)) return { title: 'أنور بآي' } const meta = TOPICS.find(t => t.key === topic) return { title: `${meta?.label} — مسار`, description: `كل أدلة مسار ${meta?.label} في أنور بآي: ${meta?.hint}. أدلة عملية بالعربية بخطوات قابلة للتطبيق.`, alternates: { canonical: `/topics/${topic}` }, } } export default async function TopicPage({ params }: Args) { const { topic } = await params if (!isTopic(topic)) notFound() const meta = TOPICS.find(t => t.key === topic)! const payload = await getPayload({ config: configPromise }) const [res, auth] = await Promise.all([ payload.find({ collection: 'posts', where: { status: { equals: 'published' } }, depth: 1, limit: 200, sort: '-publishedAt', select: { title: true, slug: true, dek: true, access: true, category: true, level: true, readingMinutes: true, views: true, cover: true, contentType: true, }, }), payload.auth({ headers: await headers() }), ]) const role = (auth.user?.role as string) || null // Topic membership is derived (category + level), same rule as the library/home. const posts = (res.docs as unknown as (HubPost & { likes?: number })[]).filter(post => topicsFor(post).includes(topic)) const totalMinutes = posts.reduce((sum, post) => sum + (post.readingMinutes ?? 0), 0) return (
{/* L-09: Breadcrumb structured data mirrors the visible trail. */}