import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';

export async function GET() {
  try {
    const [totalLeads, contactedLeads, interestedLeads, recentActivity] = await Promise.all([
      prisma.lead.count(),
      prisma.lead.count({ where: { status: 'CONTACTED' } }),
      prisma.lead.count({ where: { status: 'INTERESTED' } }),
      prisma.activityLog.findMany({
        orderBy: { createdAt: 'desc' },
        take: 50
      })
    ]);

    return NextResponse.json({
      stats: {
        totalLeads,
        contactedLeads,
        interestedLeads,
        recentActivity,
        conversionRate: totalLeads > 0 ? ((interestedLeads / totalLeads) * 100).toFixed(1) : 0
      }
    });
  } catch (error) {
    console.error('Failed to fetch dashboard stats', error);
    return NextResponse.json({ error: 'Failed to fetch dashboard stats' }, { status: 500 });
  }
}
