export function formatDate(date: Date | string): string {
  const d = typeof date === 'string' ? new Date(date) : date;
  
  if (isNaN(d.getTime())) {
    return 'Ungültiges Datum';
  }
  
  const day = d.getDate().toString().padStart(2, '0');
  const month = (d.getMonth() + 1).toString().padStart(2, '0');
  const year = d.getFullYear();
  
  return `${day}.${month}.${year}`;
}

export function formatDateTime(date: Date | string): string {
  const d = typeof date === 'string' ? new Date(date) : date;
  
  if (isNaN(d.getTime())) {
    return 'Ungültiges Datum';
  }
  
  const dateStr = formatDate(d);
  const hours = d.getHours().toString().padStart(2, '0');
  const minutes = d.getMinutes().toString().padStart(2, '0');
  
  return `${dateStr}, ${hours}:${minutes}`;
}

export function parseDateString(dateString: string): Date | null {
  if (!dateString) return null;

  // Try German format: DD.MM.YYYY or D.M.YYYY
  const germanMatch = dateString.match(/(\d{1,2})\.(\d{1,2})\.(\d{4})/);
  if (germanMatch) {
    const [, day, month, year] = germanMatch;
    return new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
  }

  // Try ISO format or other standard formats
  const date = new Date(dateString);
  return isNaN(date.getTime()) ? null : date;
}
