/**
 * Build a Razorpay `receipt` value that always satisfies Razorpay's 40-character
 * limit. Razorpay rejects longer receipts with
 *   "receipt: the length must be no more than 40."
 *
 * The receipt is a merchant-side reference only — it does NOT need to encode the
 * full entity ids (those are passed in the order's `notes`, which has no such
 * limit). We include a short recognizable tail of the id plus a compact unique
 * token (base36 timestamp + random) for traceability and uniqueness.
 *
 * @param prefix short category prefix, e.g. 'fee' | 'inv' | 'prod' | 'transport'
 * @param id     optional entity id (only the last 6 chars are used)
 */
export function buildRazorpayReceipt(prefix: string, id?: string): string {
    const MAX = 40;
    const tail = id ? String(id).slice(-6) : '';
    const token = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
    // Compose, then hard-cap at MAX as a defensive guarantee regardless of inputs.
    const parts = [prefix, tail, token].filter(Boolean);
    return parts.join('_').slice(0, MAX);
}
