/**
 * Game Generator: Creates 700+ unique slot games with different themes, names, and paytables
 * Each game has unique characteristics, branding, and payout structure
 */

export interface SlotGameConfig {
  id: string;
  name: string;
  provider: string;
  theme: string;
  description: string;
  volatility: "low" | "medium" | "high";
  rtp: number; // 92-96%
  minBet: number; // 0.01 SC
  maxBet: number;
  maxWin: number; // Capped at 20 SC
  paylines: number;
  reels: number;
  symbols: string[];
  bonusFeatures: string[];
  brand: string; // PlayCoinKrazy.com
}

const themes = [
  // Ancient Civilizations
  "Ancient Egypt", "Greek Mythology", "Roman Empire", "Aztec Gold", "Mayan Treasures",
  "Chinese Dynasty", "Japanese Samurai", "Indian Maharaja", "Persian Riches", "Viking Conquest",
  
  // Nature & Animals
  "African Safari", "Jungle Adventure", "Ocean Treasures", "Arctic Wolves", "Desert Oasis",
  "Tropical Paradise", "Mountain Peak", "Forest Magic", "Underwater Kingdom", "Sky Birds",
  
  // Fantasy & Magic
  "Dragon's Gold", "Wizard's Spell", "Fairy Tale", "Enchanted Forest", "Magic Potion",
  "Mystic Runes", "Crystal Palace", "Magical Creatures", "Spellbound", "Enchantment",
  
  // Luxury & Glamour
  "Diamond Royale", "Golden Treasure", "Platinum Elite", "Luxury Lifestyle", "VIP Club",
  "High Roller", "Champagne Dreams", "Jewel Collection", "Precious Metals", "Opulent Palace",
  
  // Modern & Pop Culture
  "Neon Nights", "Cyber Space", "Retro Arcade", "Future Tech", "Digital Dreams",
  "Pop Stars", "Movie Magic", "Music Festival", "Gaming Zone", "Street Style",
  
  // Seasonal & Holidays
  "Christmas Cheer", "Halloween Spooky", "Easter Eggs", "Valentine's Love", "Summer Beach",
  "Winter Wonderland", "Spring Blossom", "Autumn Harvest", "New Year Celebration", "Festival Fun",
  
  // Mythical & Legendary
  "Phoenix Rising", "Unicorn Magic", "Kraken's Deep", "Minotaur's Maze", "Sphinx Mystery",
  "Pegasus Flight", "Hydra's Gold", "Cerberus Guard", "Basilisk Curse", "Leviathan",
  
  // Sports & Action
  "Football Glory", "Basketball Stars", "Soccer Champion", "Tennis Ace", "Boxing Ring",
  "Racing Speed", "Skateboard Tricks", "Surfing Waves", "Climbing Peak", "Extreme Sports",
  
  // Food & Drink
  "Sushi Paradise", "Pizza Party", "Chocolate Heaven", "Coffee Break", "Wine Tasting",
  "Fruit Fiesta", "Candy Crush", "Burger Bonanza", "Taco Tuesday", "Donut Delight",
  
  // Music & Entertainment
  "Rock Concert", "Jazz Night", "Classical Symphony", "Hip Hop Beats", "Country Roads",
  "Electronic Vibes", "Opera House", "Disco Fever", "Karaoke Night", "DJ Turntable",
  
  // Space & Sci-Fi
  "Outer Space", "Alien Invasion", "Rocket Launch", "Moon Base", "Mars Colony",
  "Star Wars", "Galaxy Quest", "UFO Landing", "Asteroid Belt", "Black Hole",
  
  // Professions & Careers
  "Doctor's Office", "Lawyer's Desk", "Chef's Kitchen", "Pilot's Cockpit", "Astronaut",
  "Detective Mystery", "Engineer's Lab", "Artist's Studio", "Musician's Stage", "Teacher's Class",
  
  // Geography & Travel
  "Paris Romance", "Tokyo Nights", "New York City", "Las Vegas Strip", "London Bridge",
  "Dubai Desert", "Bangkok Streets", "Sydney Harbor", "Rio Carnival", "Barcelona Beach",
  
  // Historical Periods
  "Medieval Times", "Renaissance Art", "Victorian Era", "Roaring Twenties", "Wild West",
  "Pirate's Treasure", "Samurai Code", "Knight's Quest", "Crusader's Journey", "Explorer's Map",
  
  // Supernatural & Horror
  "Haunted House", "Zombie Apocalypse", "Vampire's Bite", "Werewolf Moon", "Ghost Story",
  "Witch's Brew", "Demon's Gate", "Skeleton Crew", "Mummy's Curse", "Possessed",
  
  // Business & Finance
  "Stock Market", "Crypto Boom", "Gold Rush", "Diamond Mine", "Oil Rig",
  "Bank Vault", "Money Maker", "Wealth Builder", "Fortune 500", "Millionaire's Club",
  
  // Nature & Elements
  "Fire Storm", "Water Splash", "Earth Quake", "Wind Gust", "Lightning Strike",
  "Thunder Bolt", "Tornado Spin", "Blizzard Snow", "Volcano Eruption", "Avalanche",
];

const providers = [
  "PlayCoinKrazy", "Pragmatic Play", "NetEnt", "Microgaming", "IGT",
  "Playtech", "Yggdrasil", "Betsoft", "Novomatic", "WMS",
  "Aristocrat", "Konami", "Ainsworth", "Bally", "Everi",
];

const bonusFeatures = [
  "Free Spins", "Multiplier", "Wild Symbol", "Scatter Symbol", "Bonus Game",
  "Expanding Reels", "Sticky Wilds", "Cascading Symbols", "Mega Spin", "Super Spin",
  "Mystery Symbol", "Pick and Click", "Wheel Spin", "Card Flip", "Ladder Climb",
  "Respin Feature", "Hold and Spin", "Nudge Feature", "Turbo Mode", "Jackpot",
];

