import { checkAdminAuth } from '@/lib/auth'; import { getAllResponses, getSurveyStats } from '@/lib/survey'; import { NextRequest, NextResponse } from 'next/server'; export async function POST(request: NextRequest) { try { const body = await request.json(); const { password } = body; // Check for admin auth const isAuthenticated = await checkAdminAuth(password); if (!isAuthenticated) { return NextResponse.json( { error: 'Unauthorized' }, { status: 401 } ); } // Get survey statistics and all responses const stats = getSurveyStats(); const responses = getAllResponses(); // Filter out responses with comments, but don't include IDs to ensure anonymity const comments = responses .filter(response => response.comment && response.comment.trim() !== '') .map(response => ({ vote: response.vote, comment: response.comment, timestamp: response.timestamp })); return NextResponse.json({ stats, comments }); } catch (error) { console.error('Error getting stats:', error); return NextResponse.json( { error: 'Failed to get survey statistics' }, { status: 500 } ); } }