import { NextRequest, NextResponse } from 'next/server';

import { requireSuperAdmin, withAuth } from '@/lib/api/helpers';
import { errorResponse } from '@/lib/errors';
import { getRecentErrors } from '@/lib/services/system-monitoring.service';

export const GET = withAuth(async (req: NextRequest, ctx) => {
  try {
    requireSuperAdmin(ctx, 'admin.errors.list');

    const url = new URL(req.url);
    const limit = Math.min(100, Math.max(1, parseInt(url.searchParams.get('limit') ?? '20', 10)));

    const errors = await getRecentErrors(limit);
    return NextResponse.json({ data: errors });
  } catch (error) {
    return errorResponse(error);
  }
});
