from GramAddict.core.dopamine_engine import DopamineEngine
from GramAddict.core.growth_brain import GrowthBrain


class DummyArgs:
    def __init__(self, goals):
        self.goals = goals


def test_autonomous_goals_config_parsing():
    """Test that goals can be parsed from args/config and passed to the brain."""
    args = DummyArgs(goals=["Discover new content", "Engage with community"])

    brain = GrowthBrain(username="test_user")
    dopamine = DopamineEngine()
    dopamine.boredom = 0

    # This should return the first goal initially
    goal = brain.get_current_goal(dopamine, args.goals)

    assert goal in args.goals


def test_autonomous_goal_weighting():
    """Test that GrowthBrain uses success rates to weight goals rather than uniform random choice."""
    brain = GrowthBrain(username="test_user")
    dopamine = DopamineEngine()
    dopamine.boredom = 0

    available_goals = ["goal_A", "goal_B", "goal_C"]

    # Simulate that goal_B has been incredibly successful, goal_A moderately, goal_C not at all.
    success_rates = {"goal_A": 50, "goal_B": 500, "goal_C": 0}

    # If weighting works, running this many times should result in goal_B being chosen overwhelmingly
    choices = {"goal_A": 0, "goal_B": 0, "goal_C": 0}
    for _ in range(1000):
        # We pass success_rates to get_current_goal
        choice = brain.get_current_goal(dopamine, available_goals, success_rates=success_rates)
        choices[choice] += 1

    assert choices["goal_B"] > 800, f"Goal B should be chosen heavily: {choices['goal_B']}"
    assert choices["goal_A"] > 50, f"Goal A should be chosen sometimes: {choices['goal_A']}"
    assert choices["goal_C"] < 50, f"Goal C should be chosen rarely: {choices['goal_C']}"
