/**
 * Adolescent Life Events
 * General adolescent experiences including school, personal growth, and social situations (ages 10-18)
 *
 * Events:
 * - groupProjectDrama: Dealing with lazy group project partners
 * - voiceCracking: Voice changing during puberty (males)
 * - bodySelfConsciousness: Growing awareness of physical appearance
 * - curfewArgument: Negotiating curfew with parents
 * - growthSpurt: Sudden rapid growth
 * - embarrassingSituation: Random embarrassing teen moment
 * - sleepover: First overnight party with friends
 * - socialMediaPressure: Deciding whether to get social media
 * - fashionExperimentation: Trying a bold new style
 * - learningToDrive: First time behind the wheel
 */

import {
  createMessageEvent,
  createQuestionEvent,
  createAnswerOption,
  checkProbability,
  checkMilestoneProbability,
  type EventResult,
} from '../base';
import type { Player } from '../../models/Player';

/**
 * Dealing with lazy group project partners (ages 12-18, student)
 */
export function groupProjectDrama(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'groupProjectDrama';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 12 &&
    ageYears <= 18 &&
    player.character.occupation === 'student' &&
    checkProbability(500);

  const message =
    "Your group project is due tomorrow and your partners haven't done their part. What do you do?";
  const answerOptions = [
    createAnswerOption('Do all the work yourself', '', 20),
    createAnswerOption('Tell the teacher'),
    createAnswerOption('Stay up all night helping everyone finish', '', 15, 5),
    createAnswerOption('Turn in what you have'),
  ];

  if (type !== 'answer') {
    return createQuestionEvent(fname, message, player, check, {
      answerOptions: answerOptions.map((opt) => opt.option),
    });
  } else if (type === 'answer' && response) {
    player.askedQuestions.add(fname);

    if (response.option === 'Do all the work yourself') {
      player.messageQueue.push(
        "You stayed up until 3am finishing everyone's work. You're exhausted but at least it's done."
      );
      player.character.happiness -= 10;
    } else if (response.option === 'Tell the teacher') {
      player.messageQueue.push(
        "You told the teacher about the situation. Your grade was protected, but your group members aren't happy with you."
      );
      player.character.social = (player.character.social ?? 0) - 10;
    } else if (response.option === 'Stay up all night helping everyone finish') {
      player.messageQueue.push(
        'You rallied the team and worked together all night. It was exhausting but everyone pulled through, and your group appreciates your leadership!'
      );
      player.character.social = (player.character.social ?? 0) + 15;
    } else if (response.option === 'Turn in what you have') {
      player.messageQueue.push(
        "You turned in the incomplete project. The teacher wasn't impressed, and you all got a lower grade."
      );
      player.character.happiness -= 5;
    }
  }

  return null;
}

/**
 * Voice changing during puberty (males, ages 12-14)
 */
export function voiceCracking(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'voiceCracking';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    player.character.sex === 'Male' &&
    ageYears >= 12 &&
    ageYears <= 14 &&
    checkProbability(800);

  const message =
    'Your voice cracks right in the middle of answering a question in class. Everyone giggles. Your face turns red.';

  if (check) {
    player.events.add(fname);
  }

  return createMessageEvent(fname, message, player, check);
}

/**
 * Growing awareness of physical appearance (ages 11-14)
 */
export function bodySelfConsciousness(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'bodySelfConsciousness';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 11 &&
    ageYears <= 14 &&
    checkProbability(600);

  const message =
    "You spend extra time looking in the mirror today. You're starting to notice things you never paid attention to before.";

  if (check) {
    player.events.add(fname);
  }

  return createMessageEvent(fname, message, player, check);
}

/**
 * Negotiating curfew with parents (ages 14-17)
 */
