/**
 * GET /api/admin/audits/[id]
 *
 * Returns the full Audit record including live progress fields.
 * Polled by the report UI every 2.5s while status="running".
 */

import { NextResponse } from "next/server";
import { requireSuperadmin } from "@/lib/admin-auth";
import { prisma } from "@/lib/prisma";

export const dynamic = "force-dynamic";

export async function GET(
  _req: Request,
  { params }: { params: Promise<{ id: string }> },
) {
  const denied = await requireSuperadmin();
  if (denied) return denied;

  const { id } = await params;

  const audit = await prisma.audit.findUnique({
    where: { id },
  }).catch(() => null);

  if (!audit) {
    return NextResponse.json({ error: "Audit not found" }, { status: 404 });
  }

  return NextResponse.json({ audit });
}