import { NextRequest, NextResponse } from 'next/server'; import { analyzeSite, type SiteAnalysis } from '@/lib/site-analysis'; // ─── Summary Builder ────────────────────────────────────────────────────────── function buildAnalysisSummary(a: SiteAnalysis): string { if (a.fetchError) return "I wasn't able to reach that site to analyze it."; const parts: string[] = []; if (a.techStack?.cms) parts.push(`it's built on ${a.techStack.cms}`); if (a.techStack?.framework) parts.push(`using ${a.techStack.framework}`); if (a.techStack?.ecommerce) parts.push(`with ${a.techStack.ecommerce} for e-commerce`); if (a.performance) { const score = a.performance.score; const quality = score >= 90 ? 'excellent' : score >= 50 ? 'moderate' : 'low'; parts.push(`the mobile performance score is ${score} out of 100, which is ${quality}`); } if (a.techStack?.hosting) parts.push(`hosted on ${a.techStack.hosting}`); if (a.hasForms) parts.push('it has contact forms'); if (a.techStack?.analytics && a.techStack.analytics.length > 0) { parts.push(`using ${a.techStack.analytics.join(' and ')} for analytics`); } if (parts.length === 0) { return "I was able to fetch the site but couldn't determine much about its technology stack."; } return `Here's what I found: ${parts.join(', ')}.`; } // ─── Route Handler ──────────────────────────────────────────────────────────── export async function POST(request: NextRequest) { try { const { url } = (await request.json()) as { url?: string }; if (!url?.trim()) { return NextResponse.json({ success: false, error: 'URL required' }, { status: 400 }); } const analysis = await analyzeSite(url.trim()); const summary = buildAnalysisSummary(analysis); return NextResponse.json({ success: true, summary, analysis }); } catch (error) { console.error('[analyze-site] Failed:', error); return NextResponse.json({ success: false, error: 'Analysis failed' }, { status: 500 }); } }