/**
 * Badge Definitions
 *
 * Complete list of 20+ achievement badges available in the platform.
 * Badges are organized by category: Exercises, Modules, Streaks, Ranking, Special
 */

import {
  AchievementCategory,
  AchievementRarity,
  RequirementType,
} from '@prisma/client';

// ============================================
// TYPES
// ============================================

export interface BadgeDefinition {
  code: string;
  name: string;
  description: string;
  category: AchievementCategory;
  rarity: AchievementRarity;
  icon: string;
  requirementType: RequirementType;
  requirementValue: number;
  points: number;
  metadata?: {
    color?: string;
    animation?: string;
    tooltip?: string;
    unlockMessage?: string;
  };
}

export interface BadgeGroup {
  category: AchievementCategory;
  categoryName: string;
  badges: BadgeDefinition[];
}

// ============================================
// BADGE DEFINITIONS
// ============================================

export const BADGE_DEFINITIONS: BadgeDefinition[] = [
  // ==========================================
  // EXERCISE BADGES
  // ==========================================
  {
    code: 'FIRST_EXERCISE',
    name: 'Primer Paso',
    description: 'Completa tu primer ejercicio',
    category: AchievementCategory.EXERCISES,
    rarity: AchievementRarity.COMMON,
    icon: '🎯',
    requirementType: RequirementType.EXERCISES_COMPLETED,
    requirementValue: 1,
    points: 10,
    metadata: {
      color: '#4CAF50',
      unlockMessage: '¡Has comenzado tu viaje matemático!',
    },
  },
  {
    code: 'FIVE_EXERCISES',
    name: 'En Marcha',
    description: 'Completa 5 ejercicios',
    category: AchievementCategory.EXERCISES,
    rarity: AchievementRarity.COMMON,
    icon: '🚀',
    requirementType: RequirementType.EXERCISES_COMPLETED,
    requirementValue: 5,
    points: 25,
    metadata: {
      color: '#2196F3',
      unlockMessage: '¡Excelente comienzo!',
    },
  },
  {
    code: 'TWENTY_FIVE_EXERCISES',
    name: 'Estudiante Constante',
    description: 'Completa 25 ejercicios',
    category: AchievementCategory.EXERCISES,
    rarity: AchievementRarity.RARE,
    icon: '📝',
    requirementType: RequirementType.EXERCISES_COMPLETED,
    requirementValue: 25,
    points: 75,
    metadata: {
      color: '#9C27B0',
      unlockMessage: '¡Tu constancia es admirable!',
    },
  },
  {
    code: 'FIFTY_EXERCISES',
    name: 'Matemático Dedicado',
    description: 'Completa 50 ejercicios',
    category: AchievementCategory.EXERCISES,
    rarity: AchievementRarity.RARE,
    icon: '📚',
    requirementType: RequirementType.EXERCISES_COMPLETED,
    requirementValue: 50,
    points: 150,
    metadata: {
      color: '#673AB7',
      unlockMessage: '¡Dedicación excepcional!',
    },
  },
  {
    code: 'HUNDRED_EXERCISES',
    name: 'Centurión Matemático',
    description: 'Completa 100 ejercicios',
    category: AchievementCategory.EXERCISES,
    rarity: AchievementRarity.EPIC,
    icon: '🏆',
    requirementType: RequirementType.EXERCISES_COMPLETED,
    requirementValue: 100,
    points: 300,
    metadata: {
      color: '#FF9800',
      animation: 'pulse',
      unlockMessage: '¡Has entrado en la centuria!',
    },
  },
  {
    code: 'FIVE_PERFECT',
    name: 'Perfeccionista',
    description: 'Completa 5 ejercicios sin errores',
    category: AchievementCategory.EXERCISES,
    rarity: AchievementRarity.RARE,
    icon: '💎',
    requirementType: RequirementType.PERFECT_SCORES,
    requirementValue: 5,
    points: 75,
    metadata: {
      color: '#00BCD4',
      unlockMessage: '¡La perfección es tu norma!',
    },
  },
  {
    code: 'TWENTY_PERFECT',
    name: 'Maestro de la Precisión',
    description: 'Completa 20 ejercicios sin errores',
    category: AchievementCategory.EXERCISES,
    rarity: AchievementRarity.EPIC,
    icon: '✨',
    requirementType: RequirementType.PERFECT_SCORES,
    requirementValue: 20,
    points: 200,
    metadata: {
      color: '#E91E63',
      unlockMessage: '¡Precisión extraordinaria!',
    },
  },
  {
    code: 'SPEED_DEMON',
    name: 'Rayo',
    description: 'Completa 10 ejercicios en menos de 60 segundos cada uno',
    category: AchievementCategory.EXERCISES,
    rarity: AchievementRarity.RARE,
    icon: '⚡',
    requirementType: RequirementType.EXERCISES_COMPLETED,
    requirementValue: 10,
    points: 100,
    metadata: {
      color: '#FFEB3B',
      animation: 'flash',
      unlockMessage: '¡Más rápido que la luz!',
    },
  },
  {
    code: 'NIGHT_OWL_EXERCISES',
    name: 'Búho Nocturno',
    description: 'Completa 5 ejercicios después de medianoche',
    category: AchievementCategory.EXERCISES,
    rarity: AchievementRarity.RARE,
    icon: '🦉',
    requirementType: RequirementType.NIGHT_OWL,
    requirementValue: 5,
    points: 80,
    metadata: {
      color: '#3F51B5',
      unlockMessage: '¡La noche es testigo de tu esfuerzo!',
    },
  },

  // ==========================================
  // MODULE BADGES
  // ==========================================
  {
    code: 'FIRST_MODULE',
    name: 'Primera Conquista',
    description: 'Completa tu primer módulo',
    category: AchievementCategory.MODULES,
    rarity: AchievementRarity.RARE,
    icon: '🎓',
    requirementType: RequirementType.MODULES_COMPLETED,
    requirementValue: 1,
    points: 100,
    metadata: {
      color: '#4CAF50',
      unlockMessage: '¡Primera meta alcanzada!',
    },
  },
  {
    code: 'ALGEBRA_MASTER',
    name: 'Maestro del Álgebra',
    description: 'Completa todos los módulos del curso',
    category: AchievementCategory.MODULES,
    rarity: AchievementRarity.LEGENDARY,
    icon: '👑',
    requirementType: RequirementType.MODULES_COMPLETED,
    requirementValue: 3,
    points: 500,
    metadata: {
      color: '#FFD700',
      animation: 'shine',
      unlockMessage: '¡Maestro absoluto del álgebra lineal!',
    },
  },
  {
    code: 'PERFECT_MODULE',
    name: 'Módulo Perfecto',
    description: 'Completa un módulo con 100% de ejercicios correctos',
    category: AchievementCategory.MODULES,
    rarity: AchievementRarity.EPIC,
    icon: '⭐',
    requirementType: RequirementType.PERFECT_MODULE,
    requirementValue: 1,
    points: 300,
    metadata: {
      color: '#FFC107',
      unlockMessage: '¡Perfección total en el módulo!',
    },
  },
  {
    code: 'MODULE_FUNDAMENTOS',
    name: 'Fundamentos Conquistados',
    description: 'Completa el módulo de Fundamentos',
    category: AchievementCategory.MODULES,
    rarity: AchievementRarity.RARE,
    icon: '📐',
    requirementType: RequirementType.MODULES_COMPLETED,
    requirementValue: 1,
    points: 120,
    metadata: {
      color: '#009688',
      unlockMessage: '¡Los fundamentos son sólidos!',
    },
  },
  {
    code: 'MODULE_SISTEMAS',
    name: 'Sistemas Dominados',
    description: 'Completa el módulo de Sistemas y Espacios',
    category: AchievementCategory.MODULES,
    rarity: AchievementRarity.RARE,
    icon: '🔢',
    requirementType: RequirementType.MODULES_COMPLETED,
    requirementValue: 1,
    points: 120,
    metadata: {
      color: '#795548',
      unlockMessage: '¡Has dominado los sistemas!',
    },
  },
  {
    code: 'MODULE_APLICACIONES',
    name: 'Aplicador Experto',
    description: 'Completa el módulo de Aplicaciones',
    category: AchievementCategory.MODULES,
    rarity: AchievementRarity.RARE,
    icon: '📊',
    requirementType: RequirementType.MODULES_COMPLETED,
    requirementValue: 1,
    points: 120,
    metadata: {
      color: '#607D8B',
      unlockMessage: '¡Aplicando el conocimiento con éxito!',
    },
  },

  // ==========================================
  // STREAK BADGES
  // ==========================================
  {
    code: 'THREE_DAY_STREAK',
    name: 'En Racha',
    description: '3 días consecutivos de estudio',
    category: AchievementCategory.STREAKS,
    rarity: AchievementRarity.COMMON,
    icon: '🔥',
    requirementType: RequirementType.STREAK_DAYS,
    requirementValue: 3,
    points: 30,
    metadata: {
      color: '#FF5722',
      animation: 'burn',
      unlockMessage: '¡La racha comienza!',
    },
  },
  {
    code: 'WEEK_STREAK',
    name: 'Semana Perfecta',
    description: '7 días consecutivos de estudio',
    category: AchievementCategory.STREAKS,
    rarity: AchievementRarity.RARE,
    icon: '📅',
    requirementType: RequirementType.STREAK_DAYS,
    requirementValue: 7,
    points: 100,
    metadata: {
      color: '#F44336',
      unlockMessage: '¡Una semana de dedicación!',
    },
  },
  {
    code: 'TWO_WEEK_STREAK',
    name: 'Quincena Implacable',
    description: '14 días consecutivos de estudio',
    category: AchievementCategory.STREAKS,
    rarity: AchievementRarity.EPIC,
    icon: '🔥',
    requirementType: RequirementType.STREAK_DAYS,
    requirementValue: 14,
    points: 200,
    metadata: {
      color: '#E91E63',
      animation: 'inferno',
      unlockMessage: '¡Nada detiene tu progreso!',
    },
  },
  {
    code: 'MONTH_STREAK',
    name: 'Mensual Legendario',
    description: '30 días consecutivos de estudio',
    category: AchievementCategory.STREAKS,
    rarity: AchievementRarity.LEGENDARY,
    icon: '🌟',
    requirementType: RequirementType.STREAK_DAYS,
    requirementValue: 30,
    points: 500,
    metadata: {
      color: '#9C27B0',
      animation: 'supernova',
      unlockMessage: '¡Legendario! Un mes entero de estudio!',
    },
  },

  // ==========================================
  // RANKING BADGES
  // ==========================================
  {
    code: 'TOP_100',
    name: 'Top 100',
    description: 'Alcanza el Top 100 del ranking global',
    category: AchievementCategory.RANKING,
    rarity: AchievementRarity.RARE,
    icon: '🎖️',
    requirementType: RequirementType.RANKING_POSITION,
    requirementValue: 100,
    points: 100,
    metadata: {
      color: '#8BC34A',
      unlockMessage: '¡Estás en élite!',
    },
  },
  {
    code: 'TOP_50',
    name: 'Top 50',
    description: 'Alcanza el Top 50 del ranking global',
    category: AchievementCategory.RANKING,
    rarity: AchievementRarity.EPIC,
    icon: '🏅',
    requirementType: RequirementType.RANKING_POSITION,
    requirementValue: 50,
    points: 150,
    metadata: {
      color: '#CDDC39',
      unlockMessage: '¡Entre los mejores!',
    },
  },
  {
    code: 'TOP_10',
    name: 'Top 10',
    description: 'Alcanza el Top 10 del ranking global',
    category: AchievementCategory.RANKING,
    rarity: AchievementRarity.EPIC,
    icon: '🎖️',
    requirementType: RequirementType.RANKING_POSITION,
    requirementValue: 10,
    points: 250,
    metadata: {
      color: '#FFC107',
      animation: 'gleam',
      unlockMessage: '¡Entre los 10 mejores!',
    },
  },
  {
    code: 'PODIUM',
    name: 'Podium',
    description: 'Alcanza el Top 3 del ranking global',
    category: AchievementCategory.RANKING,
    rarity: AchievementRarity.LEGENDARY,
    icon: '🥇',
    requirementType: RequirementType.RANKING_POSITION,
    requirementValue: 3,
    points: 400,
    metadata: {
      color: '#FFD700',
      animation: 'crown',
      unlockMessage: '¡En el podio!',
    },
  },
  {
    code: 'CHAMPION',
    name: 'El Campeón',
    description: 'Alcanza el #1 del ranking global',
    category: AchievementCategory.RANKING,
    rarity: AchievementRarity.LEGENDARY,
    icon: '👑',
    requirementType: RequirementType.RANKING_POSITION,
    requirementValue: 1,
    points: 1000,
    metadata: {
      color: '#FFD700',
      animation: 'coronation',
      unlockMessage: '¡El campeón indiscutible!',
    },
  },

  // ==========================================
  // SPECIAL BADGES
  // ==========================================
  {
    code: 'EARLY_BIRD',
    name: 'Madrugador',
    description: 'Completa un ejercicio antes de las 6 AM',
    category: AchievementCategory.SPECIAL,
    rarity: AchievementRarity.RARE,
    icon: '🌅',
    requirementType: RequirementType.EARLY_BIRD,
    requirementValue: 1,
    points: 50,
    metadata: {
      color: '#FF9800',
      unlockMessage: '¡El que madruga, Dios ayuda!',
    },
  },
  {
    code: 'AUTODIDACT',
    name: 'Autodidacta',
    description: 'Completa 25 ejercicios sin usar pistas',
    category: AchievementCategory.SPECIAL,
    rarity: AchievementRarity.EPIC,
    icon: '📖',
    requirementType: RequirementType.EXERCISES_WITHOUT_HINTS,
    requirementValue: 25,
    points: 200,
    metadata: {
      color: '#795548',
      unlockMessage: '¡El verdadero aprendizaje es autodidacta!',
    },
  },
  {
    code: 'PERSISTENT',
    name: 'Persistente',
    description: 'Completa un ejercicio después de 3 o más intentos',
    category: AchievementCategory.SPECIAL,
    rarity: AchievementRarity.COMMON,
    icon: '💪',
    requirementType: RequirementType.EXERCISES_COMPLETED,
    requirementValue: 1,
    points: 20,
    metadata: {
      color: '#9E9E9E',
      unlockMessage: '¡El éxito es la suma de pequeños esfuerzos!',
    },
  },
  {
    code: 'FIRST_TRY_WARRIOR',
    name: 'Guerrero de Primer Intento',
    description: 'Completa 50 ejercicios correctamente al primer intento',
    category: AchievementCategory.SPECIAL,
    rarity: AchievementRarity.EPIC,
    icon: '🎯',
    requirementType: RequirementType.EXERCISES_COMPLETED,
    requirementValue: 50,
    points: 250,
    metadata: {
      color: '#F44336',
      unlockMessage: '¡Precisión absoluta desde el inicio!',
    },
  },
  {
    code: 'EXPLORER',
    name: 'Explorador',
    description: 'Intenta ejercicios de todos los niveles de dificultad',
    category: AchievementCategory.SPECIAL,
    rarity: AchievementRarity.RARE,
    icon: '🗺️',
    requirementType: RequirementType.EXERCISES_COMPLETED,
    requirementValue: 1,
    points: 80,
    metadata: {
      color: '#00BCD4',
      unlockMessage: '¡Has explorado todas las dificultades!',
    },
  },
];

