"""
Career Setback Events
Negative career and workplace events

Events:
- passedOverPromotion: Missed deserved promotion
- workplaceBullying: Coworker making work life miserable
- projectFailure: Major project fails spectacularly
- badPerformanceReview: Harsh performance review
- businessFailure: Business is failing
"""

import random
from events.base import messageFunction


def passedOverPromotion(player, type='message'):
    """Someone less qualified got the promotion you deserved"""
    fname = 'passedOverPromotion'
    check = (fname not in player.events and
             player.c.ageYears >= 25 and
             player.c.ageYears <= 65 and
             player.c.occupation == 'work' and
             getattr(player.c, 'job', False) and
             1 >= random.random()*1000)
    message = "Someone less qualified got the promotion you deserved. You're frustrated and demoralized."

    if check:
        player.events.add(fname)
        player.c.happiness -= 25
        # TODO: Add career stat when implemented: player.c.career -= 10
        player.c.stress += 20

    return messageFunction(fname, message, player, check)


def workplaceBullying(player, type='message'):
    """A coworker has been making your work life miserable"""
    fname = 'workplaceBullying'
    check = (fname not in player.events and
             player.c.ageYears >= 18 and
             player.c.ageYears <= 65 and
             player.c.occupation == 'work' and
             getattr(player.c, 'job', False) and
             1 >= random.random()*1000)
    message = "A coworker has been making your work life miserable. You dread going in."

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

    return messageFunction(fname, message, player, check)


def projectFailure(player, type='message'):
    """A major project you led failed spectacularly"""
    fname = 'projectFailure'
    check = (fname not in player.events and
             player.c.ageYears >= 22 and
             player.c.ageYears <= 65 and
             player.c.occupation == 'work' and
             getattr(player.c, 'job', False) and
             1 >= random.random()*1000)
    message = "A major project you led failed spectacularly. Your reputation has taken a hit."

    if check:
        player.events.add(fname)
        player.c.happiness -= 30
        # TODO: Add career stat when implemented: player.c.career -= 20
        player.c.stress += 25  # Confidence proxy - increased stress from failure

    return messageFunction(fname, message, player, check)


def badPerformanceReview(player, type='message'):
    """Your performance review was harsh"""
    fname = 'badPerformanceReview'
    check = (fname not in player.events and
             player.c.ageYears >= 22 and
             player.c.ageYears <= 65 and
             player.c.occupation == 'work' and
             getattr(player.c, 'job', False) and
             1 >= random.random()*1000)
    message = "Your performance review was harsh. Your boss listed all your shortcomings."

    if check:
        player.events.add(fname)
        player.c.happiness -= 25
        player.c.stress += 45  # Combined stress from harsh review and confidence loss

    return messageFunction(fname, message, player, check)


def businessFailure(player, type='message'):
    """Your business is failing"""
    fname = 'businessFailure'
    check = (fname not in player.events and
             player.c.ageYears >= 25 and
             player.c.ageYears <= 65 and
             getattr(player.c, 'ownesBusiness', False) and
             1 >= random.random()*5000)  # Very rare event
    message = "Your business is failing. You might have to close it down."

    if check:
        player.events.add(fname)
        if player.c.money >= 5000:
            player.c.money -= 5000
        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 += 50
        player.c.energy -= 30

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


__all__ = [
    'passedOverPromotion',
    'workplaceBullying',
    'projectFailure',
    'badPerformanceReview',
    'businessFailure',
]
