/**
 * School Negative Events
 * Negative events that occur during school years
 *
 * NOTE: Many school events already exist in education/schoolLife.ts and education/quickWins.ts
 * This file contains additional school-specific negative events not covered elsewhere.
 *
 * Events:
 * - bullying: Being bullied by other students (ages 6-18, student) - questionEvent with choices
 */

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

/**
 * Being bullied by other students (ages 6-18, student)
 * This is a question event that allows the player to choose how to respond
 */
export function bullying(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'bullying';
  const ageYears = player.character.ageYears;

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

  const message =
    'Some kids at school have been picking on you, making fun of you, and pushing you around. What do you do?';
  const answerOptions = [
    createAnswerOption('Tell a teacher or adult', '0'),
    createAnswerOption('Stand up to them', '1'),
    createAnswerOption('Ignore them and hope it stops', '2'),
    createAnswerOption('Tell your parents', '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 === 'Tell a teacher or adult') {
      player.messageQueue.push(
        'You told a teacher about the bullying. They promised to handle it. The bullying decreases but some kids call you a snitch.'
      );
      player.character.happiness -= 10;
      player.character.social = modifyStat(player.character.social, -5);
    } else if (response.option === 'Stand up to them') {
      player.messageQueue.push(
        'You stood up to the bullies. It was scary, but they seem to respect you more now. The bullying mostly stopped.'
      );
      player.character.happiness += 5;
      player.character.stress = (player.character.stress ?? 0) + 15;
    } else if (response.option === 'Ignore them and hope it stops') {
      player.messageQueue.push(
        "You tried to ignore the bullying, but it continued. It's really affecting your mood and you dread going to school."
      );
      player.character.happiness -= 30;
      player.character.stress = (player.character.stress ?? 0) + 30;
      deductEnergy(player.character, 15);
    } else if (response.option === 'Tell your parents') {
      player.messageQueue.push(
        'You told your parents about the bullying. They contacted the school and the situation improved.'
      );
      player.character.happiness -= 5;
      // Increase affinity with parents
      const relationships = player.relationships ?? [];
      for (const person of relationships) {
        const title = person.title?.toLowerCase() ?? '';
        if (title === 'mother' || title === 'father') {
          person.affinity = (person.affinity ?? 0) + 10;
        }
      }
    }
  }

  return null;
}
