"""Tests for anchor preset operations in node_ops."""

from __future__ import annotations

import pytest
from unittest.mock import AsyncMock, patch

from godotiq.tools.bridge.node_ops import node_ops


class TestAnchorPresets:
    @pytest.mark.asyncio
    async def test_set_anchors_op_forwarded(self):
        """set_anchors op should be forwarded to the bridge."""
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={
            "results": [
                {"op": "set_anchors", "node": "MyButton", "status": "ok",
                 "preset": "full_rect", "anchors": [0, 0, 1, 1]}
            ],
            "scene_modified": True,
            "undo_available": True,
            "action_name": "GodotIQ: 1 node operation(s)",
        })
        with patch("godotiq.tools.bridge.node_ops.get_bridge", return_value=mock_bridge):
            result = await node_ops(operations=[
                {"op": "set_anchors", "node": "MyButton", "preset": "full_rect"}
            ])
            mock_bridge.request.assert_called_once()
            params = mock_bridge.request.call_args[1]["params"]
            assert params["operations"][0]["op"] == "set_anchors"
            assert params["operations"][0]["preset"] == "full_rect"

    @pytest.mark.asyncio
    async def test_set_raw_anchors_forwarded(self):
        """Raw anchor values should be forwarded."""
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={
            "results": [
                {"op": "set_anchors", "node": "MyButton", "status": "ok",
                 "anchors": [0.5, 0.5, 0.5, 0.5]}
            ],
            "scene_modified": True,
            "undo_available": True,
            "action_name": "GodotIQ: 1 node operation(s)",
        })
        with patch("godotiq.tools.bridge.node_ops.get_bridge", return_value=mock_bridge):
            result = await node_ops(operations=[
                {"op": "set_anchors", "node": "MyButton", "anchors": [0.5, 0.5, 0.5, 0.5]}
            ])
            params = mock_bridge.request.call_args[1]["params"]
            assert params["operations"][0]["anchors"] == [0.5, 0.5, 0.5, 0.5]

    @pytest.mark.asyncio
    async def test_set_anchors_no_validation_needed(self):
        """set_anchors should not trigger spatial validation."""
        mock_bridge = AsyncMock()
        mock_bridge.request = AsyncMock(return_value={
            "results": [{"op": "set_anchors", "status": "ok"}],
            "scene_modified": True,
            "undo_available": True,
            "action_name": "GodotIQ: 1 node operation(s)",
        })
        with patch("godotiq.tools.bridge.node_ops.get_bridge", return_value=mock_bridge):
            # validate:true on set_anchors should be ignored (not a spatial op)
            result = await node_ops(
                operations=[{"op": "set_anchors", "node": "Btn", "preset": "center", "validate": True}],
                scene_data={"nodes": []},
            )
            # Should not be blocked
            assert "status" not in result or result.get("status") != "BLOCKED"
