/**
 * Childhood Milestone Events
 * Major developmental milestones for young children (ages 0-5)
 *
 * Events:
 * - learnedWalk: Learning to walk (9-24 months)
 * - learningColors: Learning to identify colors (ages 2-3)
 * - lostFirstTooth: Losing first baby tooth (ages 6-8)
 * - lostLastTooth: Losing last baby tooth (ages 10-12)
 * - childLearnedWalk: Player's child learns to walk
 */

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

/**
 * Learning to walk (9-24 months / 270-720 days)
 */
export function learnedWalk(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'learnedWalk';
  const ageDays = player.character.ageDays;

  const check =
    !player.events.has(fname) &&
    checkMilestoneProbability(ageDays, 270, 720, 'days');

  const monthsOld = Math.floor(ageDays / 7 / 4.35);
  const message = `You have learned to walk at ${monthsOld} months`;

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

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

/**
 * Losing first baby tooth (ages 6-8)
 */
export function lostFirstTooth(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'lostFirstTooth';
  const ageYears = player.character.ageYears;

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

  const message = 'You have lost your first baby tooth!';

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

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

/**
 * Losing last baby tooth (ages 10-12)
 */
export function lostLastTooth(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'lostLastTooth';
  const ageYears = player.character.ageYears;

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

  const message = 'You have lost your last baby tooth!';

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

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

/**
 * Learning to identify colors (ages 2-3)
 */
export function learningColors(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'learningColors';
  const ageYears = player.character.ageYears;

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

  const message = "You learned all your colors today! You can't stop pointing at things and naming them: Red truck! Blue sky! Yellow flower!";

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

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

/**
 * Player's child learns to walk
 */
export function childLearnedWalk(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'childLearnedWalk';

  const children = player.character.children;
  if (!children || children.length === 0) {
    return null;
  }

  // Get youngest child
  const child = children.reduce((youngest, current) =>
    current.ageDays < youngest.ageDays ? current : youngest
  );

  const check =
    !player.events.has(fname) &&
    checkMilestoneProbability(child.ageDays, 270, 720, 'days');

  const monthsOld = Math.floor(child.ageDays / 7 / 4.35);
  const message = `Your child ${child.firstname} learned to walk at ${monthsOld} months`;

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

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