/**
 * Early Childhood Events
 * Emotional and social development events for young children (ages 2-7)
 *
 * Events:
 * - firstDayOfPreschool: Emotional reaction to first day of preschool/daycare (ages 3-4)
 * - imaginaryFriend: Creating an imaginary friend (ages 3-5)
 * - firstNightmare: Waking up from a scary dream (ages 3-6)
 * - petGoldfish: Getting a first pet goldfish (ages 4-7)
 * - scaredOfDark: Fear of the dark at bedtime (ages 4-6)
 * - firstTimeTyingShoes: Learning to tie shoelaces (ages 5-7)
 * - sandboxDisagreement: Conflict over toys in sandbox/playground (ages 3-5)
 * - pickySomeEater: Going through a picky eating phase (ages 2-4)
 * - firstHaircut: Reaction to first haircut (ages 2-4)
 */

import {
  createMessageEvent,
  createQuestionEvent,
  createAnswerOption,
  checkProbability,
  type EventResult,
} from '../base';
import type { Player } from '../../models/Player';
import { deductEnergy } from '../../utils/statUtils.js';

/**
 * First day of preschool (ages 3-4)
 */
export function firstDayOfPreschool(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'firstDayOfPreschool';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 3 &&
    ageYears <= 4 &&
    checkProbability(500);

  const message = "It's your first day of preschool! How do you feel about leaving your parents?";
  const answerOptions = [
    'Cry and cling to parent',
    'Wave goodbye excitedly!',
    "Hide behind parent's leg",
  ];

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

    if (response.option === answerOptions[0]) {
      player.character.happiness -= 5;
      deductEnergy(player.character, 5);
      player.messageQueue.push(
        "You cried and didn't want your parents to leave, but eventually you calmed down."
      );
    } else if (response.option === answerOptions[1]) {
      player.character.happiness += 5;
      player.character.social = (player.character.social ?? 0) + 5;
      player.messageQueue.push('You were excited to make new friends and explore!');
    } else {
      player.character.happiness -= 3;
      player.messageQueue.push(
        'You were shy at first, but soon warmed up to your new environment.'
      );
    }
  }

  return null;
}

/**
 * Creating an imaginary friend (ages 3-5)
 */
export function imaginaryFriend(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'imaginaryFriend';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 3 &&
    ageYears <= 5 &&
    checkProbability(500);

  const message = "You've created an imaginary friend! What's their name?";
  const answerOptions = [
    'Mr. Whiskers',
    'Rainbow',
    'Shadow',
    "I don't need imaginary friends",
  ];

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

    if (response.option === 'Mr. Whiskers') {
      player.character.happiness += 10;
      player.messageQueue.push('Mr. Whiskers becomes your constant companion in all your adventures!');
    } else if (response.option === 'Rainbow') {
      player.character.happiness += 10;
      player.character.social = (player.character.social ?? 0) + 5;
      player.messageQueue.push('Rainbow is your colorful friend who makes everything more fun!');
    } else if (response.option === 'Shadow') {
      player.character.happiness += 10;
      player.messageQueue.push('Shadow follows you everywhere and keeps you company!');
    } else {
      player.character.social = (player.character.social ?? 0) - 5;
      player.messageQueue.push('You prefer to play with real friends instead.');
    }
  }

  return null;
}

/**
 * First nightmare (ages 3-6)
 */
