/**
 * School Social Events
 * Realtime social events that happen during school hours (ages 5-17)
 * Cafeteria choices, tutoring, cheating dilemmas, found wallet, etc.
 */

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

// ──────────────────────────────────────────────
// Tutoring Request
// ──────────────────────────────────────────────

export function tutoringRequest(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'tutoringRequest';
  const check = !player.askedQuestions.has(fname) &&
    player.character.location.includes('school') &&
    player.character.ageYears >= 10 &&
    player.character.ageYears <= 17 &&
    (player.character.intelligence ?? 50) >= 50 &&
    checkProbability(250);

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

  const tutoringVariants = [
    "A classmate who's been struggling asks if you can help them study after school. They look really stressed about the upcoming test.",
    "Someone from your class catches you in the hall: \"You're really good at this subject. Could you help me study?\"",
    "A classmate slides a note across your desk: \"Please help me study, I'm going to fail this test.\"",
  ];

  return createQuestionEvent(
    fname,
    pickVariant(tutoringVariants),
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Help them for free - they need it', '', 15, 0, 0),
        createAnswerOption('Help them for $10/hour', '', 15, 0, -10),
        createAnswerOption('Point them to the school tutoring center', '', 0, 0, 0),
        createAnswerOption("Say you're too busy", '', 0, 0, 0),
      ],
    }
  );
}

// ──────────────────────────────────────────────
// Cafeteria Social Choice
// ──────────────────────────────────────────────

export function cafeteriaSocialChoice(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'cafeteriaSocialChoice';
  const check = !player.askedQuestions.has(fname) &&
    player.character.location.includes('school') &&
    player.hourOfDay === 12 &&
    player.character.ageYears >= 8 &&
    player.character.ageYears <= 17 &&
    checkProbability(200);

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

  const cafeteriaVariants = [
    "At lunch, you see a new kid sitting alone in the cafeteria. Your usual friends are waving you over to their table. Where do you sit?",
    "You walk into the cafeteria and notice someone eating alone. They look like they could use some company. Your friends are at their usual table.",
    "Lunchtime. A transfer student is sitting by themselves, looking around nervously. Your usual group has saved you a seat.",
  ];

  return createQuestionEvent(
    fname,
    pickVariant(cafeteriaVariants),
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Sit with the new kid and introduce yourself', '', 5, 0, 0),
        createAnswerOption('Invite the new kid to sit with your friends', '', 5, 5, 0),
        createAnswerOption('Sit with your friends as usual', '', 0, 0, 0),
        createAnswerOption('Eat alone in the library', '', 0, 0, 0),
      ],
    }
  );
}

// ──────────────────────────────────────────────
// Cheating Opportunity
// ──────────────────────────────────────────────

export function cheatingOpportunity(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'cheatingOpportunity';
  const check = !player.askedQuestions.has(fname) &&
    player.character.location.includes('school') &&
    player.character.ageYears >= 12 &&
    player.character.ageYears <= 17 &&
    checkProbability(300);

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

  return createQuestionEvent(
    fname,
    "During a big test, the kid next to you slides their paper where you can clearly see the answers. You didn't study enough and this test counts for a lot of your grade. What do you do?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Look away and do your best honestly', '', 15, 0, 0),
        createAnswerOption('Copy a few answers you were unsure about', '', 5, 0, 0),
        createAnswerOption('Copy as many as you can', '', 0, 0, 0),
        createAnswerOption('Tell the teacher someone is trying to share answers', '', 5, 10, 0),
      ],
    }
  );
}

// ──────────────────────────────────────────────
// Found Wallet at School
// ──────────────────────────────────────────────

export function foundWalletSchool(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'foundWalletSchool';
  const amount = Math.floor(Math.random() * 40) + 10;
  const check = !player.askedQuestions.has(fname) &&
    player.character.location.includes('school') &&
    player.character.ageYears >= 8 &&
    player.character.ageYears <= 17 &&
    checkProbability(400);

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

  return createQuestionEvent(
    fname,
    `You find a wallet in the hallway with $${amount} inside and a student ID. What do you do?`,
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Turn it in to the office', '', 5, 0, 0),
        createAnswerOption('Find the person and return it yourself', '', 10, 0, 0),
        createAnswerOption(`Take the $${amount} and toss the wallet`, '', 0, 0, -amount),
        createAnswerOption('Leave it where you found it', '', 0, 0, 0),
      ],
    }
  );
}

// ──────────────────────────────────────────────
// Group Project
// ──────────────────────────────────────────────

export function groupProject(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'groupProject';
  const check = !player.askedQuestions.has(fname) &&
    player.character.occupation === 'student' &&
    player.character.ageYears >= 10 &&
    player.character.ageYears <= 17 &&
    checkProbability(200);

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

  return createQuestionEvent(
    fname,
    "Your teacher assigns a group project worth a big portion of your grade. You're grouped with two classmates: one who never does any work, and one who's super bossy. How do you approach it?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Take charge and delegate tasks fairly', '', 15, 0, 0),
        createAnswerOption('Do all the work yourself to guarantee a good grade', '', 25, 0, 0),
        createAnswerOption('Go with the flow and let the bossy one lead', '', 5, 0, 0),
        createAnswerOption('Talk to the teacher about switching groups', '', 5, 5, 0),
      ],
    }
  );
}

