import { NextRequest, NextResponse } from 'next/server'; import { cookies } from 'next/headers'; import { db } from '@/lib/db'; export const dynamic = 'force-dynamic'; export async function GET(request: NextRequest) { try { const userId = cookies().get('userId')?.value; if (!userId) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } // Get user with plan info const user = await db.user.findUnique({ where: { id: userId }, select: { plan: true, }, }); if (!user) { return NextResponse.json({ error: 'User not found' }, { status: 404 }); } // Count dynamic QR codes const dynamicQRCount = await db.qRCode.count({ where: { userId, type: 'DYNAMIC', }, }); // Count static QR codes const staticQRCount = await db.qRCode.count({ where: { userId, type: 'STATIC', }, }); // Determine limits based on plan let dynamicLimit = 3; // FREE plan default if (user.plan === 'PRO') { dynamicLimit = 50; } else if (user.plan === 'BUSINESS') { dynamicLimit = 500; } return NextResponse.json({ dynamicUsed: dynamicQRCount, dynamicLimit, staticUsed: staticQRCount, }); } catch (error) { console.error('Error fetching user stats:', error); return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ); } }