/**
 * Questlines Database
 * Branching narrative quests with choices and consequences
 */

export const questlines = [
  {
    id: 'save_mozart',
    title: 'Save Young Mozart',
    icon: '🎼',
    description: 'Help Mozart claim artistic freedom while mastering classical brilliance.',
    autoStart: true,
    startCondition: {
      era: '1780s',
      hasMetMusician: 'mozart',
      playerLevel: 2
    },
    steps: [
      {
        id: 'step1',
        type: 'dialogue',
        npc: 'mozart',
        text: 'My father Leopold is too controlling! He manages every aspect of my career. I want artistic freedom!',
        choices: [
          {
            text: 'Help him escape and pursue independence',
            nextStep: 'step2a',
            reputationChange: { classical: 10 },
            branch: 'freedom'
          },
          {
            text: 'Advise patience and respect for family',
            nextStep: 'step2b',
            reputationChange: { classical: -5 },
            branch: 'obedience'
          }
        ]
      },
      {
        id: 'step2a',
        type: 'objective',
        text: 'Help Mozart establish his own career in Vienna',
        objective: {
          type: 'complete_challenge',
          genre: 'classical',
          musicianId: 'mozart',
          tags: ['arpeggios', 'ensemble'],
          recommendedChallengeIds: ['mozart_variations', 'mozart_duet'],
          count: 2
        },
        nextStep: 'step2a_mastery'
      },
      {
        id: 'step2a_mastery',
        type: 'objective',
        text: "Internalize Mozart's dazzling style",
        objective: {
          type: 'learn_style',
          musicianId: 'mozart',
          styleId: 'mozart_style',
          count: 1
        },
        nextStep: 'finale_freedom'
      },
      {
        id: 'step2b',
        type: 'dialogue',
        npc: 'mozart',
        text: 'Perhaps you are right. Family loyalty is important...',
        choices: [
          {
            text: 'That is wisdom beyond your years',
            nextStep: 'finale_obedience',
            reputationChange: { classical: 5 },
            branch: 'obedience'
          }
        ]
      },
      {
        id: 'finale_freedom',
        type: 'conclusion',
        text: 'Mozart successfully breaks free and creates his greatest works independently!',
        branch: 'freedom'
      },
      {
        id: 'finale_obedience',
        type: 'conclusion',
        text: 'Mozart stays with his father but never reaches his full artistic potential.',
        branch: 'obedience'
      }
    ],
    rewards: {
      freedom: {
        fans: 420,
        streams: 180,
        credits: 200,
        genreXp: { classical: 120 },
        unlocks: ['independent_spirit']
      },
      obedience: {
        fans: 220,
        streams: 90,
        credits: 150,
        genreXp: { classical: 60 },
        unlocks: ['family_values']
      }
    }
  },

  {
    id: 'elvis_influence',
    title: 'The Elvis Dilemma',
    icon: '🎙️',
    description: 'Guide Elvis through the tension between rebellion and mainstream fame.',
    autoStart: true,
    startCondition: {
      era: '1950s',
      hasMetMusician: 'elvis',
      playerLevel: 2
    },
    steps: [
      {
        id: 'step1',
        type: 'dialogue',
        npc: 'elvis',
        text: 'They say my music is dangerous, corrupting youth. But I am just being myself!',
        choices: [
          {
            text: 'Stand firm - your art is revolutionary!',
            nextStep: 'step2a',
            reputationChange: { rock: 15 },
            branch: 'rebel'
          },
          {
            text: 'Maybe tone it down for mainstream acceptance',
            nextStep: 'step2b',
            reputationChange: { rock: -10, pop: 10 },
            branch: 'mainstream'
          }
        ]
      },
      {
        id: 'step2a',
        type: 'objective',
        text: 'Help Elvis create his most groundbreaking performances',
        objective: {
          type: 'complete_challenge',
          genre: 'rock',
          musicianId: 'elvis',
          tags: ['groove', 'charisma'],
          recommendedChallengeIds: ['elvis_stagecraft'],
          count: 3
        },
        nextStep: 'step2a_vibes'
      },
      {
        id: 'step2a_vibes',
        type: 'objective',
        text: "Lock in Elvis's rebellious swagger",
        objective: {
          type: 'learn_style',
          musicianId: 'elvis',
          styleId: 'elvis_style',
          count: 1
        },
        nextStep: 'finale_rebel'
      },
      {
        id: 'step2b',
        type: 'objective',
        text: 'Help Elvis craft radio-friendly hits',
        objective: {
          type: 'complete_challenge',
          genre: 'pop',
          musicianId: 'elvis',
          tags: ['crossover', 'arrangement'],
          recommendedChallengeIds: ['elvis_crossover'],
          count: 2
        },
        nextStep: 'step2b_polish'
      },
      {
        id: 'step2b_polish',
        type: 'objective',
        text: "Capture Elvis's crossover charisma",
        objective: {
          type: 'learn_style',
          musicianId: 'elvis',
          styleId: 'elvis_style',
          count: 1
        },
        nextStep: 'finale_mainstream'
      },
      {
        id: 'finale_rebel',
        type: 'conclusion',
        text: 'Elvis remains true to himself and becomes the King of Rock and Roll!',
        branch: 'rebel'
      },
      {
        id: 'finale_mainstream',
        type: 'conclusion',
        text: 'Elvis becomes successful but loses some of his rebellious edge.',
        branch: 'mainstream'
      }
    ],
    rewards: {
      rebel: {
        fans: 520,
        streams: 230,
        credits: 260,
        genreXp: { rock: 100 },
        unlocks: ['rock_rebellion']
      },
      mainstream: {
        fans: 360,
        streams: 260,
        credits: 210,
        genreXp: { pop: 80 },
        unlocks: ['crossover_appeal']
      }
    }
  },

  {
    id: 'hendrix_discovery',
    title: "Discover Hendrix's Sound",
    icon: '🎸',
    description: 'Experiment with Hendrix to invent the psychedelic electric frontier.',
    autoStart: true,
    startCondition: {
      era: '1960s',
      playerLevel: 3
    },
    steps: [
      {
        id: 'step1',
        type: 'dialogue',
        npc: 'hendrix',
        text: 'I am experimenting with feedback and distortion. People think I am crazy!',
        choices: [
          {
            text: 'Your innovations will change guitar forever!',
            nextStep: 'step2',
            reputationChange: { rock: 10, psychedelic: 10 },
            branch: 'innovator'
          }
        ]
      },
      {
        id: 'step2',
        type: 'objective',
        text: 'Learn the psychedelic guitar techniques',
        objective: {
          type: 'complete_challenge',
          genre: 'psychedelic',
          musicianId: 'hendrix',
          recommendedChallengeIds: ['hendrix_feedback'],
          count: 2
        },
        nextStep: 'finale'
      },
      {
        id: 'finale',
        type: 'conclusion',
        text: 'You helped Hendrix perfect his sound, changing rock music forever!',
        branch: 'innovator'
      }
    ],
    rewards: {
      innovator: {
        fans: 640,
        streams: 300,
        credits: 320,
        genreXp: { psychedelic: 140, rock: 80 },
        unlocks: ['psychedelic_guitar']
      }
    }
  },

  {
    id: 'bach_tenth_symphony',
    title: "Beethoven's Lost 10th Symphony",
    icon: '🎻',
    description: 'Unearth the sketches of Beethoven’s elusive 10th Symphony.',
    autoStart: false,
    startCondition: {
      era: '1800s',
      hasMetMusician: 'beethoven',
      playerLevel: 4
    },
    steps: [
      {
        id: 'step1',
        type: 'dialogue',
        npc: 'beethoven',
        text: 'I have sketches for a 10th Symphony, but my deafness makes it impossible to complete...',
        choices: [
          {
            text: 'I will help you complete it!',
            nextStep: 'step2',
            reputationChange: { classical: 20 },
            branch: 'complete'
          },
          {
            text: 'Perhaps it is best left unfinished',
            nextStep: 'finale_unfinished',
            reputationChange: { classical: 5 },
            branch: 'preserve'
          }
        ]
      },
      {
        id: 'step2',
        type: 'objective',
        text: 'Master classical composition to help Beethoven',
        objective: {
          type: 'complete_challenge',
          genre: 'classical',
          musicianId: 'beethoven',
          recommendedChallengeIds: ['beethoven_sonata', 'beethoven_orchestra'],
          count: 5
        },
        nextStep: 'finale_complete'
      },
      {
        id: 'finale_complete',
        type: 'conclusion',
        text: 'Beethoven\'s 10th Symphony is complete - a masterpiece for the ages!',
        branch: 'complete'
      },
      {
        id: 'finale_unfinished',
        type: 'conclusion',
        text: 'The 10th Symphony remains a tantalizing mystery.',
        branch: 'preserve'
      }
    ],
    rewards: {
      complete: {
        fans: 800,
        streams: 420,
        credits: 400,
        genreXp: { classical: 220 },
        unlocks: ['beethoven_tenth', 'composition_master']
      },
      preserve: {
        fans: 300,
        streams: 180,
        credits: 150,
        genreXp: { classical: 80 }
      }
    }
  },

  {
    id: 'jazz_fusion',
    title: 'Miles Davis Jazz Fusion',
    icon: '🎺',
    description: 'Blend jazz and rock with Miles Davis to invent a new sound.',
    autoStart: false,
    startCondition: {
      era: '1960s',
      playerLevel: 3
    },
    steps: [
      {
        id: 'step1',
        type: 'dialogue',
        npc: 'miles_davis',
        text: 'Jazz is getting stale. I want to merge it with rock and electronics!',
        choices: [
          {
            text: 'Revolutionary! Let us create jazz fusion!',
            nextStep: 'step2',
            reputationChange: { jazz: 15, rock: 10 },
            branch: 'fusion'
          }
        ]
      },
      {
        id: 'step2',
        type: 'objective',
        text: 'Master both jazz and rock to create fusion',
        objective: {
          type: 'complete_challenge',
          genre: 'jazz',
          recommendedChallengeIds: ['miles_blue', 'jazz_combo'],
          count: 2
        },
        nextStep: 'step3'
      },
      {
        id: 'step3',
        type: 'objective',
        text: 'Complete the fusion',
        objective: {
          type: 'complete_challenge',
          genre: 'rock',
          recommendedChallengeIds: ['fusion_power_chords'],
          count: 2
        },
        nextStep: 'finale'
      },
      {
        id: 'finale',
        type: 'conclusion',
        text: 'Jazz fusion is born! Bitches Brew changes music history!',
        branch: 'fusion'
      }
    ],
    rewards: {
      fusion: {
        fans: 740,
        streams: 360,
        credits: 360,
        genreXp: { jazz: 120, rock: 100 },
        unlocks: ['jazz_fusion', 'genre_pioneer']
      }
    }
  }
];

/**
 * Get available quests for player
 */
export const getAvailableQuests = (playerState, worldState) => {
  return questlines.filter(quest => {
    const { startCondition } = quest;
    
    // Check if already completed
    if (worldState.completedQuests.includes(quest.id)) {
      return false;
    }
    
    // Check if already active
    if (worldState.activeQuests.some(q => q.id === quest.id)) {
      return false;
    }

    // Check era
    if (startCondition.era && worldState.currentEra !== startCondition.era) {
      return false;
    }

    // Check player level
    if (startCondition.playerLevel && playerState.level < startCondition.playerLevel) {
      return false;
    }

    // Check if met musician
    if (startCondition.hasMetMusician) {
      const learnedStyles = playerState.learnedStyles || [];
      const rawId = startCondition.hasMetMusician;
      const hasStyle =
        learnedStyles.includes(rawId) ||
        learnedStyles.includes(`${rawId}_style`);

      if (!hasStyle) {
        return false;
      }
    }

    return true;
  });
};

/**
 * Get quest by ID
 */
export const getQuestById = (id) => {
  return questlines.find(q => q.id === id);
};

