/**
 * GET /api/admin/errors — recent error logs.
 */
import { NextResponse } from "next/server";
import { requireAdmin } from "@/lib/admin-auth";
import { prisma } from "@/lib/prisma";

export const dynamic = "force-dynamic";

export async function GET(req: Request) {
  const auth = await requireAdmin();
  if (!auth.ok) return NextResponse.json({ error: "Forbidden" }, { status: auth.status });
  const { searchParams } = new URL(req.url);
  const source = searchParams.get("source");
  const limit = Math.max(1, Math.min(200, Number(searchParams.get("limit") || 50)));
  const where: Record<string, unknown> = { level: "error" };
  if (source) where.source = source;

  const logs = await prisma.platformLog.findMany({ where, orderBy: { timestamp: "desc" }, take: limit });
  return NextResponse.json({ errors: logs });
}
