/**
 * POST /api/public-audit/track — lightweight client-side event sink.
 * Used by the public audit result page to record locked-panel impressions.
 */

import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";

export const dynamic = "force-dynamic";

export async function POST(req: Request) {
  let body: { auditId?: string; event?: string };
  try {
    body = await req.json();
  } catch {
    return NextResponse.json({ ok: false }, { status: 400 });
  }

  const { auditId, event } = body;
  if (!auditId || !event) return NextResponse.json({ ok: false }, { status: 400 });

  // Fire-and-forget — don't block the response
  prisma.platformEvent.create({
    data: {
      userId: null,
      brandId: null,
      action: `public_audit.${event}`,
      category: "audit",
      metadata: { auditId },
    },
  }).catch(() => {/* non-fatal */});

  return NextResponse.json({ ok: true });
}
