"""
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 random
from events.base import messageFunction


def parentDivorce(player, type='message'):
    """Parents getting divorced - child/teen years"""
    fname = 'parentDivorce'
    check = fname not in player.events and player.c.ageYears >= 5 and player.c.ageYears <= 18 and 1 >= random.random()*3000
    message = "Your parents are getting divorced. Your whole world feels unstable."

    if check:
        player.events.add(fname)
        player.c.happiness -= 40
        player.c.stress += 35

        # Decrease affinity with both parents
        from functions import get_relationship
        mother = get_relationship(player, 'mother')
        father = get_relationship(player, 'father')

        if mother:
            mother.affinity -= 20
        if father:
            father.affinity -= 20

    return messageFunction(fname, message, player, check)


def siblingRivalry(player, type='message'):
    """Parents clearly favoring sibling"""
    fname = 'siblingRivalry'

    # Find siblings
    siblings = []
    for person in player.r:
        if 'sibling' in person.relationships:
            siblings.append(person)

    check = (fname not in player.events and
             player.c.ageYears >= 5 and
             player.c.ageYears <= 25 and
             len(siblings) > 0 and
             1 >= random.random()*2000)

    message = "Your parents are clearly favoring your sibling. You feel overlooked and undervalued."

    if check:
        player.events.add(fname)
        player.c.happiness -= 25

        # Decrease affinity with parents
        from functions import get_relationship
        mother = get_relationship(player, 'mother')
        father = get_relationship(player, 'father')

        if mother:
            mother.affinity -= 15
        if father:
            father.affinity -= 15

        # Decrease affinity with random sibling
        if siblings:
            sibling = random.choice(siblings)
            sibling.affinity -= 20

    return messageFunction(fname, message, player, check)


def familyEstrangement(player, type='message'):
    """Major falling out with family members"""
    fname = 'familyEstrangement'
    check = fname not in player.events and player.c.ageYears >= 18 and player.c.ageYears <= 100 and 1 >= random.random()*4000
    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 -= 35
        player.c.stress += 30

        # Decrease affinity with all family members
        from functions import get_allFamily
        family = get_allFamily(player)
        for member in family:
            member.affinity -= 40

    return messageFunction(fname, message, player, check)


def parentIllness(player, type='message'):
    """Parent diagnosed with serious illness"""
    fname = 'parentIllness'

    # Check if player has at least one parent
    from functions import get_relationship
    mother = get_relationship(player, 'mother')
    father = get_relationship(player, 'father')
    has_parent = mother is not None or father is not None

    check = (fname not in player.events and
             player.c.ageYears >= 20 and
             player.c.ageYears <= 100 and
             has_parent and
             1 >= random.random()*3000)

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

    if check:
        player.events.add(fname)
        player.c.happiness -= 30
        player.c.stress += 40
        player.c.energy -= 20

    return messageFunction(fname, message, player, check)


def familyDebt(player, type='message'):
    """Family member needs financial help"""
    fname = 'familyDebt'
    check = fname not in player.events and player.c.ageYears >= 18 and player.c.ageYears <= 100 and 1 >= random.random()*2500
    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 >= 1000:
            player.c.money -= 1000
        else:
            # Can't afford full amount
            player.c.money = 0
            player.c.stress += 20  # Additional stress from financial ruin
        player.c.stress += 20
        player.c.happiness -= 15

    return messageFunction(fname, message, player, check)


def inheritanceDispute(player, type='message'):
    """Family fighting over inheritance"""
    fname = 'inheritanceDispute'
    check = fname not in player.events and player.c.ageYears >= 30 and player.c.ageYears <= 100 and 1 >= random.random()*4000
    message = "Family members are fighting over inheritance. Relationships are being destroyed."

    if check:
        player.events.add(fname)
        player.c.happiness -= 25
        player.c.stress += 30

        # Decrease affinity with all family members
        from functions import get_allFamily
        family = get_allFamily(player)
        for member in family:
            member.affinity -= 30

    return messageFunction(fname, message, player, check)


__all__ = [
    'parentDivorce',
    'siblingRivalry',
    'familyEstrangement',
    'parentIllness',
    'familyDebt',
    'inheritanceDispute',
]