export function curfewArgument(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'curfewArgument';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 14 &&
    ageYears <= 17 &&
    checkProbability(700);

  const message =
    "Your parents set a 10pm curfew, but your friends are staying out until midnight. What do you do?";
  const answerOptions = [
    createAnswerOption('Follow the rules'),
    createAnswerOption("Sneak out after they're asleep", '', 10),
    createAnswerOption('Negotiate for 11pm', '', 0, 5),
    createAnswerOption("Don't go out at all"),
  ];

  if (type !== 'answer') {
    return createQuestionEvent(fname, message, player, check, {
      answerOptions: answerOptions.map((opt) => opt.option),
    });
  } else if (type === 'answer' && response) {
    player.askedQuestions.add(fname);

    if (response.option === 'Follow the rules') {
      player.messageQueue.push(
        'You followed the rules and came home at 10pm. Your friends teased you a bit, but your parents appreciated it.'
      );
      player.character.happiness -= 5;
      player.character.social = (player.character.social ?? 0) - 5;
    } else if (response.option === "Sneak out after they're asleep") {
      player.messageQueue.push(
        "You snuck out after your parents went to sleep. You had fun but barely got any sleep and you're worried about getting caught."
      );
    } else if (response.option === 'Negotiate for 11pm') {
      player.messageQueue.push(
        'You calmly made your case and your parents agreed to 11pm. Compromise feels good!'
      );
      player.character.happiness += 5;
    } else if (response.option === "Don't go out at all") {
      player.messageQueue.push(
        'You decided not to go out at all. Your friends were disappointed and you felt left out.'
      );
      player.character.social = (player.character.social ?? 0) - 10;
      player.character.happiness -= 10;
    }
  }

  return null;
}

/**
 * Sudden rapid growth (ages 12-15)
 */
export function growthSpurt(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'growthSpurt';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 12 &&
    ageYears <= 15 &&
    checkProbability(1000);

  const message =
    "You grew 3 inches this year! None of your pants fit anymore and you keep bumping into things.";

  if (check) {
    player.events.add(fname);
  }

  return createMessageEvent(fname, message, player, check);
}

/**
 * Random embarrassing teen moment (ages 13-17)
 */
export function embarrassingSituation(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'embarrassingSituation';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 13 &&
    ageYears <= 17 &&
    checkProbability(500);

  const messages = [
    "You walked around all day with food stuck in your teeth. Why didn't anyone tell you?!",
    "You called your teacher 'Mom' by accident. The whole class erupted in laughter.",
    'You tripped going up the stairs and everyone saw. You wanted to disappear.',
    'Your stomach growled loudly during a quiet test. It echoed through the whole room.',
  ];
  const message = messages[Math.floor(Math.random() * messages.length)];

  if (check) {
    player.events.add(fname);
  }

  return createMessageEvent(fname, message, player, check);
}

/**
 * First overnight party with friends (ages 12-15)
 */
export function sleepover(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'sleepover';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 12 &&
    ageYears <= 15 &&
    checkProbability(1000);

  const message = "You're at a sleepover and everyone wants to play truth or dare. Do you play?";
  const answerOptions = [
    createAnswerOption('Yes, and pick truth'),
    createAnswerOption('Yes, and pick dare', '', 10),
    createAnswerOption('Pretend to fall asleep'),
    createAnswerOption('Suggest a different game', '', 0, 3),
  ];

  if (type !== 'answer') {
    return createQuestionEvent(fname, message, player, check, {
      answerOptions: answerOptions.map((opt) => opt.option),
    });
  } else if (type === 'answer' && response) {
    player.askedQuestions.add(fname);

    if (response.option === 'Yes, and pick truth') {
      player.messageQueue.push(
        'You picked truth and had to share your biggest secret. It was scary but brought you closer to your friends!'
      );
      player.character.social = (player.character.social ?? 0) + 5;
    } else if (response.option === 'Yes, and pick dare') {
      player.messageQueue.push(
        'You picked dare and had to prank call someone. Your heart was racing but everyone thought you were so brave!'
      );
      player.character.social = (player.character.social ?? 0) + 10;
    } else if (response.option === 'Pretend to fall asleep') {
      player.messageQueue.push(
        'You pretended to fall asleep. Your friends knew you were faking and thought you were lame.'
      );
      player.character.social = (player.character.social ?? 0) - 10;
      player.character.happiness -= 5;
    } else if (response.option === 'Suggest a different game') {
      player.messageQueue.push(
        'You suggested watching a movie instead. Everyone agreed and you all had a great time!'
      );
      player.character.social = (player.character.social ?? 0) + 5;
    }
  }

  return null;
}

