"""Tests for watch tool."""

import pytest
from unittest.mock import AsyncMock, patch

from godotiq.tools.bridge.watch import watch


class TestWatchTool:
    @pytest.mark.asyncio
    async def test_start_no_watches(self):
        result = await watch(action="start")
        assert "error" in result

    @pytest.mark.asyncio
    async def test_start_with_watches(self):
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={
            "action": "start",
            "watches_active": 2,
            "sample_interval_ms": 500,
        })
        with patch("godotiq.tools.bridge.watch.get_bridge", return_value=mock_bridge):
            result = await watch(
                action="start",
                watches=[
                    {"node": "Worker_1", "properties": ["state", "global_position"]},
                    {"node": "EconomyManager", "properties": ["cash"]},
                ],
                sample_interval_ms=500,
            )
            assert result["watches_active"] == 2
            params = mock_bridge.request.call_args[1]["params"]
            assert params["action"] == "start"
            assert len(params["watches"]) == 2

    @pytest.mark.asyncio
    async def test_read_events(self):
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={
            "action": "read",
            "events": [
                {"t": 1.2, "node": "Worker_1", "property": "state", "from": "IDLE", "to": "WALKING"},
            ],
            "events_total": 1,
            "watches_active": 2,
        })
        with patch("godotiq.tools.bridge.watch.get_bridge", return_value=mock_bridge):
            result = await watch(action="read")
            assert result["events_total"] == 1
            assert result["events"][0]["property"] == "state"

    @pytest.mark.asyncio
    async def test_stop(self):
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={
            "action": "stop",
            "watches_active": 0,
        })
        with patch("godotiq.tools.bridge.watch.get_bridge", return_value=mock_bridge):
            result = await watch(action="stop")
            assert result["watches_active"] == 0

    @pytest.mark.asyncio
    async def test_min_interval_clamp(self):
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={"action": "start"})
        with patch("godotiq.tools.bridge.watch.get_bridge", return_value=mock_bridge):
            await watch(
                action="start",
                watches=[{"node": "X", "properties": ["y"]}],
                sample_interval_ms=10,
            )
            params = mock_bridge.request.call_args[1]["params"]
            assert params["sample_interval_ms"] == 50

    @pytest.mark.asyncio
    async def test_clear(self):
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={
            "action": "clear",
            "events_cleared": True,
        })
        with patch("godotiq.tools.bridge.watch.get_bridge", return_value=mock_bridge):
            result = await watch(action="clear")
            assert result["events_cleared"] is True
