/**
 * Negative Financial Events
 * Financial problems and setbacks that add realism and challenge
 *
 * Events:
 * - carBreakdown: Car breaks down requiring expensive repairs
 * - scammed: Fell for a scam and lost money
 * - identityTheft: Identity stolen, resulting in financial and stress consequences
 * - lostJob: Laid off from work
 * - rentalEviction: Being evicted from rental property
 * - taxAudit: IRS tax audit
 * - investmentLoss: Investments tanked and lost money
 */

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

/**
 * Car breaks down requiring expensive repairs (ages 16-100, has car)
 */
export function carBreakdown(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'carBreakdown';
  const ageYears = player.character.ageYears;

  // Check if player has a car
  const hasCar = (player.character.items ?? []).some((item) =>
    item.name?.includes('Car')
  );

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

  const message = 'Your car broke down and repairs are expensive.';

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

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

/**
 * Fell for a scam and lost money (ages 18-100)
 */
export function scammed(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'scammed';
  const ageYears = player.character.ageYears;

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

  const message = 'You fell for a scam and lost money. You feel stupid.';

  if (check) {
    player.events.add(fname);
    if (player.character.money >= 500) {
      player.character.money -= 500;
    } else {
      player.character.money = 0;
      player.character.stress = (player.character.stress ?? 0) + 20;
    }
    player.character.happiness -= 25;
    player.character.happiness -= 15; // Additional for confidence loss
  }

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

/**
 * Identity stolen with financial consequences (ages 18-100)
 */
export function identityTheft(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'identityTheft';
  const ageYears = player.character.ageYears;

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

  const message = 'Someone stole your identity and racked up charges. What a nightmare.';

  if (check) {
    player.events.add(fname);
    if (player.character.money >= 1000) {
      player.character.money -= 1000;
    } else {
      player.character.money = 0;
      player.character.stress = (player.character.stress ?? 0) + 20;
    }
    player.character.stress = (player.character.stress ?? 0) + 40;
    player.character.happiness -= 30;
  }

  return createMessageEvent(fname, message, player, check, { energyCost: 50 });
}

/**
 * Laid off from job (ages 18-65, working)
 */
export function lostJob(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'lostJob';
  const ageYears = player.character.ageYears;

  const check =
    !player.events.has(fname) &&
    ageYears >= 18 &&
    ageYears <= 65 &&
    player.character.occupation === 'work' &&
    player.character.job &&
    checkProbability(2000);

  const message = "You were laid off from your job. You're worried about making ends meet.";

  if (check) {
    player.events.add(fname);
    // Remove job
    player.character.job = undefined;
    player.character.occupation = 'unemployed';
    if (player.character.money >= 500) {
      player.character.money -= 500;
    } else {
      player.character.money = 0;
      player.character.stress = (player.character.stress ?? 0) + 20;
    }
    player.character.happiness -= 40;
    player.character.stress = (player.character.stress ?? 0) + 40;
  }

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

/**
 * Being evicted from rental property (ages 18-40, renting)
 */
export function rentalEviction(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'rentalEviction';
  const ageYears = player.character.ageYears;

  // Check if likely renting (no house in items)
  const hasHouse = (player.character.items ?? []).some((item) =>
    ['Luxury Villa', 'Personal Island'].includes(item.name ?? '')
  );
  const likelyRenting = !hasHouse && ageYears >= 18 && ageYears <= 40;

  const check =
    !player.events.has(fname) &&
    ageYears >= 18 &&
    ageYears <= 100 &&
    likelyRenting &&
    checkProbability(3000);

  const message = 'Your landlord is evicting you. You need to find a new place fast.';

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

  return createMessageEvent(fname, message, player, check, { moneyCost: 1500 });
}

/**
 * IRS tax audit (ages 22-100)
 */
export function taxAudit(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'taxAudit';
  const ageYears = player.character.ageYears;

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

  const message = "You're being audited by the IRS. This is going to be expensive and stressful.";

  if (check) {
    player.events.add(fname);
    if (player.character.money >= 800) {
      player.character.money -= 800;
    } else {
      player.character.money = 0;
      player.character.stress = (player.character.stress ?? 0) + 20;
    }
    player.character.stress = (player.character.stress ?? 0) + 35;
  }

  return createMessageEvent(fname, message, player, check, { energyCost: 30 });
}

/**
 * Investments tanked and lost money (ages 25-100, has investments)
 */
export function investmentLoss(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'investmentLoss';
  const ageYears = player.character.ageYears;

  // Proxy: has investments if money > $10,000
  const hasInvestments = player.character.money > 10000;

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

  const message = 'Your investments tanked. You lost a significant amount of money.';

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

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