/**
 * Health & Wellness Issue Events (Negative)
 * Health-related negative events and wellness setbacks
 *
 * NOTE: Some health events exist in health/events.ts:
 * - injuryFromAccident, chronicPain, dentalEmergency, seriousIllness
 *
 * Events in this file:
 * - weightGain: Significant weight gain (ages 16-100)
 * - sleepDeprivation: Extended period of poor sleep (ages 14-100)
 * - addictionProblem: Developing unhealthy dependency (ages 16-100) - questionEvent
 */

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

/**
 * Significant weight gain affecting comfort and self-image (ages 16-100)
 */
export function weightGain(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'weightGain';
  const ageYears = player.character.ageYears;

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

  const message = "You've gained significant weight. Your clothes don't fit and you feel uncomfortable.";

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

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

/**
 * Extended period of poor sleep affecting everything (ages 14-100)
 */
export function sleepDeprivation(player: Player, _type: 'message' | 'question' = 'message'): EventResult {
  const fname = 'sleepDeprivation';
  const ageYears = player.character.ageYears;

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

  const message = "You haven't been sleeping well for weeks. Everything is harder now.";

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

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

/**
 * Developing an unhealthy dependency affecting life (ages 16-100) - questionEvent
 */
export function addictionProblem(
  player: Player,
  type: 'message' | 'question' | 'answer' = 'message',
  response?: { option: string }
): EventResult {
  const fname = 'addictionProblem';
  const ageYears = player.character.ageYears;

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

  const message = "You've developed an unhealthy dependency. It's affecting your life.";
  const answerOptions = [
    createAnswerOption('Seek professional help', '0', 30, 0, 1000),
    createAnswerOption('Try to quit alone', '1', 50),
    createAnswerOption('Ignore the problem', '2'),
  ];

  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 === 'Seek professional help') {
      player.messageQueue.push(
        "You seek professional help to address your dependency. It's difficult and expensive, but you're taking the right steps toward recovery."
      );
      player.character.happiness += 10;
    } else if (response.option === 'Try to quit alone') {
      player.messageQueue.push(
        "You try to quit on your own. It's incredibly difficult and emotionally draining, but you're determined to break free."
      );
      player.character.happiness -= 20;
    } else if (response.option === 'Ignore the problem') {
      player.messageQueue.push(
        'You ignore the problem and continue as before. Your health and happiness continue to deteriorate.'
      );
      player.character.health = modifyStat(player.character.health, -30);
      player.character.happiness = modifyStat(player.character.happiness, -30);
    }
  }

  return null;
}