// ──────────────────────────────────────────────
// Locker Room Gossip
// ──────────────────────────────────────────────

export function lockerRoomGossip(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'lockerRoomGossip';
  const check = !player.askedQuestions.has(fname) &&
    player.character.location.includes('school') &&
    player.character.ageYears >= 12 &&
    player.character.ageYears <= 17 &&
    checkProbability(350);

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

  return createQuestionEvent(
    fname,
    "You overhear some classmates spreading a juicy rumor about one of your friends. They don't know you're listening. What do you do?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Speak up and defend your friend', '', 10, 0, 0),
        createAnswerOption('Tell your friend privately what you heard', '', 5, 0, 0),
        createAnswerOption('Stay quiet - not your business', '', 0, 0, 0),
        createAnswerOption('Spread the rumor further', '', 0, 0, 0),
      ],
    }
  );
}

// ──────────────────────────────────────────────
// School Talent Show
// ──────────────────────────────────────────────

export function talentShow(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'talentShow';
  const check = !player.askedQuestions.has(fname) &&
    player.character.occupation === 'student' &&
    player.character.ageYears >= 8 &&
    player.character.ageYears <= 16 &&
    checkProbability(250);

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

  return createQuestionEvent(
    fname,
    "The school is hosting a talent show and sign-ups are open! Do you participate?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Sign up for a solo act', '', 15, 0, 10),
        createAnswerOption('Perform with friends as a group', '', 10, 0, 5),
        createAnswerOption('Help backstage instead', '', 10, 0, 0),
        createAnswerOption('Just watch from the audience', '', 0, 0, 0),
      ],
    }
  );
}

// ──────────────────────────────────────────────
// Sports Tryouts
// ──────────────────────────────────────────────

export function sportsTryouts(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'sportsTryouts';
  const check = !player.askedQuestions.has(fname) &&
    player.character.occupation === 'student' &&
    player.character.ageYears >= 10 &&
    player.character.ageYears <= 17 &&
    checkProbability(180);

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

  const sports = ['basketball', 'soccer', 'track', 'swimming', 'volleyball'];
  const sport = sports[Math.floor(Math.random() * sports.length)];

  return createQuestionEvent(
    fname,
    `Tryouts for the school ${sport} team are next week. Are you going for it?`,
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Practice hard and give it your all', '', 20, 0, 0),
        createAnswerOption('Show up casually and see how it goes', '', 10, 0, 0),
        createAnswerOption('Try out but manage expectations', '', 10, 0, 0),
        createAnswerOption('Pass on it this year', '', 0, 0, 0),
      ],
    }
  );
}

// ──────────────────────────────────────────────
// Teacher Conflict
// ──────────────────────────────────────────────

export function teacherConflict(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'teacherConflict';
  const check = !player.askedQuestions.has(fname) &&
    player.character.location.includes('school') &&
    player.character.ageYears >= 12 &&
    player.character.ageYears <= 17 &&
    checkProbability(350);

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

  return createQuestionEvent(
    fname,
    "Your teacher gave you a grade you think is unfair on your last assignment. You worked really hard on it. What do you do?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Politely ask the teacher to explain the grade', '', 5, 0, 0),
        createAnswerOption('Accept it and move on', '', 0, 0, 0),
        createAnswerOption('Complain to your parents', '', 0, 5, 0),
        createAnswerOption('Argue with the teacher in front of everyone', '', 10, 0, 0),
      ],
    }
  );
}

// ──────────────────────────────────────────────
// Library Discovery
// ──────────────────────────────────────────────

export function libraryDiscovery(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'libraryDiscovery';
  const check = !player.events.has(fname) &&
    player.character.occupation === 'student' &&
    player.character.ageYears >= 6 &&
    player.character.ageYears <= 14 &&
    checkProbability(350);

  if (check) {
    player.events.add(fname);
    player.c.intelligence = modifyStat(player.c.intelligence, 5);
    player.c.creativity = modifyStat(player.c.creativity, 5);
  }

  const topics = ['dinosaurs', 'space', 'ancient Egypt', 'ocean creatures', 'robots', 'mythology'];
  const topic = topics[Math.floor(Math.random() * topics.length)];

  const libraryVariants = [
    `While browsing the school library, you discovered a fascinating book about ${topic}. You couldn't put it down and ended up reading through the entire recess!`,
    `You stumbled across an amazing book about ${topic} in the library. You were so absorbed you didn't hear the bell ring.`,
    `A book about ${topic} caught your eye in the library. Before you knew it, the whole recess had flown by.`,
  ];

  return createMessageEvent(
    fname,
    pickVariant(libraryVariants),
    player,
    check
  );
}

/**
 * All school social event functions
 */
export const schoolSocialEvents = {
  tutoringRequest,
  cafeteriaSocialChoice,
  cheatingOpportunity,
  foundWalletSchool,
  groupProject,
  lockerRoomGossip,
  talentShow,
  sportsTryouts,
  teacherConflict,
  libraryDiscovery,
};
