"""Todo and epic management tools for agents.

Creates an MCP server with 'create_todo', 'delete_todo', and 'create_epic'
tools that agents can call to manage todo items and epics on the board.
"""

from claude_agent_sdk import tool, create_sdk_mcp_server

CREATE_TODO_SCHEMA = {
    "type": "object",
    "properties": {
        "title": {
            "type": "string",
            "description": "The title of the todo item. Should be clear and concise.",
        },
        "description": {
            "type": "string",
            "description": "Optional detailed description of the todo item.",
        },
        "epic_id": {
            "type": "string",
            "description": "Optional epic ID to assign this todo to. Use view_board to see available epics.",
        },
        "requires": {
            "type": "array",
            "items": {"type": "string"},
            "description": "Optional list of item IDs that this todo depends on (must be completed before this one can start). Use view_board to see available item IDs.",
        },
        "autostart": {
            "type": "boolean",
            "description": "If true, automatically start an agent to work on this todo item. Starts immediately if no dependencies, or auto-starts when all dependencies are completed. Defaults to false.",
        },
        "auto_approve": {
            "type": "integer",
            "enum": [0, 1, 2],
            "description": (
                "Auto-approval mode for the agent's output. "
                "0 = OFF (default): the agent's work lands in Review for a human. "
                "1 = REVIEW: spawn a read-only review agent and auto-merge if it approves "
                "(if it requests changes, comments go back to the original agent, capped at 3 rounds). "
                "2 = DIRECT: auto-merge as soon as the agent finishes, with no review pass."
            ),
        },
        "use_chrome": {
            "type": "boolean",
            "description": (
                "If true, the agent for this todo launches with browser access "
                "(the claude --chrome tools: navigate, read pages, click, screenshot). "
                "Set this for tasks that involve the web or a running UI. "
                "Leave false (default) for code-only tasks."
            ),
        },
    },
    "required": ["title"],
}

CREATE_EPIC_SCHEMA = {
    "type": "object",
    "properties": {
        "title": {
            "type": "string",
            "description": "The title of the epic. Should describe a higher-level work stream or feature area.",
        },
        "color": {
            "type": "string",
            "description": "Color for the epic. One of: red, orange, amber, green, teal, blue, purple, pink. Defaults to blue.",
            "enum": ["red", "orange", "amber", "green", "teal", "blue", "purple", "pink"],
        },
    },
    "required": ["title"],
}

DELETE_TODO_SCHEMA = {
    "type": "object",
    "properties": {
        "item_id": {
            "type": "string",
            "description": "The ID of the todo item to delete. Use view_board to find item IDs.",
        },
    },
    "required": ["item_id"],
}


def create_todo_server(on_create_todo, on_delete_todo=None, on_create_epic=None):
    """Create an MCP server with todo and epic management tools.

    Args:
        on_create_todo: async callback(title, description, epic_id=None, requires=None,
            autostart=False, auto_approve=0, use_chrome=False) -> dict
            Called when agent uses the create_todo tool.
            Should return the created item info (id, title, etc).
        on_delete_todo: async callback(item_id) -> str
            Called when agent uses the delete_todo tool.
            Should return a confirmation message.
        on_create_epic: async callback(title, color) -> dict
            Called when agent uses the create_epic tool.
            Should return the created epic info (id, title, color).
    """

    @tool(
        "create_todo",
        "Create a new todo item when you identify tasks that need to be done. "
        "Use this to break down work into smaller actionable items, "
        "create follow-up tasks, or note issues that need attention. "
        "Provide a clear title and optionally a detailed description. "
        "IMPORTANT: If this todo depends on other todos being completed first, "
        "pass their item IDs in the 'requires' array — this enforces the dependency "
        "so agents cannot start this task until prerequisites are done. "
        "Set 'autostart' to true to automatically launch an agent on this todo. "
        "If the todo has no dependencies, the agent starts immediately. "
        "If the todo has dependencies, the agent auto-starts once all dependencies are completed. "
        "Set 'auto_approve' to 1 (review) or 2 (direct merge) to skip the human review step "
        "when the agent completes. Defaults to 0 (off) — work lands in Review for a human. "
        "Set 'use_chrome' to true for tasks that need a browser (web pages, a running UI).",
        CREATE_TODO_SCHEMA,
    )
    async def create_todo(input: dict) -> dict:
        """Create a new todo item."""
        title = input.get("title", "")
        description = input.get("description", "")
        epic_id = input.get("epic_id")
        requires = input.get("requires")
        autostart = input.get("autostart", False)
        auto_approve_raw = input.get("auto_approve", 0)
        try:
            auto_approve = int(auto_approve_raw) if auto_approve_raw is not None else 0
        except (TypeError, ValueError):
            auto_approve = 0
        if auto_approve not in (0, 1, 2):
            auto_approve = 0
        use_chrome = bool(input.get("use_chrome", False))
        item_info = await on_create_todo(
            title, description, epic_id, requires, autostart, auto_approve, use_chrome
        )
        msg = f"Created todo item: {item_info['title']} (ID: {item_info['id']})"
        if item_info.get("autostart_scheduled"):
            msg += " — agent auto-start scheduled"
        elif autostart and requires:
            msg += " — agent will auto-start when all dependencies are completed"
        elif autostart:
            msg += " — autostart was requested but could not be scheduled"
        if auto_approve == 1:
            msg += " — auto-approve: review mode"
        elif auto_approve == 2:
            msg += " — auto-approve: direct merge"
        if use_chrome:
            msg += " — browser access enabled"
        return {
            "content": [
                {
                    "type": "text",
                    "text": msg,
                }
            ]
        }

    tools = [create_todo]

    if on_delete_todo:
        @tool(
            "delete_todo",
            "Delete a todo item from the board. Only items in the 'todo' column can be deleted. "
            "Use view_board first to see item IDs. Use this to remove completed planning items "
            "or reorganize the backlog by deleting and recreating items.",
            DELETE_TODO_SCHEMA,
        )
        async def delete_todo(input: dict) -> dict:
            """Delete a todo item."""
            item_id = input.get("item_id", "")
            result = await on_delete_todo(item_id)
            return {
                "content": [
                    {
                        "type": "text",
                        "text": result,
                    }
                ]
            }
        tools.append(delete_todo)

    if on_create_epic:
        @tool(
            "create_epic",
            "Create a new epic to group related todo items into a higher-level work stream. "
            "Use this when you identify a set of related tasks that belong together. "
            "Provide a descriptive title and optionally a color.",
            CREATE_EPIC_SCHEMA,
        )
        async def create_epic(input: dict) -> dict:
            """Create a new epic."""
            title = input.get("title", "")
            color = input.get("color", "blue")
            epic_info = await on_create_epic(title, color)
            return {
                "content": [
                    {
                        "type": "text",
                        "text": f"Created epic: {epic_info['title']} (ID: {epic_info['id']}, color: {epic_info['color']})"
                    }
                ]
            }
        tools.append(create_epic)

    return create_sdk_mcp_server("todo", tools=tools)
