export type EmotionTheme = {
  gradientClass: string;
  gradientBackground: string;
  suggestionGradient: string;
  suggestionBorder: string;
};

export type EmotionIdentity = {
  slug?: string | null;
  name?: string | null;
};

const themeFreude: EmotionTheme = {
  gradientClass: 'from-amber-300 via-orange-400 to-rose-500',
  gradientBackground: 'linear-gradient(135deg, #fde68a, #fb923c, #fb7185)',
  suggestionGradient: 'from-amber-50 via-white to-orange-50 dark:from-amber-500/20 dark:via-gray-900 dark:to-orange-500/10',
  suggestionBorder: 'border-amber-200/60 dark:border-amber-400/30',
};

const themeLiebe: EmotionTheme = {
  gradientClass: 'from-rose-400 via-fuchsia-500 to-purple-600',
  gradientBackground: 'linear-gradient(135deg, #fb7185, #ec4899, #9333ea)',
  suggestionGradient: 'from-rose-50 via-white to-fuchsia-50 dark:from-rose-500/20 dark:via-gray-900 dark:to-fuchsia-500/10',
  suggestionBorder: 'border-rose-200/60 dark:border-rose-400/30',
};

const themeEkstase: EmotionTheme = {
  gradientClass: 'from-fuchsia-400 via-purple-500 to-indigo-500',
  gradientBackground: 'linear-gradient(135deg, #f472b6, #a855f7, #6366f1)',
  suggestionGradient: 'from-fuchsia-50 via-white to-indigo-50 dark:from-fuchsia-500/20 dark:via-gray-900 dark:to-indigo-500/10',
  suggestionBorder: 'border-fuchsia-200/60 dark:border-fuchsia-400/30',
};

const themeEntspannt: EmotionTheme = {
  gradientClass: 'from-teal-300 via-emerald-400 to-cyan-500',
  gradientBackground: 'linear-gradient(135deg, #5eead4, #34d399, #22d3ee)',
  suggestionGradient: 'from-teal-50 via-white to-emerald-50 dark:from-teal-500/20 dark:via-gray-900 dark:to-emerald-500/10',
  suggestionBorder: 'border-emerald-200/60 dark:border-emerald-400/30',
};

const themeBesinnlich: EmotionTheme = {
  gradientClass: 'from-slate-500 via-blue-500 to-indigo-600',
  gradientBackground: 'linear-gradient(135deg, #64748b, #3b82f6, #4f46e5)',
  suggestionGradient: 'from-slate-50 via-white to-blue-50 dark:from-slate-600/20 dark:via-gray-900 dark:to-blue-500/10',
  suggestionBorder: 'border-slate-200/60 dark:border-slate-500/30',
};

const themeUeberraschung: EmotionTheme = {
  gradientClass: 'from-indigo-300 via-violet-500 to-rose-500',
  gradientBackground: 'linear-gradient(135deg, #a5b4fc, #a855f7, #fb7185)',
  suggestionGradient: 'from-indigo-50 via-white to-violet-50 dark:from-indigo-500/20 dark:via-gray-900 dark:to-violet-500/10',
  suggestionBorder: 'border-indigo-200/60 dark:border-indigo-400/30',
};

const themeDefault: EmotionTheme = {
  gradientClass: 'from-pink-500 via-purple-500 to-indigo-600',
  gradientBackground: 'linear-gradient(135deg, #ec4899, #a855f7, #4f46e5)',
  suggestionGradient: 'from-pink-50 via-white to-indigo-50 dark:from-pink-500/20 dark:via-gray-900 dark:to-indigo-500/10',
  suggestionBorder: 'border-pink-200/60 dark:border-pink-400/30',
};

const EMOTION_THEMES: Record<string, EmotionTheme> = {
  freude: themeFreude,
  happy: themeFreude,
  liebe: themeLiebe,
  romance: themeLiebe,
  romantik: themeLiebe,
  nostalgie: themeEntspannt,
  relaxed: themeEntspannt,
  ruehrung: themeBesinnlich,
  traurigkeit: themeBesinnlich,
  teamgeist: themeFreude,
  gemeinschaft: themeFreude,
  ueberraschung: themeUeberraschung,
  surprise: themeUeberraschung,
  ekstase: themeEkstase,
  excited: themeEkstase,
  besinnlichkeit: themeBesinnlich,
  sad: themeBesinnlich,
  default: themeDefault,
};

const EMOTION_ICONS: Record<string, string> = {
  freude: '😊',
  happy: '😊',
  liebe: '❤️',
  romantik: '💞',
  nostalgie: '📼',
  ruehrung: '🥲',
  teamgeist: '🤝',
  ueberraschung: '😲',
  surprise: '😲',
  ekstase: '🤩',
  besinnlichkeit: '🕯️',
};

function sluggify(value?: string | null): string {
  return (value ?? '')
    .toString()
    .toLowerCase()
    .normalize('NFD')
    .replace(/[\u0300-\u036f]/g, '')
    .replace(/[^a-z0-9]+/g, '')
    .trim();
}

function resolveEmotionKey(identity?: EmotionIdentity | null): string {
  if (!identity) return 'default';
  const nameKey = sluggify(identity.name);
  if (nameKey && EMOTION_THEMES[nameKey]) {
    return nameKey;
  }
  const slugKey = sluggify(identity.slug);
  if (slugKey && EMOTION_THEMES[slugKey]) {
    return slugKey;
  }
  return 'default';
}

export function getEmotionTheme(identity?: EmotionIdentity | null): EmotionTheme {
  const key = resolveEmotionKey(identity);
  return EMOTION_THEMES[key] ?? themeDefault;
}

export function getEmotionIcon(identity?: EmotionIdentity | null): string {
  const key = resolveEmotionKey(identity);
  return EMOTION_ICONS[key] ?? '✨';
}
