/**
 * Daily Life Negative Events
 * Minor inconveniences and setbacks that happen in everyday life
 *
 * Events:
 * - ruinedClothes: Ruined your favorite outfit (ages 8-100)
 * - badRestaurantExperience: Terrible restaurant experience (ages 16-100)
 * - vehicleBreakdown: Car or bike broke down at a bad time (ages 16-100)
 */

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

/**
 * Ruined your favorite outfit (ages 8-100)
 */
export function ruinedClothes(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'ruinedClothes';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 8 &&
    ageYears <= 100 &&
    checkProbability(800);

  // Different scenarios based on age
  let scenario: string;
  if (ageYears < 13) {
    scenario =
      'You got mud all over your favorite outfit while playing outside. Your parents are not happy.';
  } else if (ageYears < 18) {
    scenario =
      'You spilled food on your favorite shirt right before an important event. It is completely ruined.';
  } else {
    scenario =
      'Your favorite piece of clothing got destroyed in the laundry. It shrunk and the colors ran.';
  }

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

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

/**
 * Terrible restaurant experience (ages 16-100)
 */
export function badRestaurantExperience(
  player: Player,
  _type: 'message' | 'question' = 'message'
): EventResult {
  const fname = 'badRestaurantExperience';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 16 &&
    ageYears <= 100 &&
    checkProbability(1000);

  const scenarios = [
    'You went to a restaurant and the service was terrible. The food was cold, the waiter was rude, and you still had to pay full price.',
    'You found a hair in your food at a restaurant. When you complained, the manager was dismissive and unhelpful.',
    'You waited over an hour for your food at a restaurant, only to receive the wrong order. By the time they fixed it, you had lost your appetite.',
  ];
  const scenario = scenarios[Math.floor(Math.random() * scenarios.length)];

  if (check) {
    player.events.add(fname);
    player.character.happiness -= 15;
    player.character.stress = (player.character.stress ?? 0) + 10;
    // Lost money on bad meal
    if (player.character.money >= 50) {
      player.character.money -= 50;
    }
  }

  return createMessageEvent(fname, scenario, player, check, { moneyCost: 50 });
}

/**
 * Vehicle broke down at a bad time (ages 16-100)
 */
export function vehicleBreakdown(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'vehicleBreakdown';
  const ageYears = player.character.ageYears;

  // Check if player has a vehicle (car or otherwise)
  const hasVehicle = (player.character.items ?? []).some(
    (item) =>
      item.name?.toLowerCase().includes('car') ||
      item.name?.toLowerCase().includes('bike') ||
      item.name?.toLowerCase().includes('motorcycle')
  );

  const check =
    !player.events.has(fname) &&
    ageYears >= 16 &&
    ageYears <= 100 &&
    hasVehicle &&
    checkProbability(1200);

  const scenarios = [
    'Your car broke down on the highway during rush hour. You had to wait hours for a tow truck.',
    'Your car would not start this morning. You missed an important appointment and now need to pay for repairs.',
    'Your vehicle broke down in the middle of nowhere. You had to walk miles to find help.',
  ];
  const scenario = scenarios[Math.floor(Math.random() * scenarios.length)];

  if (check) {
    player.events.add(fname);
    player.character.happiness -= 25;
    player.character.stress = (player.character.stress ?? 0) + 30;
    deductEnergy(player.character, 20);
    // Repair costs
    if (player.character.money >= 500) {
      player.character.money -= 500;
    } else {
      player.character.money = 0;
      player.character.stress = (player.character.stress ?? 0) + 15; // Extra stress from no money
    }
  }

  return createMessageEvent(fname, scenario, player, check, { moneyCost: 500 });
}

/**
 * Minor accidents and mishaps in daily life (ages 5-100)
 */
export function dailyMishap(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'dailyMishap';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 5 &&
    ageYears <= 100 &&
    checkProbability(500);

  // Different mishaps based on age
  let mishap: string;
  if (ageYears < 10) {
    const mishaps = [
      'You scraped your knee while playing. It really hurts!',
      'You got stung by a bee. Your arm is all swollen.',
      'You stepped in a puddle and got your shoes all wet.',
    ];
    mishap = mishaps[Math.floor(Math.random() * mishaps.length)];
  } else if (ageYears < 18) {
    const mishaps = [
      'You stubbed your toe really hard on the furniture. The pain is excruciating.',
      'You got a paper cut that will not stop stinging.',
      'You bit your tongue while eating. Now you cannot enjoy your food.',
    ];
    mishap = mishaps[Math.floor(Math.random() * mishaps.length)];
  } else {
    const mishaps = [
      'You stepped on a LEGO in the dark. The pain was incredible.',
      'You bumped your head on an open cabinet door. Now you have a headache.',
      'You spilled coffee all over your keyboard. You are having a rough day.',
    ];
    mishap = mishaps[Math.floor(Math.random() * mishaps.length)];
  }

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

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

/**
 * Weather ruined plans (ages 8-100)
 */
export function weatherRuinedPlans(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'weatherRuinedPlans';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 8 &&
    ageYears <= 100 &&
    checkProbability(900);

  const scenarios = [
    'You had outdoor plans but it started pouring rain. Everything got cancelled.',
    'A sudden storm ruined the event you were looking forward to all week.',
    'The weather was so bad that you got stuck at home when you wanted to go out.',
  ];
  const scenario = scenarios[Math.floor(Math.random() * scenarios.length)];

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

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

/**
 * Lost or had wallet/phone stolen (ages 12-100)
 */
export function lostBelongings(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'lostBelongings';
  const ageYears = player.character.ageYears;

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

  const scenarios = [
    'You lost your phone somewhere. You have been searching everywhere but cannot find it.',
    'You think someone stole your wallet. All your cards and cash are gone.',
    'You left your bag somewhere and when you went back, it was gone.',
  ];
  const scenario = scenarios[Math.floor(Math.random() * scenarios.length)];

  if (check) {
    player.events.add(fname);
    player.character.happiness -= 30;
    player.character.stress = (player.character.stress ?? 0) + 35;
    // Lost some money
    const lostAmount = Math.min(player.character.money, 200);
    player.character.money -= lostAmount;
  }

  return createMessageEvent(fname, scenario, player, check, { moneyCost: 200 });
}

/**
 * Got sick with a cold or flu (ages 3-100)
 */
export function caughtCold(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'caughtCold';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 3 &&
    ageYears <= 100 &&
    checkProbability(800);

  const message =
    'You caught a cold. Your nose is stuffy, your throat hurts, and you feel miserable. You will need to rest.';

  if (check) {
    player.events.add(fname);
    player.character.health = modifyStat(player.character.health, -15);
    player.character.energy = modifyStat(player.character.energy, -25);
    player.character.happiness -= 15;
  }

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

/**
 * Locked yourself out (ages 12-100)
 */
export function lockedOut(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'lockedOut';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 12 &&
    ageYears <= 100 &&
    checkProbability(1000);

  const message =
    'You locked yourself out of your home. You had to wait outside for hours until someone could help you get back in.';

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

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

/**
 * Technology failure at a critical moment (ages 10-100)
 */
export function techFailure(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'techFailure';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 10 &&
    ageYears <= 100 &&
    checkProbability(900);

  // Different tech failures based on context
  let scenario: string;
  if (ageYears < 18 && player.character.occupation === 'student') {
    scenario =
      'Your computer crashed right before you finished your assignment. You lost hours of work and have to start over.';
  } else if (player.character.occupation === 'work') {
    scenario =
      'Your laptop died in the middle of an important presentation. It was really embarrassing and unprofessional.';
  } else {
    scenario =
      'Your phone battery died when you needed it most. You missed important calls and messages.';
  }

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

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