/**
 * Negative Academic Events
 * Academic setbacks and challenges for students
 *
 * NOTE: Some academic events exist in other modules:
 * - education/quickWins.ts: lostHomework
 * - adolescence/lifeEvents.ts: groupProjectDrama
 *
 * Events in this file:
 * - failedTest: Failed an important test (ages 10-22, student)
 * - rejectedFromCollege: Dream college rejection (ages 17-18)
 * - academicProbation: Low GPA, academic probation (ages 18-22, college)
 * - groupProjectBetrayal: Group members didn't contribute (ages 14-22, student)
 * - plagiarismAccusation: Accused of plagiarism (ages 14-22, student) - questionEvent
 */

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

/**
 * Failed an important test (ages 10-22, student)
 */
export function failedTest(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'failedTest';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    player.character.occupation === 'student' &&
    ageYears >= 10 &&
    ageYears <= 22 &&
    checkProbability(800);

  const message = 'You failed an important test. Your grade is going to suffer.';

  if (check) {
    player.events.add(fname);
    player.character.happiness -= 20;
    player.character.stress = (player.character.stress ?? 0) + 10;
    deductEnergy(player.character, 15);
  }

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

/**
 * Dream college rejection letter (ages 17-18)
 */
export function rejectedFromCollege(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'rejectedFromCollege';
  const ageYears = player.character.ageYears;

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

  const message = 'You got a rejection letter from your dream college. It hurts.';

  if (check) {
    player.events.add(fname);
    player.character.happiness -= 30;
    deductEnergy(player.character, 20);
  }

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

/**
 * Placed on academic probation for low GPA (ages 18-22, college)
 */
export function academicProbation(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'academicProbation';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    player.character.occupation === 'student' &&
    (player.character.education ?? '').includes('college') &&
    ageYears >= 18 &&
    ageYears <= 22 &&
    checkProbability(2000);

  const message = "Your GPA dropped too low. You've been placed on academic probation.";

  if (check) {
    player.events.add(fname);
    player.character.happiness -= 35;
    player.character.stress = (player.character.stress ?? 0) + 40;
  }

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

/**
 * Group members didn't do their part and blamed you (ages 14-22, student)
 */
export function groupProjectBetrayal(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'groupProjectBetrayal';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    player.character.occupation === 'student' &&
    ageYears >= 14 &&
    ageYears <= 22 &&
    checkProbability(1000);

  const message =
    "Your group members didn't do their part and blamed you to the teacher. You got a bad grade.";

  if (check) {
    player.events.add(fname);
    player.character.happiness -= 25;
    player.character.social = (player.character.social ?? 0) - 15;
  }

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

/**
 * Accused of plagiarism - must respond with choice (ages 14-22, student)
 */
export function plagiarismAccusation(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'plagiarismAccusation';
  const ageYears = player.character.ageYears;

  const check =
    !player.askedQuestions.has(fname) &&
    player.character.occupation === 'student' &&
    ageYears >= 14 &&
    ageYears <= 22 &&
    checkProbability(3000);

  const message = "You're accused of plagiarism on an assignment. How do you respond?";
  const answerOptions = [
    createAnswerOption('Prove it was your own work', '0', 30),
    createAnswerOption('Accept partial blame', '1'),
    createAnswerOption('Fight the accusation', '2', 40),
  ];

  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 === 'Prove it was your own work') {
      player.messageQueue.push(
        'You spent hours gathering evidence and defending your work. The accusation was dropped, but it was exhausting and stressful.'
      );
      player.character.stress = (player.character.stress ?? 0) + 20;
    } else if (response.option === 'Accept partial blame') {
      player.messageQueue.push(
        "You admitted to poor citation practices. It's over quickly, but your grade and confidence took a hit."
      );
      player.character.happiness -= 15;
      player.character.stress = (player.character.stress ?? 0) + 15;
    } else if (response.option === 'Fight the accusation') {
      player.messageQueue.push(
        'You fought back aggressively. The process was draining, and your relationships with teachers and classmates suffered.'
      );
      player.character.social = (player.character.social ?? 0) - 20;
    }
  }

  return null;
}
