// Admin-only pages: show the real database error instead of a blank
// "page couldn't load", so a missing table or collation clash can be fixed
// from the message (the text is only ever rendered to signed-in admins).
export function dbErrorMessage(err: unknown): string {
  const e = err as { message?: string; cause?: { message?: string; code?: string; sqlMessage?: string } };
  return e?.cause?.sqlMessage || e?.cause?.message || e?.message || String(err);
}

export default function AdminDbError({ what, error }: { what: string; error: string }) {
  const missing = /doesn't exist|no such table|ER_NO_SUCH_TABLE/i.test(error);
  return (
    <div className="space-y-2 rounded-lg p-4 text-sm pill-danger">
      <p className="font-semibold">Couldn&apos;t load {what} — the database returned an error:</p>
      <pre className="whitespace-pre-wrap break-all font-mono text-xs">{error}</pre>
      {missing && (
        <p>
          A table is missing, so the merch database update (migration 0008) wasn&apos;t fully applied. Send this
          message to your developer.
        </p>
      )}
    </div>
  );
}
