"""
Negative Academic Events
Academic setbacks and challenges for students

Events:
- failedTest: Failed an important test (ages 10-22, student)
- rejectedFromCollege: Dream college rejection (ages 17-18)
- academicProbation: Low GPA, academic probation (ages 18-22, college)
- groupProjectBetrayal: Group members didn't contribute (ages 14-22, student)
- plagiarismAccusation: Accused of plagiarism (ages 14-22, student) - questionEvent
"""

import random
from events.base import messageFunction, questionFunction, answerOption


def failedTest(player, type='message'):
    """Failed an important test"""
    fname = 'failedTest'
    check = (fname not in player.events and
             player.c.occupation == 'student' and
             player.c.ageYears >= 10 and
             player.c.ageYears <= 22 and
             1 >= random.random()*800)  # Moderate probability: 1 in 800

    message = "You failed an important test. Your grade is going to suffer."

    if check:
        player.events.add(fname)
        player.c.happiness -= 20
        player.c.stress += 10  # Intelligence proxy - stress from academic failure
        player.c.energy -= 15

    return messageFunction(fname, message, player, check)


def rejectedFromCollege(player, type='message'):
    """Dream college rejection letter"""
    fname = 'rejectedFromCollege'
    check = (fname not in player.events and
             player.c.ageYears >= 17 and
             player.c.ageYears <= 18 and
             1 >= random.random()*1500)  # Lower probability: 1 in 1500

    message = "You got a rejection letter from your dream college. It hurts."

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

    return messageFunction(fname, message, player, check)


def academicProbation(player, type='message'):
    """Placed on academic probation for low GPA"""
    fname = 'academicProbation'
    check = (fname not in player.events and
             player.c.occupation == 'student' and
             'college' in player.c.education and
             player.c.ageYears >= 18 and
             player.c.ageYears <= 22 and
             1 >= random.random()*2000)  # Rare: 1 in 2000

    message = "Your GPA dropped too low. You've been placed on academic probation."

    if check:
        player.events.add(fname)
        player.c.happiness -= 35
        player.c.stress += 40  # Combined stress from probation and academic struggles

    return messageFunction(fname, message, player, check)


def groupProjectBetrayal(player, type='message'):
    """Group members didn't do their part and blamed you"""
    fname = 'groupProjectBetrayal'
    check = (fname not in player.events and
             player.c.occupation == 'student' and
             player.c.ageYears >= 14 and
             player.c.ageYears <= 22 and
             1 >= random.random()*1000)  # Moderate probability: 1 in 1000

    message = "Your group members didn't do their part and blamed you to the teacher. You got a bad grade."

    if check:
        player.events.add(fname)
        player.c.happiness -= 25
        player.c.social -= 15
        # Note: 'trust' attribute doesn't exist in personClass, so omitted

    return messageFunction(fname, message, player, check)


def plagiarismAccusation(player, type='message', message=False, response=False):
    """Accused of plagiarism - must respond with choice"""
    fname = 'plagiarismAccusation'
    check = (fname not in player.askedQuestions and
             player.c.occupation == 'student' and
             player.c.ageYears >= 14 and
             player.c.ageYears <= 22 and
             1 >= random.random()*3000)  # Rare: 1 in 3000

    message = "You're accused of plagiarism on an assignment. How do you respond?"

    answerOptions = [
        answerOption('Prove it was your own work', energyCost=30),
        answerOption('Accept partial blame'),
        answerOption('Fight the accusation', energyCost=40)
    ]

    if (type != 'answer'):
        return questionFunction(fname, message, player, check, answerOptions)
    elif (type == 'answer'):
        if (response['option'] == answerOptions[0]):
            # Prove it was your own work
            player.messageQueue.append("You spent hours gathering evidence and defending your work. The accusation was dropped, but it was exhausting and stressful.")
            player.c.stress += 20
        elif (response['option'] == answerOptions[1]):
            # Accept partial blame
            player.messageQueue.append("You admitted to poor citation practices. It's over quickly, but your grade and confidence took a hit.")
            player.c.happiness -= 15
            player.c.stress += 15  # Stress from accepting blame and grade penalty
        elif (response['option'] == answerOptions[2]):
            # Fight the accusation
            player.messageQueue.append("You fought back aggressively. The process was draining, and your relationships with teachers and classmates suffered.")
            player.c.social -= 20


__all__ = [
    'failedTest',
    'rejectedFromCollege',
    'academicProbation',
    'groupProjectBetrayal',
    'plagiarismAccusation',
]
