/**
 * Format an ID with prefix (2 letters + 6 digits)
 * 
 * @param id - The ID to format
 * @param prefix - The 2-letter prefix (e.g., 'QT', 'CO')
 * @returns Formatted ID (e.g., "QT000001") or 'N/A' if id is null/undefined
 */
export function formatId(id: number | string | null | undefined, prefix: string | null | undefined = ''): string {
  if (id === null || id === undefined || id === '') {
    return 'N/A';
  }

  // Ensure prefix is exactly 2 characters, uppercase, with fallback to empty string
  const safePrefix = prefix || '';
  const formattedPrefix = safePrefix ? String(safePrefix).toUpperCase().substring(0, 2).padEnd(2, ' ') : '';

  // Format ID as 6 digits with leading zeros
  const numericId = typeof id === 'string' ? parseInt(id, 10) : id;
  if (isNaN(numericId)) {
    return String(id);
  }

  const formattedId = String(numericId).padStart(6, '0');

  return formattedPrefix + formattedId;
}

/**
 * Format quote ID
 */
export function formatQuoteId(id: number | string | null | undefined, prefix: string = 'QT'): string {
  return formatId(id, prefix);
}

/**
 * Format order ID
 */
export function formatOrderId(id: number | string | null | undefined, prefix: string = 'CO'): string {
  return formatId(id, prefix);
}

/**
 * Format cart ID
 */
export function formatCartId(id: number | string | null | undefined, prefix: string = 'CC'): string {
  return formatId(id, prefix);
}
