// ui/src/utils/formatters.js

export const formatDate = (dateString) => {
  if (!dateString) return 'N/A';
  const date = new Date(dateString);
  // Using UTC methods for consistency if dates are stored in UTC
  // For simple date, local might be fine, but UTC is safer for consistency across server/client
  const day = String(date.getUTCDate()).padStart(2, '0');
  const month = String(date.getUTCMonth() + 1).padStart(2, '0'); // Months are 0-indexed
  const year = date.getUTCFullYear();
  return `${day}/${month}/${year}`;
};

export const formatDateTime = (dateTimeString) => {
  if (!dateTimeString) return 'N/A';
  const date = new Date(dateTimeString);
  const day = String(date.getUTCDate()).padStart(2, '0');
  const month = String(date.getUTCMonth() + 1).padStart(2, '0');
  const year = date.getUTCFullYear();
  const hours = String(date.getUTCHours()).padStart(2, '0');
  const minutes = String(date.getUTCMinutes()).padStart(2, '0');
  return `${day}/${month}/${year} ${hours}:${minutes}`;
};

export const formatCurrency = (value, currency = 'PYG') => {
  if (value == null || isNaN(Number(value))) return 'N/A';
  return Number(value).toLocaleString('es-PY', { // Default to Paraguayan Guarani
    style: 'currency',
    currency: currency,
    minimumFractionDigits: 0, // Assuming whole numbers for PYG primarily
    maximumFractionDigits: 0, // Adjust if decimals are common for the currency
  });
};

export const truncateText = (text, maxLength = 50) => {
  if (!text) return 'N/A';
  if (text.length <= maxLength) return text;
  return text.substring(0, maxLength) + '...';
};

// Generic status class generator
// It can be expanded with more specific module-based color themes if needed later
const defaultStatusStyles = {
  base: 'px-2.5 py-0.5 rounded-full text-xs font-semibold',
  yellow: 'bg-yellow-100 text-yellow-800',
  green: 'bg-green-100 text-green-800',
  red: 'bg-red-100 text-red-800',
  blue: 'bg-blue-100 text-blue-800',
  gray: 'bg-gray-100 text-gray-800',
};

// Status mappings for different modules/contexts can be centralized here
// For now, using a general mapping based on common terms observed
const commonStatusMap = {
  // Asistencias
  'pendiente': defaultStatusStyles.yellow,
  'presente': defaultStatusStyles.green,
  'confirmada': defaultStatusStyles.green,
  'ausente': defaultStatusStyles.red,
  'justificada': defaultStatusStyles.blue,
  'cancelada': defaultStatusStyles.gray, // Could be red too, gray for less severe
  'anulada': defaultStatusStyles.gray,   // Same as cancelada

  // Planillas
  'pagado': defaultStatusStyles.green,
  // 'pendiente' is already mapped
  // 'anulado' is already mapped

  // Tareas
  // 'pendiente' is already mapped
  'realizada': defaultStatusStyles.green,
  'completada': defaultStatusStyles.green,
  'hecho': defaultStatusStyles.green,
  'en-progreso': defaultStatusStyles.blue,
  // 'anulada' is already mapped
  // 'cancelada' is already mapped
};

export const getStatusClass = (status) => {
  const baseClasses = defaultStatusStyles.base;
  if (!status) return `${baseClasses} ${defaultStatusStyles.gray}`;

  const lowerStatus = String(status).toLowerCase().replace(/\s+/g, '-');
  const style = commonStatusMap[lowerStatus] || defaultStatusStyles.gray;

  return `${baseClasses} ${style}`;
};

// Example of how it could be more module specific if needed:
/*
export const getAsistenciaStatusClass = (status) => {
  const baseClasses = defaultStatusStyles.base;
  // ... Asistencia specific logic ...
  return `${baseClasses} ${specificStyle}`;
}
*/
