import type { Contact } from './types'

export function formatMessageTime(timestamp: string): string {
  const date = new Date(timestamp)
  const now = new Date()
  const diffMs = now.getTime() - date.getTime()
  const diffMins = Math.floor(diffMs / 60000)
  const diffHours = Math.floor(diffMs / 3600000)
  const diffDays = Math.floor(diffMs / 86400000)

  if (diffMins < 1) return 'Gerade eben'
  if (diffMins < 60) return `${diffMins} Min.`
  if (diffHours < 24) return `${diffHours} Std.`
  if (diffDays === 1) return 'Gestern'
  if (diffDays < 7) return `${diffDays} Tage`

  return date.toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit' })
}

export function formatMessageDate(timestamp: string): string {
  const date = new Date(timestamp)
  const now = new Date()
  const diffDays = Math.floor((now.getTime() - date.getTime()) / 86400000)

  if (diffDays === 0) return 'Heute'
  if (diffDays === 1) return 'Gestern'
  if (diffDays < 7) {
    return date.toLocaleDateString('de-DE', { weekday: 'long' })
  }

  return date.toLocaleDateString('de-DE', { day: '2-digit', month: 'long', year: 'numeric' })
}

export function getContactInitials(name: string): string {
  const parts = name.split(' ').filter(p => p.length > 0)
  if (parts.length >= 2) {
    return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase()
  }
  return name.slice(0, 2).toUpperCase()
}

export function getRoleLabel(role: Contact['role']): string {
  const labels: Record<Contact['role'], string> = {
    parent: 'Eltern',
    teacher: 'Lehrkraft',
    staff: 'Verwaltung',
    student: 'Schueler/in'
  }
  return labels[role] || role
}

export function getRoleColor(role: Contact['role'], isDark: boolean): string {
  const colors: Record<Contact['role'], { dark: string; light: string }> = {
    parent: { dark: 'bg-blue-500/20 text-blue-300', light: 'bg-blue-100 text-blue-700' },
    teacher: { dark: 'bg-purple-500/20 text-purple-300', light: 'bg-purple-100 text-purple-700' },
    staff: { dark: 'bg-amber-500/20 text-amber-300', light: 'bg-amber-100 text-amber-700' },
    student: { dark: 'bg-green-500/20 text-green-300', light: 'bg-green-100 text-green-700' }
  }
  return isDark ? colors[role].dark : colors[role].light
}

// Emoji categories for picker
export const emojiCategories = {
  'Häufig': ['👍', '❤️', '😊', '😂', '🙏', '👏', '🎉', '✅', '📝', '📚'],
  'Smileys': ['😀', '😃', '😄', '😁', '😅', '😂', '🤣', '😊', '😇', '🙂', '😉', '😌', '😍', '🥰', '😘'],
  'Gesten': ['👍', '👎', '👌', '✌️', '🤞', '🤝', '👏', '🙌', '👋', '✋', '🤚', '🖐️', '🙏'],
  'Symbole': ['❤️', '💙', '💚', '💛', '🧡', '💜', '✅', '❌', '⭐', '🌟', '💯', '📌', '📎'],
  'Schule': ['📚', '📖', '📝', '✏️', '📓', '📕', '📗', '📘', '🎓', '🏫', '📅', '⏰', '🔔']
}
