import { prisma } from '@/lib/db'; import { NextResponse } from 'next/server'; export async function GET() { try { const today = new Date(); today.setHours(0, 0, 0, 0); const [totalArticles, searchesToday, allArticles] = await Promise.all([ prisma.article.count(), prisma.search.count({ where: { createdAt: { gte: today, }, }, }), prisma.article.findMany({ select: { content: true, }, }), ]); const totalWords = allArticles.reduce((sum, article) => { const words = article.content.split(/\s+/).filter(word => word.length > 0).length; return sum + words; }, 0); return NextResponse.json({ totalArticles, searchesToday, totalWords, }); } catch (error) { console.error('Error fetching stats:', error); return NextResponse.json( { error: 'Failed to fetch statistics' }, { status: 500 } ); } }