"""MCP tool registrations for features."""

from mcp.server.fastmcp import Context


def register_tools(mcp, api_fn):
    @mcp.tool()
    async def create_feature(
        project_id: str,
        name: str,
        description: str = "",
        status: str = "planned",
        branch: str = "",
        ctx: Context = None,
    ) -> dict | str:
        """Create a new feature for a project.

        Args:
            project_id: The project to create the feature in (org/repo format, e.g. 'pyrabbit/peon-mcp').
            name: Short name/title for the feature.
            description: Detailed description of the feature scope and goals.
            status: Initial status — 'planned' (default), 'in_progress', 'done', or 'cancelled'.
            branch: Git branch name for this feature (optional). Auto-generated from name if omitted.
        """
        return await api_fn(ctx).create_feature(project_id, name, description, status, branch)

    @mcp.tool()
    async def list_features(
        project_id: str,
        status: str | None = None,
        ctx: Context = None,
    ) -> list[dict] | str:
        """List features for a project, optionally filtered by status.

        Args:
            project_id: The project to list features for (org/repo format, e.g. 'pyrabbit/peon-mcp').
            status: Filter by status ('planned', 'in_progress', 'done', 'cancelled'). Omit to show all.
        """
        return await api_fn(ctx).list_features(project_id, status)

    @mcp.tool()
    async def get_feature(feature_id: int, ctx: Context = None) -> dict | str:
        """Get a single feature by its ID.

        Args:
            feature_id: The numeric ID of the feature.
        """
        return await api_fn(ctx).get_feature(feature_id)

    @mcp.tool()
    async def complete_feature(feature_id: int, pr_url: str = "", ctx: Context = None) -> dict | str:
        """Mark a feature as done after all its tasks are complete.

        Validates that all tasks within the feature are done or cancelled before
        marking the feature as complete. If pr_url is not provided, automatically
        creates a PR via GitHub CLI and stores the URL.

        Args:
            feature_id: The numeric ID of the feature to complete.
            pr_url: URL of the PR merging the feature branch to main (optional).
                    If not provided, will auto-create via gh CLI.
        """
        return await api_fn(ctx).complete_feature(feature_id, pr_url)
