import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@/lib/supabase/server';
import { requireRmpAdmin } from '@/lib/accounting/rmpService';

export async function GET(request: NextRequest) {
  const auth = await requireRmpAdmin();
  if (!auth) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

  const supabase = await createClient();
  const { searchParams } = new URL(request.url);
  const site = searchParams.get('site') ?? '';

  const now = new Date();
  const monthStart = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-01`;

  let baseQuery = supabase.from('rmp_email_suppressions').select('id, suppression_type, suppressed_at');
  if (site) baseQuery = baseQuery.eq('site', site);

  const { data: all } = await baseQuery;
  const total = (all ?? []).length;
  const added_this_month = (all ?? []).filter((s: any) => s.suppressed_at >= monthStart).length;
  const hostile_replies = (all ?? []).filter((s: any) => s.suppression_type === 'hostile_reply').length;

  return NextResponse.json({ total, added_this_month, hostile_replies });
}