// ============================================
// HELPER FUNCTIONS
// ============================================

/**
 * Get badges grouped by category
 */
export function getBadgesByCategory(): BadgeGroup[] {
  const groups: Record<AchievementCategory, BadgeDefinition[]> = {
    [AchievementCategory.EXERCISES]: [],
    [AchievementCategory.MODULES]: [],
    [AchievementCategory.STREAKS]: [],
    [AchievementCategory.RANKING]: [],
    [AchievementCategory.SPECIAL]: [],
  };

  for (const badge of BADGE_DEFINITIONS) {
    groups[badge.category].push(badge);
  }

  return [
    {
      category: AchievementCategory.EXERCISES,
      categoryName: 'Ejercicios',
      badges: groups[AchievementCategory.EXERCISES],
    },
    {
      category: AchievementCategory.MODULES,
      categoryName: 'Módulos',
      badges: groups[AchievementCategory.MODULES],
    },
    {
      category: AchievementCategory.STREAKS,
      categoryName: 'Rachas',
      badges: groups[AchievementCategory.STREAKS],
    },
    {
      category: AchievementCategory.RANKING,
      categoryName: 'Ranking',
      badges: groups[AchievementCategory.RANKING],
    },
    {
      category: AchievementCategory.SPECIAL,
      categoryName: 'Especiales',
      badges: groups[AchievementCategory.SPECIAL],
    },
  ];
}

