/**
 * Cosmetics Service
 * Manages seasonal cosmetics and player cosmetic ownership
 */

export interface Cosmetic {
  id: string;
  name: string;
  description: string;
  type: 'avatar' | 'frame' | 'effect' | 'badge';
  eventId: string;
  theme: string;
  icon: string;
  cost: number;
  costCurrency: 'SC' | 'GC';
  requirement: string;
  rarity: 'common' | 'rare' | 'epic' | 'legendary';
}

// Predefined cosmetics for seasonal events
const COSMETICS: Record<string, Cosmetic> = {
  // Halloween cosmetics
  halloween_avatar_ghost: {
    id: 'halloween_avatar_ghost',
    name: 'Ghost Avatar',
    description: 'Friendly ghost profile picture',
    type: 'avatar',
    eventId: 'halloween_2026',
    theme: 'halloween',
    icon: '👻',
    cost: 250,
    costCurrency: 'SC',
    requirement: '25 wins',
    rarity: 'rare',
  },
  halloween_avatar_pumpkin: {
    id: 'halloween_avatar_pumpkin',
    name: 'Pumpkin King Avatar',
    description: 'Carved pumpkin profile picture',
    type: 'avatar',
    eventId: 'halloween_2026',
    theme: 'halloween',
    icon: '🎃',
    cost: 500,
    costCurrency: 'SC',
    requirement: '50 wins',
    rarity: 'epic',
  },
  halloween_frame_spooky: {
    id: 'halloween_frame_spooky',
    name: 'Spooky Frame',
    description: 'Haunted picture frame effect',
    type: 'frame',
    eventId: 'halloween_2026',
    theme: 'halloween',
    icon: '🕷️',
    cost: 150,
    costCurrency: 'SC',
    requirement: '10 wins',
    rarity: 'common',
  },
  halloween_effect_glow: {
    id: 'halloween_effect_glow',
    name: 'Spooky Glow',
    description: 'Green glowing effect on wins',
    type: 'effect',
    eventId: 'halloween_2026',
    theme: 'halloween',
    icon: '✨',
    cost: 300,
    costCurrency: 'SC',
    requirement: '30 wins',
    rarity: 'epic',
  },
  halloween_badge_master: {
    id: 'halloween_badge_master',
    name: 'Spooky Master Badge',
    description: 'Exclusive badge for top players',
    type: 'badge',
    eventId: 'halloween_2026',
    theme: 'halloween',
    icon: '🏆',
    cost: 1000,
    costCurrency: 'SC',
    requirement: 'Top 10 Leaderboard',
    rarity: 'legendary',
  },

  // Christmas cosmetics
  christmas_avatar_santa: {
    id: 'christmas_avatar_santa',
    name: 'Santa Avatar',
    description: 'Jolly Santa profile picture',
    type: 'avatar',
    eventId: 'christmas_2026',
    theme: 'christmas',
    icon: '🎅',
    cost: 250,
    costCurrency: 'SC',
    requirement: '25 wins',
    rarity: 'rare',
  },
  christmas_avatar_elf: {
    id: 'christmas_avatar_elf',
    name: 'Elf Avatar',
    description: 'Festive elf profile picture',
    type: 'avatar',
    eventId: 'christmas_2026',
    theme: 'christmas',
    icon: '🧝',
    cost: 500,
    costCurrency: 'SC',
    requirement: '50 wins',
    rarity: 'epic',
  },
  christmas_frame_lights: {
    id: 'christmas_frame_lights',
    name: 'Christmas Lights Frame',
    description: 'Twinkling lights frame effect',
    type: 'frame',
    eventId: 'christmas_2026',
    theme: 'christmas',
    icon: '🎄',
    cost: 150,
    costCurrency: 'SC',
    requirement: '10 wins',
    rarity: 'common',
  },
  christmas_effect_snow: {
    id: 'christmas_effect_snow',
    name: 'Snowfall Effect',
    description: 'Falling snow on win screen',
    type: 'effect',
    eventId: 'christmas_2026',
    theme: 'christmas',
    icon: '❄️',
    cost: 300,
    costCurrency: 'SC',
    requirement: '30 wins',
    rarity: 'epic',
  },
  christmas_badge_champion: {
    id: 'christmas_badge_champion',
    name: 'Holiday Champion Badge',
    description: 'Exclusive badge for top players',
    type: 'badge',
    eventId: 'christmas_2026',
    theme: 'christmas',
    icon: '🏆',
    cost: 1000,
    costCurrency: 'SC',
    requirement: 'Top 10 Leaderboard',
    rarity: 'legendary',
  },
};

/**
 * Get cosmetic by ID
 */
export function getCosmetic(cosmeticId: string): Cosmetic | undefined {
  return COSMETICS[cosmeticId];
}

/**
 * Get all cosmetics for an event
 */
export function getCosmeticsByEvent(eventId: string): Cosmetic[] {
  return Object.values(COSMETICS).filter((c) => c.eventId === eventId);
}

/**
 * Get cosmetics by type
 */
export function getCosmeticsByType(type: string): Cosmetic[] {
  return Object.values(COSMETICS).filter((c) => c.type === type);
}

/**
 * Get cosmetics by rarity
 */
export function getCosmeticsByRarity(rarity: string): Cosmetic[] {
  return Object.values(COSMETICS).filter((c) => c.rarity === rarity);
}

/**
 * Get all cosmetics
 */
export function getAllCosmetics(): Cosmetic[] {
  return Object.values(COSMETICS);
}

/**
 * Check if cosmetic exists
 */
export function cosmeticExists(cosmeticId: string): boolean {
  return cosmeticId in COSMETICS;
}

/**
 * Get cosmetic cost
 */
export function getCosmeticCost(cosmeticId: string): { amount: number; currency: string } | null {
  const cosmetic = COSMETICS[cosmeticId];
  if (!cosmetic) return null;

  return {
    amount: cosmetic.cost,
    currency: cosmetic.costCurrency,
  };
}

/**
 * Get cosmetic requirement
 */
export function getCosmeticRequirement(cosmeticId: string): string | null {
  const cosmetic = COSMETICS[cosmeticId];
  return cosmetic?.requirement || null;
}

/**
 * Get cosmetics by theme
 */
export function getCosmeticsByTheme(theme: string): Cosmetic[] {
  return Object.values(COSMETICS).filter((c) => c.theme === theme);
}

/**
 * Calculate total cosmetics value for a player
 */
export function calculateCosmeticsValue(cosmeticIds: string[]): { SC: number; GC: number } {
  let scTotal = 0;
  let gcTotal = 0;

  for (const id of cosmeticIds) {
    const cosmetic = COSMETICS[id];
    if (!cosmetic) continue;

    if (cosmetic.costCurrency === 'SC') {
      scTotal += cosmetic.cost;
    } else {
      gcTotal += cosmetic.cost;
    }
  }

  return { SC: scTotal, GC: gcTotal };
}

/**
 * Get cosmetics by event and type
 */
export function getCosmeticsByEventAndType(eventId: string, type: string): Cosmetic[] {
  return Object.values(COSMETICS).filter((c) => c.eventId === eventId && c.type === type);
}
