"""
Editor Domain Tools

Tools for launching and controlling the Godot editor.
"""

from pathlib import Path

from godot_mcp.core import create_error_response, godot_server, mcp, validate_path

__all__ = ["editor_launch"]


@mcp.tool()
def editor_launch(project_path: str) -> str:
    """Launch the Godot editor for a specific project.

    Category: Editor

    Args:
        project_path: Path to the Godot project directory
    """
    if not project_path:
        return create_error_response("Project path is required")

    if not validate_path(project_path):
        return create_error_response("Invalid project path")

    try:
        # Ensure godot_path is set
        godot_path = godot_server.detect_godot_path()

        # Check if the project directory exists and contains a project.godot file
        project_file = Path(project_path) / "project.godot"
        if not project_file.exists():
            return create_error_response(
                f"Not a valid Godot project: {project_path}",
                ["Ensure the path points to a directory containing a project.godot file"],
            )

        godot_server.log_debug(f"Launching Godot editor for project: {project_path}")

        godot_server.run_subprocess(f'"{godot_path}" -e --path "{project_path}"')

        return f"Godot editor launched successfully for project at {project_path}."
    except Exception as error:
        return create_error_response(
            f"Failed to launch Godot editor: {error}",
            [
                "Ensure Godot is installed correctly",
                "Set GODOT_PATH environment variable",
            ],
        )
