/**
 * Family Conflict Events
 * Negative events related to family relationships and conflicts
 *
 * Events:
 * - parentDivorce: Parents getting divorced (ages 5-18)
 * - siblingRivalry: Parents favoring sibling (ages 5-25)
 * - familyEstrangement: Major falling out with family (ages 18-100)
 * - parentIllness: Parent diagnosed with serious illness (ages 20-100)
 * - familyDebt: Family member needs financial help (ages 18-100)
 * - inheritanceDispute: Family fighting over inheritance (ages 30-100)
 */

import { Player } from '../../models/Player.js';
import { Person } from '../../models/Person.js';
import {
  createMessageEvent,
  checkProbability,
  type EventResult,
} from '../base.js';
import { getAllFamily, getRelationship } from './activities.js';

/**
 * Extended person type that includes optional title property
 */
type ExtendedPerson = Person & { title?: string };

// ============================================================================
// Negative Family Events
// ============================================================================

/**
 * Parents getting divorced (ages 5-18)
 * Major negative life event that impacts happiness and family relationships
 */
export function parentDivorce(
  player: Player,
  _type: 'message' | 'question' = 'message'
): EventResult {
  const fname = 'parentDivorce';
  const ageYears = player.c.ageYears;

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

  const message =
    'Your parents are getting divorced. Your whole world feels unstable.';

  if (check) {
    player.events.add(fname);
    player.c.happiness = Math.max(0, (player.c.happiness ?? 50) - 40);
    player.c.stress = Math.min(100, (player.c.stress ?? 0) + 35);

    // Decrease affinity with both parents
    const mother = getRelationship(player, 'mother');
    const father = getRelationship(player, 'father');

    if (mother) {
      mother.affinity = Math.max(-100, (mother.affinity ?? 50) - 20);
    }
    if (father) {
      father.affinity = Math.max(-100, (father.affinity ?? 50) - 20);
    }
  }

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

/**
 * Parents clearly favoring sibling (ages 5-25)
 * Causes resentment toward parents and sibling
 */
export function siblingRivalry(
  player: Player,
  _type: 'message' | 'question' = 'message'
): EventResult {
  const fname = 'siblingRivalry';
  const ageYears = player.c.ageYears;

  // Find siblings
  const siblings = (player.r ?? []).filter((person: Person) => {
    const relationships = person.relationships ?? [];
    const extPerson = person as ExtendedPerson;
    const title = extPerson.title?.toLowerCase() ?? '';
    return (
      relationships.includes('sibling') ||
      relationships.includes('brother') ||
      relationships.includes('sister') ||
      title.includes('sibling') ||
      title.includes('brother') ||
      title.includes('sister')
    );
  });

  const check =
    !player.events.has(fname) &&
    ageYears >= 5 &&
    ageYears <= 25 &&
    siblings.length > 0 &&
    checkProbability(2000);

  const message =
    'Your parents are clearly favoring your sibling. You feel overlooked and undervalued.';

  if (check) {
    player.events.add(fname);
    player.c.happiness = Math.max(0, (player.c.happiness ?? 50) - 25);

    // Decrease affinity with parents
    const mother = getRelationship(player, 'mother');
    const father = getRelationship(player, 'father');

    if (mother) {
      mother.affinity = Math.max(-100, (mother.affinity ?? 50) - 15);
    }
    if (father) {
      father.affinity = Math.max(-100, (father.affinity ?? 50) - 15);
    }

    // Decrease affinity with random sibling
    if (siblings.length > 0) {
      const sibling = siblings[Math.floor(Math.random() * siblings.length)];
      sibling.affinity = Math.max(-100, (sibling.affinity ?? 50) - 20);
    }
  }

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

/**
 * Major falling out with family members (ages 18-100)
 * Results in estrangement from entire family
 */
export function familyEstrangement(
  player: Player,
  _type: 'message' | 'question' = 'message'
): EventResult {
  const fname = 'familyEstrangement';
  const ageYears = player.c.ageYears;

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

  const message =
    "You've had a major falling out with family members. You're not speaking anymore.";

  if (check) {
    player.events.add(fname);
    player.c.happiness = Math.max(0, (player.c.happiness ?? 50) - 35);
    player.c.stress = Math.min(100, (player.c.stress ?? 0) + 30);

    // Decrease affinity with all family members
    const family = getAllFamily(player);
    for (const member of family) {
      member.affinity = Math.max(-100, (member.affinity ?? 50) - 40);
    }
  }

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

/**
 * Parent diagnosed with serious illness (ages 20-100)
 * Emotional toll on player when parent becomes ill
 */
export function parentIllness(
  player: Player,
  _type: 'message' | 'question' = 'message'
): EventResult {
  const fname = 'parentIllness';
  const ageYears = player.c.ageYears;

  // Check if player has at least one parent
  const mother = getRelationship(player, 'mother');
  const father = getRelationship(player, 'father');
  const hasParent = mother !== null || father !== null;

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

  const message =
    "Your parent has been diagnosed with a serious illness. You're worried and scared.";

  if (check) {
    player.events.add(fname);
    player.c.happiness = Math.max(0, (player.c.happiness ?? 50) - 30);
    player.c.stress = Math.min(100, (player.c.stress ?? 0) + 40);
    player.c.energy = Math.max(0, player.c.energy - 20);
  }

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

/**
 * Family member needs financial help (ages 18-100)
 * Family emergency puts strain on player's finances
 */
export function familyDebt(
  player: Player,
  _type: 'message' | 'question' = 'message'
): EventResult {
  const fname = 'familyDebt';
  const ageYears = player.c.ageYears;

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

  const message =
    'A family member needs financial help for an emergency. You feel obligated to help.';

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

    if ((player.c.money ?? 0) >= 1000) {
      player.c.money = (player.c.money ?? 0) - 1000;
    } else {
      // Can't afford full amount
      player.c.money = 0;
      player.c.stress = Math.min(100, (player.c.stress ?? 0) + 20); // Additional stress from financial ruin
    }
    player.c.stress = Math.min(100, (player.c.stress ?? 0) + 20);
    player.c.happiness = Math.max(0, (player.c.happiness ?? 50) - 15);
  }

  return createMessageEvent(fname, message, player, check, {
    moneyCost: Math.min(player.c.money ?? 0, 1000),
  });
}

/**
 * Family fighting over inheritance (ages 30-100)
 * Greed tears family apart
 */
export function inheritanceDispute(
  player: Player,
  _type: 'message' | 'question' = 'message'
): EventResult {
  const fname = 'inheritanceDispute';
  const ageYears = player.c.ageYears;

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

  const message =
    'Family members are fighting over inheritance. Relationships are being destroyed.';

  if (check) {
    player.events.add(fname);
    player.c.happiness = Math.max(0, (player.c.happiness ?? 50) - 25);
    player.c.stress = Math.min(100, (player.c.stress ?? 0) + 30);

    // Decrease affinity with all family members
    const family = getAllFamily(player);
    for (const member of family) {
      member.affinity = Math.max(-100, (member.affinity ?? 50) - 30);
    }
  }

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