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

/**
 * Admin API: Get email suppression list and stats.
 * GET /api/admin/email-preferences
 */
export async function GET() {
    const user = await getCurrentUser();
    if (!user || user.role !== 'admin') {
        return NextResponse.json({ error: 'Unauthorized' }, { status: 403 });
    }

    try {
        const [suppressionList, total, unsubscribed, bounced, complaints] = await Promise.all([
            prisma.emailSuppression.findMany({
                orderBy: { suppressed_at: 'desc' },
                take: 200
            }),
            prisma.emailSuppression.count(),
            prisma.emailSuppression.count({ where: { reason: 'unsubscribe' } }),
            prisma.emailSuppression.count({ where: { reason: 'bounce' } }),
            prisma.emailSuppression.count({ where: { reason: 'complaint' } }),
        ]);

        return NextResponse.json({
            suppressionList,
            stats: { total, unsubscribed, bounced, complaints }
        });
    } catch (error: any) {
        return NextResponse.json({ error: error.message }, { status: 500 });
    }
}
