"""
Negative Financial Events
Financial problems and setbacks that add realism and challenge

Events (from NEGATIVE_EVENTS_LIST.md - Financial Problems section):
- 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 random
from events.base import messageFunction


def carBreakdown(player, type='message'):
    """Car breaks down requiring expensive repairs"""
    fname = 'carBreakdown'

    # Check conditions: age 16-100, has car
    has_car = any('Car' in item.name for item in player.c.items) if hasattr(player.c, 'items') else False
    check = (fname not in player.events and
             player.c.ageYears >= 16 and
             player.c.ageYears <= 100 and
             has_car and
             1 >= random.random() * 1000)

    message = "Your car broke down and repairs are expensive."

    if check:
        player.events.add(fname)
        if player.c.money >= 1200:
            player.c.money -= 1200
        else:
            # Can't afford full amount
            player.c.money = 0
            player.c.stress += 20  # Additional stress from financial ruin
        player.c.happiness -= 20
        player.c.stress += 20

    return messageFunction(fname, message, player, check)


def scammed(player, type='message'):
    """Fell for a scam and lost money"""
    fname = 'scammed'

    # Check conditions: age 18-100
    check = (fname not in player.events and
             player.c.ageYears >= 18 and
             player.c.ageYears <= 100 and
             1 >= random.random() * 1000)

    message = "You fell for a scam and lost money. You feel stupid."

    if check:
        player.events.add(fname)
        if player.c.money >= 500:
            player.c.money -= 500
        else:
            # Can't afford full amount
            player.c.money = 0
            player.c.stress += 20  # Additional stress from financial ruin
        player.c.happiness -= 25
        # Note: 'confidence' stat doesn't exist in personClass, using happiness as proxy
        player.c.happiness -= 15  # Additional happiness penalty for confidence loss

    return messageFunction(fname, message, player, check)


def identityTheft(player, type='message'):
    """Identity stolen with financial consequences"""
    fname = 'identityTheft'

    # Check conditions: age 18-100
    check = (fname not in player.events and
             player.c.ageYears >= 18 and
             player.c.ageYears <= 100 and
             1 >= random.random() * 2000)

    message = "Someone stole your identity and racked up charges. What a nightmare."

    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 += 40
        player.c.happiness -= 30

    return messageFunction(fname, message, player, check, energyCost=50)


def lostJob(player, type='message'):
    """Laid off from job"""
    fname = 'lostJob'

    # Check conditions: age 18-65, occupation is 'work'
    check = (fname not in player.events and
             player.c.ageYears >= 18 and
             player.c.ageYears <= 65 and
             player.c.occupation == 'work' and
             player.c.job and
             1 >= random.random() * 2000)

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

    if check:
        player.events.add(fname)
        # Remove job
        player.c.job = False
        player.c.occupation = 'unemployed'
        if player.c.money >= 500:
            player.c.money -= 500
        else:
            # Can't afford full amount
            player.c.money = 0
            player.c.stress += 20  # Additional stress from financial ruin
        player.c.happiness -= 40
        player.c.stress += 40

    return messageFunction(fname, message, player, check)


def rentalEviction(player, type='message'):
    """Being evicted from rental property"""
    fname = 'rentalEviction'

    # Check conditions: age 18-100, renting (no house in items, age-appropriate)
    has_house = any(item.name in ['Luxury Villa', 'Personal Island'] for item in player.c.items) if hasattr(player.c, 'items') else False
    likely_renting = not has_house and player.c.ageYears >= 18 and player.c.ageYears <= 40

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

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

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

    return messageFunction(fname, message, player, check, moneyCost=1500)


def taxAudit(player, type='message'):
    """IRS tax audit"""
    fname = 'taxAudit'

    # Check conditions: age 22-100
    check = (fname not in player.events and
             player.c.ageYears >= 22 and
             player.c.ageYears <= 100 and
             1 >= random.random() * 2500)

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

    if check:
        player.events.add(fname)
        if player.c.money >= 800:
            player.c.money -= 800
        else:
            # Can't afford full amount
            player.c.money = 0
            player.c.stress += 20  # Additional stress from financial ruin
        player.c.stress += 35

    return messageFunction(fname, message, player, check, energyCost=30)


def investmentLoss(player, type='message'):
    """Investments tanked and lost money"""
    fname = 'investmentLoss'

    # Check conditions: age 25-100, has investments (money > $10,000 as proxy)
    has_investments = player.c.money > 10000

    check = (fname not in player.events and
             player.c.ageYears >= 25 and
             player.c.ageYears <= 100 and
             has_investments and
             1 >= random.random() * 1500)

    message = "Your investments tanked. You lost a significant amount of money."

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

    return messageFunction(fname, message, player, check)


__all__ = [
    'carBreakdown',
    'scammed',
    'identityTheft',
    'lostJob',
    'rentalEviction',
    'taxAudit',
    'investmentLoss',
]
