from unittest.mock import MagicMock, patch

import pytest

from GramAddict.core.behaviors.like import LikePlugin


@pytest.fixture
def like_plugin():
    return LikePlugin()


@pytest.fixture
def mock_context():
    ctx = MagicMock()
    ctx.configs = MagicMock()
    ctx.configs.get_plugin_config.return_value = {"percentage": 100}
    ctx.shared_state = {"res_score": 1.0}
    ctx.context_xml = "<xml></xml>"
    ctx.device = MagicMock()

    ctx.session_state = MagicMock()
    ctx.session_state.check_limit.return_value = False
    ctx.session_state.totalLikes = 0

    mock_nav = MagicMock()
    mock_nav.do.return_value = True
    ctx.cognitive_stack = {"nav_graph": mock_nav}
    return ctx


class TestLikePlugin:
    @patch("GramAddict.core.behaviors.like.random.random", return_value=0.1)
    def test_can_activate_enabled(self, mock_random, like_plugin, mock_context):
        assert like_plugin.can_activate(mock_context) is True

    def test_can_activate_disabled_via_config(self, like_plugin, mock_context):
        mock_context.configs.get_plugin_config.return_value = {"percentage": 0}
        assert like_plugin.can_activate(mock_context) is False

    def test_execute_already_liked(self, like_plugin, mock_context):
        mock_nav = mock_context.cognitive_stack["nav_graph"]
        mock_nav.do.return_value = False

        result = like_plugin.execute(mock_context)

        assert result.executed is False

    def test_execute_success(self, like_plugin, mock_context):
        result = like_plugin.execute(mock_context)

        assert result.executed is True
        assert result.interactions == 1

        mock_context.cognitive_stack["nav_graph"].do.assert_called_once_with("tap like button")