/**
 * Deciding whether to get social media (ages 11-14)
 */
export function socialMediaPressure(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'socialMediaPressure';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 11 &&
    ageYears <= 14 &&
    checkProbability(800);

  const message = 'All your friends are on social media now. Do you want to join?';
  const answerOptions = [
    'Yes, make an account',
    "No, you're not interested",
    'Make a private account',
  ];

  if (type !== 'answer') {
    return createQuestionEvent(fname, message, player, check, { answerOptions });
  } else if (type === 'answer' && response) {
    player.askedQuestions.add(fname);

    if (response.option === 'Yes, make an account') {
      player.messageQueue.push(
        "You made a social media account! You're excited to connect with friends and share your life online."
      );
      player.character.social = (player.character.social ?? 0) + 10;
      player.character.happiness += 5;
      player.character.hasSocialMedia = true;
    } else if (response.option === "No, you're not interested") {
      player.messageQueue.push(
        "You decided social media isn't for you. Your friends don't get it, but you feel good about your choice."
      );
      player.character.social = (player.character.social ?? 0) - 5;
      player.character.happiness += 5;
    } else if (response.option === 'Make a private account') {
      player.messageQueue.push(
        'You made a private account so you can stay connected but keep your privacy. Smart move!'
      );
      player.character.social = (player.character.social ?? 0) + 5;
      player.character.hasSocialMedia = true;
    }
  }

  return null;
}

/**
 * Trying a bold new style (ages 13-17)
 */
export function fashionExperimentation(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'fashionExperimentation';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 13 &&
    ageYears <= 17 &&
    checkProbability(900);

  const message = 'You want to try a completely different fashion style. What do you go for?';
  const answerOptions = ['Edgy and dark', 'Preppy and classic', 'Athletic and casual', 'Stay the same'];

  if (type !== 'answer') {
    return createQuestionEvent(fname, message, player, check, { answerOptions });
  } else if (type === 'answer' && response) {
    player.askedQuestions.add(fname);

    if (response.option === 'Edgy and dark') {
      player.messageQueue.push(
        'You went for an edgy, dark look. Some people stared but you feel confident and unique!'
      );
      player.character.social = (player.character.social ?? 0) + 5;
    } else if (response.option === 'Preppy and classic') {
      player.messageQueue.push(
        'You chose a preppy, classic style. You look put-together and people complimented your new look!'
      );
      player.character.social = (player.character.social ?? 0) + 5;
    } else if (response.option === 'Athletic and casual') {
      player.messageQueue.push(
        "You embraced an athletic, casual style. It's comfortable and sporty - perfect for you!"
      );
      player.character.social = (player.character.social ?? 0) + 5;
    } else if (response.option === 'Stay the same') {
      player.messageQueue.push(
        'You decided to stick with your current style. Better safe than sorry... but you wonder what could have been.'
      );
      player.character.happiness -= 5;
    }
  }

  return null;
}

/**
 * First time behind the wheel (ages 15-17)
 */
export function learningToDrive(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'learningToDrive';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    checkMilestoneProbability(ageYears, 15, 17, 'years');

  const message =
    "Your parent takes you to an empty parking lot to practice driving for the first time. You're nervous and excited!";

  if (check) {
    player.events.add(fname);
  }

  return createMessageEvent(fname, message, player, check);
}