/**
 * Get badge by code
 */
export function getBadgeByCode(code: string): BadgeDefinition | undefined {
  return BADGE_DEFINITIONS.find((b) => b.code === code);
}

/**
 * Get badges by rarity
 */
export function getBadgesByRarity(rarity: AchievementRarity): BadgeDefinition[] {
  return BADGE_DEFINITIONS.filter((b) => b.rarity === rarity);
}

/**
 * Get badges by requirement type
 */
export function getBadgesByRequirementType(
  requirementType: RequirementType
): BadgeDefinition[] {
  return BADGE_DEFINITIONS.filter((b) => b.requirementType === requirementType);
}

/**
 * Get total points from all badges
 */
export function getTotalBadgePoints(): number {
  return BADGE_DEFINITIONS.reduce((sum, badge) => sum + badge.points, 0);
}

/**
 * Count badges by category
 */
export function countBadgesByCategory(): Record<AchievementCategory, number> {
  const counts: Record<string, number> = {};
  for (const badge of BADGE_DEFINITIONS) {
    counts[badge.category] = (counts[badge.category] || 0) + 1;
  }
  return counts as Record<AchievementCategory, number>;
}

/**
 * Get badge color mapping for UI
 */
export const BADGE_RARITY_COLORS: Record<AchievementRarity, string> = {
  [AchievementRarity.COMMON]: '#9E9E9E',
  [AchievementRarity.RARE]: '#2196F3',
  [AchievementRarity.EPIC]: '#9C27B0',
  [AchievementRarity.LEGENDARY]: '#FFD700',
};

/**
 * Get badge order for display (Legendary first)
 */
export const BADGE_RARITY_ORDER: AchievementRarity[] = [
  AchievementRarity.LEGENDARY,
  AchievementRarity.EPIC,
  AchievementRarity.RARE,
  AchievementRarity.COMMON,
];

export default BADGE_DEFINITIONS;
