/**
 * Procedural Musician Generator
 * Creates unique fictional musicians using templates
 */

export class ProceduralMusicianGenerator {
  static FIRST_NAMES = [
    'Django', 'Luna', 'Echo', 'Atlas', 'Phoenix', 'Sage', 'River', 'Storm',
    'Jazz', 'Blues', 'Melody', 'Harmony', 'Rhythm', 'Tempo', 'Nova', 'Orion',
    'Lyric', 'Chord', 'Beat', 'Soul', 'Vibe', 'Groove', 'Pulse', 'Wave'
  ];

  static LAST_NAMES = [
    'Strings', 'Keys', 'Beats', 'Riffs', 'Notes', 'Tones', 'Waves', 'Flows',
    'Sounds', 'Vibes', 'Grooves', 'Melodies', 'Harmonies', 'Rhythms',
    'Sterling', 'Goldstone', 'Silverwind', 'Blackwood', 'Whitefield'
  ];

  static ADJECTIVES = [
    'Mysterious', 'Legendary', 'Forgotten', 'Lost', 'Hidden', 'Secret',
    'Enigmatic', 'Obscure', 'Underground', 'Mythical', 'Phantom'
  ];

  static TITLES = [
    'The Virtuoso', 'The Innovator', 'The Pioneer', 'The Maverick',
    'The Prodigy', 'The Master', 'The Legend', 'The Icon'
  ];

  /**
   * Generate a random musician for an era and genre
   * @param {string} era - Time period
   * @param {string} genre - Music genre
   * @param {number} seed - Random seed for consistency
   * @returns {Object} - Procedurally generated musician
   */
  static generate(era, genre, seed = Math.random()) {
    // Seeded random
    const random = this.seededRandom(seed);

    const firstName = this.FIRST_NAMES[Math.floor(random() * this.FIRST_NAMES.length)];
    const lastName = this.LAST_NAMES[Math.floor(random() * this.LAST_NAMES.length)];
    const adjective = this.ADJECTIVES[Math.floor(random() * this.ADJECTIVES.length)];
    const title = this.TITLES[Math.floor(random() * this.TITLES.length)];

    const name = `${firstName} ${lastName}`;
    const difficulty = Math.floor(random() * 3) + 2; // 2-4

    return {
      id: `proc_${name.toLowerCase().replace(/\s+/g, '_')}_${seed.toString().substring(2, 8)}`,
      name,
      era,
      location: this.getLocationForEra(era, random),
      genres: [genre],
      stylePrompt: this.generateStylePrompt(genre, random),
      description: `${adjective} ${genre} musician known as "${title}"`,
      historicalContext: `A ${adjective.toLowerCase()} figure in ${era} ${genre} history`,
      difficulty,
      icon: this.getGenreIcon(genre),
      requiredLevel: difficulty - 1,
      isProcedural: true
    };
  }

  /**
   * Seeded random generator
   */
  static seededRandom(seed) {
    let value = seed;
    return () => {
      value = (value * 9301 + 49297) % 233280;
      return value / 233280;
    };
  }

  /**
   * Get location for era
   */
  static getLocationForEra(era, random) {
    const locations = {
      '1720s': [{ city: 'Leipzig', country: 'Germany' }, { city: 'Venice', country: 'Italy' }],
      '1780s': [{ city: 'Vienna', country: 'Austria' }],
      '1800s': [{ city: 'Vienna', country: 'Austria' }, { city: 'Paris', country: 'France' }],
      '1920s': [{ city: 'New Orleans', country: 'USA' }, { city: 'Paris', country: 'France' }],
      '1950s': [{ city: 'Memphis', country: 'USA' }, { city: 'Nashville', country: 'USA' }],
      '1960s': [{ city: 'London', country: 'UK' }, { city: 'Los Angeles', country: 'USA' }],
      '1970s': [{ city: 'New York', country: 'USA' }, { city: 'Los Angeles', country: 'USA' }],
      '1980s': [{ city: 'Los Angeles', country: 'USA' }, { city: 'Tokyo', country: 'Japan' }],
      '1990s': [{ city: 'Seattle', country: 'USA' }, { city: 'Berlin', country: 'Germany' }],
      '2000s': [{ city: 'London', country: 'UK' }, { city: 'Paris', country: 'France' }],
      '2010s': [{ city: 'Los Angeles', country: 'USA' }, { city: 'Seoul', country: 'South Korea' }],
      '2020s': [{ city: 'Los Angeles', country: 'USA' }, { city: 'Lagos', country: 'Nigeria' }]
    };

    const pool = locations[era] || locations['2020s'];
    return pool[Math.floor(random() * pool.length)];
  }

  /**
   * Generate style prompt for genre
   */
  static generateStylePrompt(genre, random) {
    const templates = {
      rock: ['electric guitar', 'drums', 'bass', 'energetic', 'powerful', 'raw'],
      jazz: ['saxophone', 'piano', 'improvisation', 'swing', 'sophisticated', 'smooth'],
      electronic: ['synthesizers', 'beats', 'bass', 'atmospheric', 'futuristic', 'digital'],
      hip_hop: ['rap', 'beats', 'samples', 'flow', 'rhythm', 'urban'],
      classical: ['orchestra', 'piano', 'strings', 'elegant', 'refined', 'complex'],
      pop: ['catchy', 'melodic', 'polished', 'mainstream', 'vocal', 'hooks']
    };

    const words = templates[genre] || templates.pop;
    const shuffled = [...words].sort(() => random() - 0.5);
    return shuffled.slice(0, 5).join(', ');
  }

  /**
   * Get icon for genre
   */
  static getGenreIcon(genre) {
    const icons = {
      rock: '🎸',
      jazz: '🎷',
      electronic: '🎧',
      hip_hop: '🎤',
      classical: '🎹',
      pop: '⭐',
      metal: '⚡',
      funk: '🕺',
      reggae: '🌴',
      country: '🤠'
    };

    return icons[genre] || '🎵';
  }

  /**
   * Generate multiple musicians
   * @param {number} count - Number to generate
   * @param {string} era - Era
   * @param {Array} genrePool - Available genres
   * @returns {Array} - Generated musicians
   */
  static generateMultiple(count, era, genrePool) {
    const musicians = [];
    
    for (let i = 0; i < count; i++) {
      const genre = genrePool[i % genrePool.length];
      const seed = Math.random();
      musicians.push(this.generate(era, genre, seed));
    }

    return musicians;
  }
}

