/**
 * Centralized date and number formatters using English numerals only
 * Ensures consistent formatting across server and client (prevents hydration mismatches)
 */

import { formatDateDDMMYYYY, formatDateTimeDDMMYYYY } from '@/lib/utils';

/**
 * Format a date string to DD/MM/YYYY format
 * Uses centralized formatDateDDMMYYYY from utils for consistency
 */
export function formatDateStable(value: string | undefined): string {
  return formatDateDDMMYYYY(value);
}

/**
 * Format a date string to DD/MM/YYYY HH:mm:ss format
 * Uses centralized formatDateTimeDDMMYYYY from utils for consistency
 */
export function formatDateTimeStable(value: string | undefined): string {
  return formatDateTimeDDMMYYYY(value);
}

/**
 * Format a date string to HH:mm:ss or HH:mm format
 * Safe for SSR as it always returns the same output regardless of locale
 */
export function formatTimeStable(value: string | undefined, includeSeconds: boolean = false): string {
  if (!value) return '-';
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return '-';
  const isoString = date.toISOString();
  if (includeSeconds) {
    return isoString.slice(11, 19); // HH:mm:ss
  } else {
    return isoString.slice(11, 16); // HH:mm
  }
}

/**
 * Format a number with English numerals only
 * Uses Intl.NumberFormat with en-US locale to ensure consistent decimal/thousand separators
 */
export function formatNumberStable(
  value: number | undefined,
  options: {
    minimumFractionDigits?: number;
    maximumFractionDigits?: number;
    useGrouping?: boolean;
  } = {}
): string {
  if (value === undefined || value === null) return '-';
  
  const { minimumFractionDigits = 0, maximumFractionDigits = 3, useGrouping = true } = options;
  
  return new Intl.NumberFormat('en-US', {
    minimumFractionDigits,
    maximumFractionDigits,
    useGrouping,
  }).format(value);
}

/**
 * Format a currency value with English numerals only
 * Uses the specified currency code (default: EGP for Egyptian Pound)
 */
export function formatCurrencyStable(
  value: number | undefined,
  currencyCode: string = 'EGP',
  minimumFractionDigits: number = 2,
  maximumFractionDigits: number = 2
): string {
  if (value === undefined || value === null) return '-';
  
  return new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: currencyCode,
    minimumFractionDigits,
    maximumFractionDigits,
  }).format(value);
}

/**
 * Format a percentage value with English numerals only
 */
export function formatPercentageStable(
  value: number | undefined,
  minimumFractionDigits: number = 0,
  maximumFractionDigits: number = 2
): string {
  if (value === undefined || value === null) return '-';
  
  return new Intl.NumberFormat('en-US', {
    style: 'percent',
    minimumFractionDigits,
    maximumFractionDigits,
  }).format(value / 100); // Convert to decimal for percent format
}
