import { NextResponse } from "next/server";
import { PublicError, publicMessageFor } from "./errors";

export function ok(data: any = {}) {
  return NextResponse.json({ ok: true, ...data });
}

export function fail(err: unknown, opts?: { adminMessage?: string; status?: number; isAdmin?: boolean }) {
  const status = opts?.status ?? 400;

  if (opts?.isAdmin) {
    const msg =
      err instanceof PublicError
        ? err.message
        : err instanceof Error
          ? err.message
          : "Unknown error";
    return NextResponse.json({ ok: false, message: msg }, { status });
  }

  // consumer-safe
  return NextResponse.json({ ok: false, message: publicMessageFor("GENERIC") }, { status });
}
