import time

import pytest

from GramAddict.core.dopamine_engine import DopamineEngine


def test_dopamine_engine_wants_to_change_feed():
    try:
        engine = DopamineEngine()
    except Exception as e:
        pytest.fail(f"DopamineEngine failed to initialize: {e}")

    # Set boredom to trigger threshold
    engine.boredom = 90.0

    # Assert that the method exists and returns a boolean (probabilistic, so we just check type)
    result = engine.wants_to_change_feed()
    assert isinstance(result, bool), "wants_to_change_feed() must return a boolean"


def test_dopamine_engine_reset_session_clears_boredom():
    engine = DopamineEngine()

    # Simulate a crashed/burnt out session
    engine.boredom = 100.0
    assert engine.is_app_session_over() is True, "Session should be over when boredom is at 100"

    time.sleep(0.1)  # small buffer for time
    old_start = engine.session_start

    # Trigger the fix
    engine.reset_session()

    # Verify exact state reset
    assert engine.boredom == 0.0, "Boredom must be reset to 0.0 on a new session"
    assert engine.session_start > old_start, "Session start time must be updated"
    assert engine.is_app_session_over() is False, "Session should no longer be over"


def test_dopamine_engine_wants_to_doomscroll():
    engine = DopamineEngine()

    engine.boredom = 50.0
    assert engine.wants_to_doomscroll() is False

    # Trigger doomscroll threshold
    engine.boredom = 95.0
    result = engine.wants_to_doomscroll()
    assert isinstance(result, bool)
