"""
E2E tests for Input Map operations.

Tests input map configuration using real Godot demo projects:
- input_setup_action
- input_list_actions
"""

import json
from pathlib import Path


from godot_mcp.server import (
    input_list_actions,
    input_setup_action,
)


class TestSetupInputMap:
    """E2E tests for input_setup_action tool."""

    def test_setup_basic_input_action(self, demo_project_copy: Path) -> None:
        """Test setting up a basic input action in a demo project."""
        result = input_setup_action(
            project_path=str(demo_project_copy),
            action_name="e2e_jump",
            keys=["Space"],
        )

        assert "successfully" in result.lower() or "e2e_jump" in result

        # Verify action was added to project.godot
        project_file = demo_project_copy / "project.godot"
        content = project_file.read_text(encoding="utf-8")
        assert "e2e_jump" in content or "input/e2e_jump" in content

    def test_setup_input_multiple_keys(self, demo_project_copy: Path) -> None:
        """Test setting up an input action with multiple key bindings."""
        result = input_setup_action(
            project_path=str(demo_project_copy),
            action_name="e2e_move_left",
            keys=["A", "Left"],
        )

        assert "successfully" in result.lower() or "e2e_move_left" in result

    def test_setup_wasd_movement(self, demo_project_copy: Path) -> None:
        """Test setting up complete WASD movement controls."""
        movement_actions = [
            ("e2e_up", ["W", "Up"]),
            ("e2e_down", ["S", "Down"]),
            ("e2e_left", ["A", "Left"]),
            ("e2e_right", ["D", "Right"]),
        ]

        for action_name, keys in movement_actions:
            result = input_setup_action(
                project_path=str(demo_project_copy),
                action_name=action_name,
                keys=keys,
            )
            assert "fail" not in result.lower()

    def test_setup_input_with_modifiers(self, demo_project_copy: Path) -> None:
        """Test setting up input action with modifier keys."""
        result = input_setup_action(
            project_path=str(demo_project_copy),
            action_name="e2e_save",
            keys=["Ctrl+S"],
        )

        assert "successfully" in result.lower() or "e2e_save" in result


class TestListInputActions:
    """E2E tests for input_list_actions tool."""

    def test_list_existing_actions(self, any_demo_project: Path) -> None:
        """Test listing input actions on an existing demo project."""
        result = input_list_actions(
            project_path=str(any_demo_project),
        )

        # Demo projects may have input actions defined
        assert isinstance(result, str)

    def test_list_actions_after_setup(self, demo_project_copy: Path) -> None:
        """Test listing input actions after setting some up."""
        # Set up some actions
        input_setup_action(
            project_path=str(demo_project_copy),
            action_name="e2e_test_action1",
            keys=["Space"],
        )
        input_setup_action(
            project_path=str(demo_project_copy),
            action_name="e2e_test_action2",
            keys=["Enter"],
        )

        # List actions
        result = input_list_actions(
            project_path=str(demo_project_copy),
        )

        # Should contain our actions
        if result and "error" not in result.lower():
            try:
                data = json.loads(result)
                action_names = [a["name"] for a in data.get("actions", [])]
                assert "e2e_test_action1" in action_names
                assert "e2e_test_action2" in action_names
            except json.JSONDecodeError:
                # Check project.godot directly
                project_content = (demo_project_copy / "project.godot").read_text()
                assert "e2e_test_action1" in project_content
                assert "e2e_test_action2" in project_content
