/**
 * Adulthood Life Events
 * Everyday adult experiences, challenges, and milestones (ages 18+)
 */

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

export function firstApartment(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'firstApartment';
  const check = !player.askedQuestions.has(fname) &&
    player.character.ageYears >= 18 &&
    player.character.ageYears <= 25 &&
    checkProbability(2000);

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

  return createQuestionEvent(
    fname,
    "You're looking at your first apartment. It's small and needs work, but it's yours. Do you take it?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Sign the lease!', '', 0, 0, 500),
        createAnswerOption('Keep looking'),
        createAnswerOption('Find a roommate first', '', 0, 0, 250),
        createAnswerOption('Stay with parents longer')
      ]
    }
  );
}

export function workLifeBalance(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'workLifeBalance';
  const check = !player.askedQuestions.has(fname) &&
    player.character.ageYears >= 25 &&
    player.character.ageYears <= 50 &&
    player.character.occupation === 'work' &&
    !!player.character.job &&
    checkProbability(3000);

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

  return createQuestionEvent(
    fname,
    "Your boss offers overtime pay, but you have plans with friends. What do you choose?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Take the overtime', '', 20, 0, 0),
        createAnswerOption('Keep your plans'),
        createAnswerOption('Split the difference', '', 10, 0, 0)
      ]
    }
  );
}

export function forgotBirthdayCall(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'forgotBirthdayCall';
  const check = !player.events.has(fname) &&
    player.character.ageYears >= 22 &&
    player.character.ageYears <= 65 &&
    checkProbability(5000);

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

  return createMessageEvent(
    fname,
    "You realize you forgot to call your mom on her birthday. You feel terrible and quickly send a late message.",
    player,
    check
  );
}

export function friendsDrifting(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'friendsDrifting';
  const check = !player.events.has(fname) &&
    player.character.ageYears >= 25 &&
    player.character.ageYears <= 40 &&
    checkProbability(4000);

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

  return createMessageEvent(
    fname,
    "You scroll through your phone and realize you haven't talked to your best friend from college in over a year. When did you get so busy?",
    player,
    check
  );
}

export function unexpectedBill(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'unexpectedBill';
  const check = !player.events.has(fname) &&
    player.character.ageYears >= 20 &&
    player.character.ageYears <= 65 &&
    checkProbability(3000);

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

    // Random bill messages
    const messages = [
      "Your car needs $800 in repairs. Ouch.",
      "The medical bill is way higher than expected. There goes your savings.",
      "You forgot to pay insurance and now there's a late fee plus penalties.",
      "Your laptop died and needs replacing. Why does this always happen at the worst time?"
    ];
    const message = messages[Math.floor(Math.random() * messages.length)];

    // Random cost between 200 and 800
    const cost = Math.floor(Math.random() * 600) + 200;
    player.character.money = Math.max(0, player.character.money - cost);
    player.character.happiness -= 15;

    return createMessageEvent(fname, message, player, true, { moneyCost: cost });
  }

  return null;
}

export function promotionOpportunity(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'promotionOpportunity';
  const check = !player.askedQuestions.has(fname) &&
    player.character.ageYears >= 25 &&
    player.character.ageYears <= 55 &&
    player.character.occupation === 'work' &&
    !!player.character.job &&
    checkProbability(4000);

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

  return createQuestionEvent(
    fname,
    "You're offered a promotion with more money but longer hours. Accept?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Yes, take promotion', '', 30, 0, 0),
        createAnswerOption('No, stay where you are'),
        createAnswerOption('Negotiate better terms', '', 20, 10, 0)
      ]
    }
  );
}

export function agingParent(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'agingParent';
  const check = !player.askedQuestions.has(fname) &&
    player.character.ageYears >= 35 &&
    player.character.ageYears <= 60 &&
    checkProbability(5000);

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

  return createQuestionEvent(
    fname,
    "Your parent is getting older and needs help. How do you support them?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Visit more often', '', 10, 0, 0),
        createAnswerOption('Hire outside help', '', 0, 0, 500),
        createAnswerOption('Move them in with you', '', 20, 0, 200),
        createAnswerOption('Encourage siblings to help', '', 0, 5, 0)
      ]
    }
  );
}

export function careerChangeDesire(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'careerChangeDesire';
  const check = !player.askedQuestions.has(fname) &&
    player.character.ageYears >= 30 &&
    player.character.ageYears <= 50 &&
    player.character.occupation === 'work' &&
    !!player.character.job &&
    checkProbability(5000);

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

  return createQuestionEvent(
    fname,
    "You're tired of your job and dream of doing something different. Do you make a change?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Quit and pursue passion', '', 50, 0, 1000),
        createAnswerOption('Stay in current job'),
        createAnswerOption('Side hustle first', '', 20, 0, 0),
        createAnswerOption('Go back to school', '', 0, 0, 5000)
      ]
    }
  );
}

export function coworkerRivalry(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'coworkerRivalry';
  const check = !player.events.has(fname) &&
    player.character.ageYears >= 25 &&
    player.character.ageYears <= 60 &&
    player.character.occupation === 'work' &&
    !!player.character.job &&
    checkProbability(4000);

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

  return createMessageEvent(
    fname,
    "A new coworker is getting all the attention. You feel competitive and a little jealous.",
    player,
    check
  );
}

export function divorceConsideration(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'divorceConsideration';
  // Note: In full implementation, would check partner affinity < 20
  const check = !player.askedQuestions.has(fname) &&
    player.character.ageYears >= 25 &&
    player.character.ageYears <= 65 &&
    !!player.character.partner &&
    checkProbability(6000);

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

  return createQuestionEvent(
    fname,
    "Your marriage isn't what it used to be. Do you try to fix it or let go?",
    player,
    check,
    {
      answerOptions: [
        createAnswerOption('Couples therapy', '', 30, 10, 500),
        createAnswerOption('Separate for now', '', 40, 0, 0),
        createAnswerOption('Divorce', '', 50, 0, 2000),
        createAnswerOption('Keep trying alone', '', 20, 0, 0)
      ]
    }
  );
}