const symbolThemes = {
  "Ancient Egypt": ["Pharaoh", "Pyramid", "Scarab", "Ankh", "Sphinx", "Gold", "Hieroglyphics"],
  "Greek Mythology": ["Zeus", "Athena", "Poseidon", "Pegasus", "Medusa", "Olympus", "Laurel"],
  "Dragon's Gold": ["Dragon", "Gold", "Treasure", "Crown", "Gem", "Fire", "Castle"],
  "Jungle Adventure": ["Monkey", "Lion", "Tiger", "Parrot", "Vine", "Jungle", "Treasure"],
  "Ocean Treasures": ["Mermaid", "Dolphin", "Octopus", "Treasure", "Pearl", "Coral", "Ship"],
  "African Safari": ["Lion", "Elephant", "Zebra", "Giraffe", "Rhino", "Savanna", "Sun"],
  "Neon Nights": ["Neon", "City", "Light", "Electric", "Cyber", "Digital", "Glow"],
  "Christmas Cheer": ["Santa", "Reindeer", "Gift", "Tree", "Snow", "Bell", "Candy"],
};

/**
 * Generate a unique slot game configuration
 */
function generateGame(index: number): SlotGameConfig {
  const themeIndex = index % themes.length;
  const providerIndex = Math.floor(index / themes.length) % providers.length;
  const theme = themes[themeIndex];
  const provider = providers[providerIndex];
  
  // Generate unique name combining theme and variant
  const variant = Math.floor(index / (themes.length * providers.length)) + 1;
  const name = variant > 1 ? `${theme} ${variant}` : theme;
  
  // Generate game ID
  const id = `game_${String(index + 1).padStart(4, "0")}`;
  
  // Volatility distribution: 40% low, 40% medium, 20% high
  let volatility: "low" | "medium" | "high";
  const volRand = Math.random();
  if (volRand < 0.4) volatility = "low";
  else if (volRand < 0.8) volatility = "medium";
  else volatility = "high";
  
  // RTP based on volatility
  const rtpBase = { low: 95, medium: 94, high: 93 };
  const rtp = rtpBase[volatility] + Math.random() * 2;
  
  // Paylines: 5, 9, 15, 20, 25, 30, 40, 50
  const paylineOptions = [5, 9, 15, 20, 25, 30, 40, 50];
  const paylines = paylineOptions[Math.floor(Math.random() * paylineOptions.length)];
  
  // Max bet: 0.01 SC to 10 SC
  const maxBet = Math.round((0.01 + Math.random() * 9.99) * 100) / 100;
  
  // Max win: capped at 20 SC
  const maxWin = 20;
  
  // Bonus features: 2-4 random features
  const numFeatures = 2 + Math.floor(Math.random() * 3);
  const features: string[] = [];
  for (let i = 0; i < numFeatures; i++) {
    const feature = bonusFeatures[Math.floor(Math.random() * bonusFeatures.length)];
    if (!features.includes(feature)) {
      features.push(feature);
    }
  }
  
  // Symbols based on theme or generic
  const symbols = symbolThemes[theme as keyof typeof symbolThemes] || [
    "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5"
  ];
  
  return {
    id,
    name,
    provider: "PlayCoinKrazy",
    theme,
    description: `Experience the excitement of ${name}. ${volatility.charAt(0).toUpperCase() + volatility.slice(1)} volatility with ${paylines} paylines.`,
    volatility,
    rtp: Math.round(rtp * 100) / 100,
    minBet: 0.01,
    maxBet,
    maxWin,
    paylines,
    reels: 5,
    symbols,
    bonusFeatures: features,
    brand: "PlayCoinKrazy.com",
  };
}

/**
 * Generate all 700+ games
 */
export function generateAllGames(count: number = 700): SlotGameConfig[] {
  const games: SlotGameConfig[] = [];
  for (let i = 0; i < count; i++) {
    games.push(generateGame(i));
  }
  return games;
}

/**
 * Get a specific game by ID
 */
export function getGameById(id: string, allGames: SlotGameConfig[]): SlotGameConfig | undefined {
  return allGames.find((g) => g.id === id);
}

/**
 * Filter games by theme
 */
export function getGamesByTheme(theme: string, allGames: SlotGameConfig[]): SlotGameConfig[] {
  return allGames.filter((g) => g.theme === theme);
}

/**
 * Filter games by volatility
 */
export function getGamesByVolatility(volatility: "low" | "medium" | "high", allGames: SlotGameConfig[]): SlotGameConfig[] {
  return allGames.filter((g) => g.volatility === volatility);
}

/**
 * Get random games
 */
export function getRandomGames(count: number, allGames: SlotGameConfig[]): SlotGameConfig[] {
  const shuffled = [...allGames].sort(() => Math.random() - 0.5);
  return shuffled.slice(0, count);
}

/**
 * Get game statistics
 */
export function getGameStats(allGames: SlotGameConfig[]) {
  return {
    total: allGames.length,
    byTheme: allGames.reduce((acc, g) => {
      acc[g.theme] = (acc[g.theme] || 0) + 1;
      return acc;
    }, {} as Record<string, number>),
    byVolatility: {
      low: allGames.filter((g) => g.volatility === "low").length,
      medium: allGames.filter((g) => g.volatility === "medium").length,
      high: allGames.filter((g) => g.volatility === "high").length,
    },
    avgRtp: Math.round((allGames.reduce((sum, g) => sum + g.rtp, 0) / allGames.length) * 100) / 100,
  };
}
