import { NextResponse } from "next/server";

function isRecord(v: unknown): v is Record<string, unknown> {
  return v !== null && typeof v === "object";
}

/** P2025 : enregistrement introuvable pour update/delete avec `where` unique. */
const PRISMA_NOT_FOUND = "P2025";

export function prismaMutationErrorResponse(error: unknown): NextResponse {
  if (isRecord(error) && typeof error.code === "string") {
    if (error.code === PRISMA_NOT_FOUND) {
      return NextResponse.json({ error: "Introuvable" }, { status: 404 });
    }
  }
  console.error("[api] Prisma", error);
  return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
}