export function firstNightmare(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'firstNightmare';
  const ageYears = player.character.ageYears;

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

  const message =
    'You woke up crying from a bad dream. Your parents comfort you and leave a nightlight on. You feel safe again.';

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

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

/**
 * Getting a pet goldfish (ages 4-7)
 */
export function petGoldfish(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'petGoldfish';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 4 &&
    ageYears <= 7 &&
    checkProbability(500);

  const message = 'Your parents got you a goldfish! What do you name it?';
  const answerOptions = ['Bubbles', 'Nemo', 'Sir Swimsalot', 'Goldie'];

  if (type !== 'answer') {
    return createQuestionEvent(fname, message, player, check, {
      answerOptions: [
        answerOptions[0],
        answerOptions[1],
        createAnswerOption(answerOptions[2], '', 0, 3),
        answerOptions[3],
      ].map((opt) => (typeof opt === 'string' ? opt : opt.option)),
    });
  } else if (type === 'answer' && response) {
    player.askedQuestions.add(fname);

    if (response.option === 'Bubbles') {
      player.character.happiness += 10;
      player.messageQueue.push('Bubbles swims happily in their new home!');
    } else if (response.option === 'Nemo') {
      player.character.happiness += 10;
      player.messageQueue.push('Just like the movie! You love watching Nemo swim around.');
    } else if (response.option === 'Sir Swimsalot') {
      player.character.happiness += 15;
      player.messageQueue.push('Sir Swimsalot is the most distinguished fish in all the land!');
    } else {
      player.character.happiness += 10;
      player.messageQueue.push('Goldie becomes your first pet and you take good care of them!');
    }
  }

  return null;
}

/**
 * Fear of the dark (ages 4-6)
 */
export function scaredOfDark(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'scaredOfDark';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 4 &&
    ageYears <= 6 &&
    checkProbability(500);

  const message = "You're scared of the dark. What helps you feel brave?";
  const answerOptions = [
    'Ask for a nightlight',
    'Sleep with stuffed animal',
    'Check under the bed',
    'Call for parent',
  ];

  if (type !== 'answer') {
    return createQuestionEvent(fname, message, player, check, {
      answerOptions: [
        answerOptions[0],
        answerOptions[1],
        createAnswerOption(answerOptions[2], '', 10),
        answerOptions[3],
      ].map((opt) => (typeof opt === 'string' ? opt : opt.option)),
    });
  } else if (type === 'answer' && response) {
    player.askedQuestions.add(fname);

    if (response.option === 'Ask for a nightlight') {
      player.character.happiness += 5;
      player.character.energy += 5;
      player.messageQueue.push(
        'The soft glow of the nightlight makes you feel safe and you sleep peacefully.'
      );
    } else if (response.option === 'Sleep with stuffed animal') {
      player.character.happiness += 10;
      player.messageQueue.push(
        'Your favorite stuffed animal keeps you company and protects you from bad dreams!'
      );
    } else if (response.option === 'Check under the bed') {
      player.messageQueue.push(
        'You bravely check under the bed. Nothing there! You feel proud of yourself.'
      );
    } else {
      player.character.happiness += 5;
      player.messageQueue.push(
        'Your parent reassures you that everything is okay and you drift off to sleep.'
      );
    }
  }

  return null;
}

/**
 * Learning to tie shoes (ages 5-7)
 */
export function firstTimeTyingShoes(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'firstTimeTyingShoes';
  const ageYears = player.character.ageYears;

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

  const message = 'After many attempts, you finally tied your shoes by yourself! You feel so grown up.';

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

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

/**
 * Sandbox disagreement (ages 3-5)
 */
export function sandboxDisagreement(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'sandboxDisagreement';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 3 &&
    ageYears <= 5 &&
    checkProbability(500);

  const message = 'Another kid grabs the toy you were playing with. What do you do?';
  const answerOptions = [
    'Grab it back!',
    'Tell a grown-up',
    'Find a different toy',
    'Suggest taking turns',
  ];

  if (type !== 'answer') {
    return createQuestionEvent(fname, message, player, check, {
      answerOptions: [
        answerOptions[0],
        answerOptions[1],
        answerOptions[2],
        createAnswerOption(answerOptions[3], '', 0, 5),
      ].map((opt) => (typeof opt === 'string' ? opt : opt.option)),
    });
  } else if (type === 'answer' && response) {
    player.askedQuestions.add(fname);

    if (response.option === 'Grab it back!') {
      player.character.social = (player.character.social ?? 0) - 5;
      player.character.happiness += 5;
      player.messageQueue.push(
        'You grabbed it back! The other kid starts crying and you get in trouble.'
      );
    } else if (response.option === 'Tell a grown-up') {
      player.character.social = (player.character.social ?? 0) + 5;
      player.messageQueue.push('The grown-up helps you both learn to share nicely.');
    } else if (response.option === 'Find a different toy') {
      player.character.happiness -= 5;
      player.character.social = (player.character.social ?? 0) + 5;
      player.messageQueue.push(
        "You're disappointed, but you find another fun toy to play with instead."
      );
    } else {
      player.character.social = (player.character.social ?? 0) + 15;
      player.messageQueue.push('You both take turns! Great job sharing and making a new friend!');
    }
  }

  return null;
}

/**
 * Picky eating phase (ages 2-4)
 */
export function pickySomeEater(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'pickySomeEater';
  const ageYears = player.character.ageYears;

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

  const message =
    "You refuse to eat anything except chicken nuggets and mac & cheese. Your parents sigh and hope it's just a phase.";

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

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

/**
 * First haircut (ages 2-4)
 */
export function firstHaircut(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'firstHaircut';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    ageYears >= 2 &&
    ageYears <= 4 &&
    checkProbability(500);

  const message = "It's time for your first haircut! How does it go?";
  const answerOptions = ['Cry the whole time', 'Sit very still and brave', "Can't stop wiggling"];

  if (type !== 'answer') {
    return createQuestionEvent(fname, message, player, check, {
      answerOptions: [
        answerOptions[0],
        answerOptions[1],
        createAnswerOption(answerOptions[2], '', 5),
      ].map((opt) => (typeof opt === 'string' ? opt : opt.option)),
    });
  } else if (type === 'answer' && response) {
    player.askedQuestions.add(fname);

    if (response.option === 'Cry the whole time') {
      player.character.happiness -= 10;
      deductEnergy(player.character, 5);
      player.messageQueue.push("You cried through the whole haircut! At least it's over now.");
    } else if (response.option === 'Sit very still and brave') {
      player.character.happiness += 10;
      player.messageQueue.push('You sat so still! The hairdresser said you were the best customer ever!');
    } else {
      player.character.happiness += 5;
      player.messageQueue.push(
        "You couldn't help wiggling around! The haircut is a little uneven but still looks cute."
      );
    }
  }

  return null;
}
