import { getPayload } from 'payload' import configPromise from '@payload-config' import { TOPICS, topicsFor, type TopicKey } from '@/lib/topics' export type IndexDoc = { id: number | string title: string slug: string dek: string access: 'free' | 'vip' category: string level: string publishedAt: string | null updatedAt: string | null /** Present only when requested via fetchIndexDocs(true). */ content?: unknown } /** Published posts, public read path — no overrideAccess (drafts stay hidden, * and the content field's own read ACL decides body availability). */ export async function fetchIndexDocs(withContent = false): Promise { const payload = await getPayload({ config: configPromise }) const { docs } = await payload.find({ collection: 'posts', where: { status: { equals: 'published' } }, depth: 0, limit: 500, sort: '-publishedAt', select: { title: true, slug: true, dek: true, access: true, category: true, level: true, publishedAt: true, updatedAt: true, ...(withContent ? { content: true } : {}), }, }) return docs as unknown as IndexDoc[] } export const SITE_TITLE = 'أنور بآي — أدلة الذكاء الاصطناعي العملي بالعربية' export const SITE_INTRO = 'دليلك العربي للذكاء الاصطناعي العملي: وكلاء، نماذج، وأدوات.' /** Topic-grouped markdown index lines; a post can appear under several topics. */ export function renderIndexSections(docs: IndexDoc[], markVip: boolean): string { const lines: string[] = [] for (const topic of TOPICS) { const items = docs.filter(doc => topicsFor(doc).includes(topic.key)) if (items.length === 0) continue lines.push(`## ${topic.label}`, '') for (const doc of items) { const suffix = markVip && doc.access === 'vip' ? ' (دليل للأعضاء)' : '' lines.push(`- [${doc.title}${suffix}](/posts/${doc.slug}): ${doc.dek}`) } lines.push('') } return lines.join('\n') }