import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

/**
 * Format date to DD/MM/YYYY format consistently across the application
 * @param date - Date string (ISO 8601) or Date object
 * @param defaultValue - Value to return if date is invalid (default: '-')
 * @returns Formatted date string in DD/MM/YYYY format
 */
export function formatDateDDMMYYYY(date: string | Date | undefined | null, defaultValue = '-'): string {
  if (!date) return defaultValue;
  
  try {
    const dateObj = typeof date === 'string' ? new Date(date) : date;
    
    // Check if date is valid
    if (isNaN(dateObj.getTime())) return defaultValue;
    
    // Get date components
    const day = String(dateObj.getDate()).padStart(2, '0');
    const month = String(dateObj.getMonth() + 1).padStart(2, '0');
    const year = dateObj.getFullYear();
    
    return `${day}/${month}/${year}`;
  } catch {
    return defaultValue;
  }
}

/**
 * Format date and time to DD/MM/YYYY HH:mm format
 * @param date - Date string (ISO 8601) or Date object
 * @param defaultValue - Value to return if date is invalid (default: '-')
 * @returns Formatted date-time string
 */
export function formatDateTimeDDMMYYYY(date: string | Date | undefined | null, defaultValue = '-'): string {
  if (!date) return defaultValue;
  
  try {
    const dateObj = typeof date === 'string' ? new Date(date) : date;
    
    // Check if date is valid
    if (isNaN(dateObj.getTime())) return defaultValue;
    
    // Get date and time components
    const day = String(dateObj.getDate()).padStart(2, '0');
    const month = String(dateObj.getMonth() + 1).padStart(2, '0');
    const year = dateObj.getFullYear();
    const hours = String(dateObj.getHours()).padStart(2, '0');
    const minutes = String(dateObj.getMinutes()).padStart(2, '0');
    
    return `${day}/${month}/${year} ${hours}:${minutes}`;
  } catch {
    return defaultValue;
  }
}
