// ---------------------------------------------------------------------------
// Date / time formatting
// ---------------------------------------------------------------------------
// All helpers pin the timezone to America/Asuncion so the output is identical
// on the server (often UTC) and the client (user's local TZ).  This prevents
// React hydration mismatches like "07:20 PM" (server) vs "04:20 PM" (client).
// ---------------------------------------------------------------------------

const EVENT_TIMEZONE = 'America/Asuncion';

type Locale = 'en' | 'es';

function pickLocale(locale: Locale): string {
  return locale === 'es' ? 'es-ES' : 'en-US';
}

/**
 * "Sat, Feb 14" / "sáb, 14 feb"
 */
export function formatDateShort(dateStr: string, locale: Locale = 'en'): string {
  return new Date(dateStr).toLocaleDateString(pickLocale(locale), {
    weekday: 'short',
    month: 'short',
    day: 'numeric',
    timeZone: EVENT_TIMEZONE,
  });
}

/**
 * "Saturday, February 14, 2026" / "sábado, 14 de febrero de 2026"
 */
export function formatDateLong(dateStr: string, locale: Locale = 'en'): string {
  return new Date(dateStr).toLocaleDateString(pickLocale(locale), {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    timeZone: EVENT_TIMEZONE,
  });
}

/**
 * "February 14, 2026" / "14 de febrero de 2026"  (no weekday)
 */
export function formatDateMedium(dateStr: string, locale: Locale = 'en'): string {
  return new Date(dateStr).toLocaleDateString(pickLocale(locale), {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    timeZone: EVENT_TIMEZONE,
  });
}

/**
 * "Feb 14, 2026" / "14 feb 2026"
 */
export function formatDateCompact(dateStr: string, locale: Locale = 'en'): string {
  return new Date(dateStr).toLocaleDateString(pickLocale(locale), {
    month: 'short',
    day: 'numeric',
    year: 'numeric',
    timeZone: EVENT_TIMEZONE,
  });
}

/**
 * "04:30 PM" / "16:30"
 */
export function formatTime(dateStr: string, locale: Locale = 'en'): string {
  return new Date(dateStr).toLocaleTimeString(pickLocale(locale), {
    hour: '2-digit',
    minute: '2-digit',
    timeZone: EVENT_TIMEZONE,
  });
}

/**
 * "Feb 14, 2026, 04:30 PM" — compact date + time combined
 */
export function formatDateTime(dateStr: string, locale: Locale = 'en'): string {
  return new Date(dateStr).toLocaleString(pickLocale(locale), {
    month: 'short',
    day: 'numeric',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    timeZone: EVENT_TIMEZONE,
  });
}

/**
 * "Sat, Feb 14, 04:30 PM" — short date + time combined
 */
export function formatDateTimeShort(dateStr: string, locale: Locale = 'en'): string {
  return new Date(dateStr).toLocaleString(pickLocale(locale), {
    weekday: 'short',
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    timeZone: EVENT_TIMEZONE,
  });
}

// ---------------------------------------------------------------------------
// Price formatting
// ---------------------------------------------------------------------------

/**
 * Format price - shows decimals only if needed
 * Uses space as thousands separator (common in Paraguay)
 * Examples:
 *   45000 PYG -> "45 000 PYG" (no decimals)
 *   41.44 PYG -> "41,44 PYG" (with decimals)
 */
export function formatPrice(price: number, currency: string = 'PYG'): string {
  const hasDecimals = price % 1 !== 0;
  
  // Format the integer and decimal parts separately
  const intPart = Math.floor(Math.abs(price));
  const decPart = Math.abs(price) - intPart;
  
  // Format integer part with space as thousands separator
  const intFormatted = intPart.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
  
  // Build final string
  let result = price < 0 ? '-' : '';
  result += intFormatted;
  
  // Add decimals only if present
  if (hasDecimals) {
    const decStr = decPart.toFixed(2).substring(2); // Get just the decimal digits
    result += ',' + decStr;
  }
  
  return `${result} ${currency}`;
}

/**
 * Format currency amount (alias for formatPrice for backward compatibility)
 */
export function formatCurrency(amount: number, currency: string = 'PYG'): string {
  return formatPrice(amount, currency);
}
